WPF 托盘菜单 自定义菜单风格 的做法

背景:我已经自定义标题栏中菜单的风格了, 现在想添加托盘菜单一样的菜单(包括风格也一样)。。  在网上找了好多资料都是用 WinForm 的代码如下

        void InitTrayMenu()
        {
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon();


            notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);


            System.Windows.Forms.ContextMenu contextMenu1 = new System.Windows.Forms.ContextMenu();
            System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem("退出");


            menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            contextMenu1.MenuItems.Clear();
            contextMenu1.MenuItems.Add(menuItem1);


            notifyIcon1.ContextMenu = contextMenu1;


            notifyIcon1.Text = "客户端";
            notifyIcon1.Visible = true;
   
            notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;
        }
   


这样的话, 我是很难绑定原来的风格过来。后来采用如下方式解决了这一问题

     System.Windows.Forms.NotifyIcon notifyIcon1;
        void InitTrayMenu()
        {
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon();
            notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

            notifyIcon1.Text = "客户端";
            notifyIcon1.Visible = true;
			
			// 即使不用这种方式来添加菜单项, 但这一句仍需要添加; 否则托盘菜单在点其他地方的时候不隐藏掉  
			notifyIcon1.ContextMenu = new System.Windows.Forms.ContextMenu();			
			  
            notifyIcon1.DoubleClick += notifyIcon1_DoubleClick;

            notifyIcon1.Click += notifyIcon1_Click; // 在这里处理鼠标右击托盘菜单的
        }


        // 处理 托盘菜单 和 标题栏菜单的消息事件
        private void subMenu_Click(object sender, RoutedEventArgs e)
        {
           
        }


        void notifyIcon1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MouseEventArgs mouseArgs = e as System.Windows.Forms.MouseEventArgs;

            // 如果是鼠标不是右击,则直接返回
            if (null == mouseArgs || mouseArgs.Button != System.Windows.Forms.MouseButtons.Right)
            {
                return;
            }

            ContextMenu menu = (ContextMenu)grid_Container.FindResource("menu"); // 这句是查找资源(那里的菜单风格就可以自己写了)
            menu.IsOpen = true;
        }

        void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            ActiveWindow();// 自己写的一个激活窗口到最前的
        }



而 xaml 文件


    
        
            
                
                
                
                
            
        
    

    
        
            
                
                
                    
                        
                    
                    
                    
                

                
                    
                    
                    
                

                
                
                
                
                    
                        
                    
                
            
             
        
        
        
    











你可能感兴趣的:(WPF 托盘菜单 自定义菜单风格 的做法)