拦截信息方法

但是你可以拦截这个控件的双击消息。 以下代码可加在控件的父窗口(假设为Form1),并在Form1初始化后安装拦截挂钩。同样的方法也可屏蔽那控件的其它消息,如屏蔽它的右键事件。

private const int GWL_WNDPROC = -4;

private const int WM_LBUTTONDOWN=0x0201;

private const int WM_LBUTTONUP=0x0202;

private const int WM_LBUTTONDBLCLK=0x0203;//双击消息

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, MyWndProc wndProc);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

 

public delegate IntPtr MyWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

 

private IntPtr OldWndProc = IntPtr.Zero;

private MyWndProc Wpr = null;

private IntPtr MyControlWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) {

 switch (msg)

 { case WM_LBUTTONDBLCLK://拦截双击消息

/*在这里,你想干什么就干什么*/

....................... .......................

return (IntPtr)0;

default:

 return CallWindowProc(OldWndProc, hWnd, msg, wParam, lParam);

}

}

//假设那个控件变量名为 _thatControl

 

private void InitializeThatControl()

{ //初始化的时候安装消息拦截

this.Wpr = new MyWndProc(this.MyControlWndProc);

this.OldWndProc = SetWindowLong(_thatControl.Handle, GWL_WNDPROC, Wpr);

}

 

public Form1()

 {

InitializeComponent();

InitializeThatControl();//加这句,在Form初始化后安装消息拦截

}

 

你可能感兴趣的:(方法)