双击通知图标后显示窗口(最小化到托盘后)

时间:2012-07-04 14:05:13

标签: wpf notifyicon

我想在我的wpf aplication notify图标中使用(在项目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的新实例后......这里有任何元文件吗?

祝你好运, 达格纳

2 个答案:

答案 0 :(得分: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;
    }
}

在Window1中

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);

答案 1 :(得分:1)

Application.Current.Window1.Show();

这对我有用