给窗体的任务栏右键菜单增加项目

本文介绍如何使用 C# 在 Windows 应用程序中为窗体的任务栏右键菜单添加自定义项目,包括添加分隔符及指定操作的菜单项,并通过消息处理响应这些菜单项的点击事件。

给窗体的任务栏右键菜单增加项目

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsFormsApplication1

...{

    public partial class Form1 : Form

    ...{

        Class1 SkinClass = new Class1();

 

        public Form1()

        ...{

            InitializeComponent();

 

            SetupSystemMenu();

        }

 

        [DllImport("user32.dll")]

        private static extern int GetSystemMenu(int hwnd, int bRevert);

 

        [DllImport("user32.dll")]

        private static extern int AppendMenu(

        int hMenu, int Flagsw, int IDNewItem, string lpNewItem);

 

        private void SetupSystemMenu()

        ...{

            //   get   handle   to   system   menu 

            int menu = GetSystemMenu(this.Handle.ToInt32(), 0);

            //   add   a   separator 

            AppendMenu(menu, 0xA00, 0, null);

            //   add   an   item   with   a   unique   ID 

            AppendMenu(menu, 0, 1234, "跳至URL");

            AppendMenu(menu, 0, 1235, "关于HTML帮助");

        }

 

        protected override void WndProc(ref Message m)

        ...{

            base.WndProc(ref   m);

            //   WM_SYSCOMMAND   is   0x112 

            if (m.Msg == 0x112)

            ...{

                //   check   for   my   new   menu   item   ID 

                if (m.WParam.ToInt32() == 1234)

                ...{

                    //   show   About   box   here... 

                    MessageBox.Show("Btn One");

                }

                if (m.WParam.ToInt32() == 1235)

                ...{

                    //   show   About   box   here... 

                    MessageBox.Show("Btn Two");

                }

            }

        }

 

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值