【问题标题】:Show window after double click in notify icon (after minimize to tray)双击通知图标后显示窗口(最小化到托盘后)
【发布时间】:2017-01-05 01:26:11
【问题描述】:

我想在我的 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 双击打开新实例后...这里有什么方法吗?

最好的问候, 达格纳

【问题讨论】:

    标签: wpf notifyicon


    【解决方案1】:

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

    命令

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

    【讨论】:

    • 对不起,我忘记了RegisterWindowMessage方法
    【解决方案2】:
    Application.Current.Window1.Show();
    

    这对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      相关资源
      最近更新 更多