最近遇到需要屏蔽任务栏的右键弹出菜单问题,小费了番周折,写出来希望对需要的人有帮助。
网上查了半天资料,修改注册表可以实现,但有个缺点需要重启机器,甚为不爽。还有用鼠标钩子截获鼠标右键消息来达到目的,这里需要注意一点,当鼠标右键释放时菜单才会弹出(由于开始只截获了鼠标按下的消息,程序一直不灵,一度怀疑用钩子实现的可能性,哎,细心细心啊)。
关于c#调用Windows API和钩子的使用,资料很多就不细说了。可以参考
http://blog.csdn.net/LeoMaya/archive/2007/05/18/1615052.aspx
http://blog.csdn.net/wztgq/archive/2006/08/02/1012132.aspx
还有两个Api的浏览器,相当不错
FoxAPI中文函数浏览器V1.5(有详细的Api函数的说明)
API Explorer V2.82(可以把Api函数转换成C#和VB.Net代码)
在Visual Studio 2005中调试通过,工程可在http://download.csdn.net/source/536490下载
///Author:danseshi ///Email:[email protected] ///Bolg:http://blog.csdn.net/danseshi/ ///Date:2008.7.12 using System; using System.Windows.Forms; using System.Drawing; using System.Runtime.InteropServices; using System.Diagnostics; namespace MouseHook { public partial class Form1 : Form { #region Fields private int hMouseHook = 0; //全局钩子常量 private const int WH_MOUSE_LL = 14; //声明消息的常量,鼠标按下和释放 private const int WM_RBUTTONDOWN = 0x204; private const int WM_RBUTTONUP = 0x205; //保存任务栏的矩形区域 private Rectangle taskBarRect; private Rectangle newTaskBarRect; //定义委托 public delegate int HookProc(int nCode, int wParam, IntPtr lParam); private HookProc MouseHookProcedure; #endregion #region 声明Api函数,需要引入空间(System.Runtime.InteropServices) //寻找符合条件的窗口 [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName ); //获取窗口的矩形区域 [DllImport("user32.dll", EntryPoint = "GetWindowRect")] public static extern int GetWindowRect( int hwnd, ref Rectangle lpRect ); //安装钩子 [DllImport("user32.dll")] public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId ); //卸载钩子 [DllImport("user32.dll", EntryPoint = "UnhookWindowsHookEx")] public static extern bool UnhookWindowsHookEx( int hHook ); //调用下一个钩子 [DllImport("user32.dll")] public static extern int CallNextHookEx( int idHook, int nCode, int wParam, IntPtr lParam ); //获取当前线程的标识符 [DllImport("kernel32.dll")] public static extern int GetCurrentThreadId(); //获取一个应用程序或动态链接库的模块句柄 [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); //鼠标结构,保存了鼠标的信息 [StructLayout(LayoutKind.Sequential)] public struct MOUSEHOOKSTRUCT { public Point pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; } #endregion public Form1() { InitializeComponent(); } /// <summary> /// 安装钩子 /// </summary> private void StartHook() { if (hMouseHook == 0) { hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); if (hMouseHook == 0) {//如果设置钩子失败. this.StopHook(); MessageBox.Show("Set windows hook failed!"); } } } /// <summary> /// 卸载钩子 /// </summary> private void StopHook() { bool stop = true; if (hMouseHook != 0) { stop = UnhookWindowsHookEx(hMouseHook); hMouseHook = 0; if (!stop) {//卸载钩子失败 MessageBox.Show("Unhook failed!"); } } } private int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //把参数lParam在内存中指向的数据转换为MOUSEHOOKSTRUCT结构 MOUSEHOOKSTRUCT mouse = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));//鼠标 //这句为了看鼠标的位置 this.Text = "MousePosition:" + mouse.pt.ToString(); if (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP) { //鼠标按下或者释放时候截获 if (newTaskBarRect.Contains(mouse.pt)) { //当鼠标在任务栏的范围内 //如果返回1,则结束消息,这个消息到此为止,不再传递。 //如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者 return 1; } } } return CallNextHookEx(hMouseHook, nCode, wParam, lParam); } #region Events private void Form1_Load(object sender, EventArgs e) { MouseHookProcedure = new HookProc(MouseHookProc); //taskBarHandle为返回的任务栏的句柄 //Shell_TrayWnd为任务栏的类名 int taskBarHandle = FindWindow("Shell_TrayWnd", null); //获得任务栏的区域 //有一点要注意,函数返回时,taskBarRect包含的是窗口的左上角和右下角的屏幕坐标 //就是说taskBarRect.Width和taskBarRect.Height是相对于屏幕左上角(0,0)的数值 //这与c#的Rectangle结构是不同的 GetWindowRect(taskBarHandle, ref taskBarRect); this.richTextBox1.Text = "taskBarRect.Location:" + taskBarRect.Location.ToString() + "/n"; this.richTextBox1.Text += "taskBarRect.Size:" + taskBarRect.Size.ToString() + "/n/n"; //构造一个c#中的Rectangle结构 newTaskBarRect = new Rectangle( taskBarRect.X, taskBarRect.Y, taskBarRect.Width - taskBarRect.X, taskBarRect.Height - taskBarRect.Y ); this.richTextBox1.Text += "newTaskBarRect.Location:" + newTaskBarRect.Location.ToString() + "/n"; this.richTextBox1.Text += "newTaskBarRect.Size:" + newTaskBarRect.Size.ToString(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { this.StopHook(); } private void button1_Click(object sender, EventArgs e) { this.StartHook(); this.button1.Enabled = false; this.button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { this.StopHook(); this.button1.Enabled = true; this.button2.Enabled = false; } #endregion } }
转载自:http://blog.csdn.net/danseshi/article/details/2643440