WPF 通知栏图标和右键菜单

WPF没有自带的通知栏图标组件,需要引用Windows类库,具体代码如下:

        public MainWindow()
        {
            InitializeComponent();
            icon();
            wsl = WindowState.Minimized;
        }
#region 通知栏

            WindowState wsl;
            System.Windows.Forms.NotifyIcon notifyIcon = null;
        
            private void icon()
            {
                this.notifyIcon = new System.Windows.Forms.NotifyIcon();
                this.notifyIcon.BalloonTipText = "ECMS 服务正在运行..."; //设置程序启动时显示的文本
                this.notifyIcon.Text = "ECMS 服务";//最小化到托盘时,鼠标点击时显示的文本
                this.notifyIcon.Icon = new System.Drawing.Icon("./logo_48X48.ico");//程序图标
                this.notifyIcon.Visible = true;

                //右键菜单--打开菜单项
                System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
                open.Click += new EventHandler(ShowWindow);
                //右键菜单--退出菜单项
                System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
                exit.Click += new EventHandler(CloseWindow);
                //关联托盘控件
                System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
                notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

                notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
                this.notifyIcon.ShowBalloonTip(1000);
            }

            private void OnNotifyIconDoubleClick(object sender, EventArgs e)
            {
                /*
                 * 这一段代码需要解释一下:
                 * 窗口正常时双击图标执行这段代码是这样一个过程:
                 * this.Show()-->WindowState由Normail变为Minimized-->Window_StateChanged事件执行(this.Hide())-->WindowState由Minimized变为Normal-->窗口隐藏
                 * 窗口隐藏时双击图标执行这段代码是这样一个过程:
                 * this.Show()-->WindowState由Normail变为Minimized-->WindowState由Minimized变为Normal-->窗口显示
                 */
                this.Show();
                this.WindowState = WindowState.Minimized;
                this.WindowState = WindowState.Normal;
            }

            private void Window_StateChanged(object sender, EventArgs e)
            {
                //窗口最小化时隐藏任务栏图标
                 if (WindowState == WindowState.Minimized)
                {
                    this.Hide();
                }
            }

            private void ShowWindow(object sender, EventArgs e)
            {
                this.Visibility = System.Windows.Visibility.Visible;
                this.ShowInTaskbar = true;
                this.Activate();
            }

            private void HideWindow(object sender, EventArgs e)
            {
                this.ShowInTaskbar = false;
                this.Visibility = System.Windows.Visibility.Hidden;
            }

            private void CloseWindow(object sender, EventArgs e)
            {
                System.Windows.Application.Current.Shutdown();
            }

        #endregion


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
WPF中,可以通过以下步骤为DataGrid增加菜单: 1. 首先,在XAML文件中定义一个ContextMenu,用于作为菜单的内容。可以在Window或者UserControl的资源部分定义ContextMenu,例如: ```xaml <Window.Resources> <ContextMenu x:Key="DataGridContextMenu"> <MenuItem Header="编辑" Click="EditMenuItem_Click"/> <MenuItem Header="删除" Click="DeleteMenuItem_Click"/> </ContextMenu> </Window.Resources> ``` 2. 然后,在DataGrid的样式中,使用EventSetter来为DataGrid的PreviewMouseRightButtonDown事件添加处理程序。在处理程序中,将ContextMenu设置为菜单,并设置菜单的PlacementTarget为DataGrid,以确保菜单与DataGrid关联。例如: ```xaml <DataGrid> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <EventSetter Event="PreviewMouseRightButtonDown" Handler="DataGridRow_PreviewMouseRightButtonDown"/> </Style> </DataGrid.RowStyle> </DataGrid> ``` 3. 最后,在代码文件中实现事件处理程序,将ContextMenu设置为菜单,并将菜单的PlacementTarget设置为DataGrid。例如: ```csharp private void DataGridRow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { DataGridRow row = sender as DataGridRow; if (row != null) { DataGrid dataGrid = FindVisualParent<DataGrid>(row); if (dataGrid != null) { ContextMenu contextMenu = dataGrid.Resources["DataGridContextMenu"] as ContextMenu; if (contextMenu != null) { contextMenu.PlacementTarget = dataGrid; contextMenu.IsOpen = true; e.Handled = true; } } } } private static T FindVisualParent<T>(UIElement element) where T : UIElement { UIElement parent = element; while (parent != null) { T foundElement = parent as T; if (foundElement != null) { return foundElement; } parent = VisualTreeHelper.GetParent(parent) as UIElement; } return null; } ``` 这样,当在DataGrid的行上点击时,就会显示出定义的菜单

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值