2

我想在我的 wpf 应用程序通知图标中使用(在项目http://www.codeproject.com/Articles/36468/WPF-NotifyIcon中使用 .dll 库)。

但我不知道,如何通过双击托盘图标来显示我的窗口(最小化到托盘后)。

我宣布了新的命令

namespace MyBasicFlyffKeystroke
{
    class ShowWindowCommand : ICommand
    {
        public void Execute(object parameter)
        {
            Window1 window = new Window1();
            window.Show();
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}

我在 window1.xaml 文件中使用了它:

<tb:TaskbarIcon x:Name="notifyIcon" IconSource="icon.ico" ToolTipText="MyBasicFlyffKeystroke" 
    DoubleClickCommand="{StaticResource ShowWindow}">                    
</tb:TaskbarIcon>

<Grid.Resources>
    <my:ShowWindowCommand x:Key="ShowWindow" />
</Grid.Resources>

但是在用 Window1 双击打开新实例后...这里有什么方法吗?

最好的问候, 达格纳

4

2 回答 2

3

尝试为窗口消息添加事件处理程序

命令

namespace MyBasicFlyffKeystroke
{
    class ShowWindowCommand : ICommand
    {
        public void Execute(object parameter)
        {
            // Broadcast isn't a good idea but work...
            NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}

在窗口 1

protected override void OnSourceInitialized(EventArgs e) {
    base.OnSourceInitialized(e);
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
    if (msg == NativeMethods.WM_SHOWME) {
        WindowState = WindowState.Normal;
    }
    return IntPtr.Zero;
}

在 NativeMethods 中(更新)

public static readonly int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");

[DllImport("user32.dll")]
public static extern int RegisterWindowMessage(string message);

[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
于 2012-07-04T14:25:34.690 回答
1
Application.Current.Window1.Show();

这对我有用

于 2017-01-04T17:26:11.673 回答