通过监听Windows消息对复合控件进行整体控制(C#)二
前篇说了对消息监听的一些粗的实现,现在具体说说消息监听:
在实现IMessageFilter接口的成员方法PreFilterMessage做处理。
在这里,实现需要监听的Windows消息只要有:
代码
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYUP = 0x105;
const int WM_MOUSEMOVE = 0x200;
const int WM_MOUSEHOVER = 0x2a1;
const int WM_MOUSELEAVE = 0x2a3;
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x202;
const int WM_LBUTTONDBLCLK = 0x203;
const int WM_RBUTTONDOWN = 0x204;
const int WM_RBUTTONUP = 0x205;
const int WM_RBUTTONDBLCLK = 0x206;
const int WM_NCMOUSEMOVE = 0xa0;
const int WM_NCLBUTTONDOWN = 0xa1;
const int WM_NCLBUTTONUP = 0xa2;
const int WM_MOUSEWHEEL = 0x20a;
const int WM_SYSKEYDOWN = 0x104;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYUP = 0x105;
const int WM_MOUSEMOVE = 0x200;
const int WM_MOUSEHOVER = 0x2a1;
const int WM_MOUSELEAVE = 0x2a3;
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x202;
const int WM_LBUTTONDBLCLK = 0x203;
const int WM_RBUTTONDOWN = 0x204;
const int WM_RBUTTONUP = 0x205;
const int WM_RBUTTONDBLCLK = 0x206;
const int WM_NCMOUSEMOVE = 0xa0;
const int WM_NCLBUTTONDOWN = 0xa1;
const int WM_NCLBUTTONUP = 0xa2;
const int WM_MOUSEWHEEL = 0x20a;
同时很感谢大家提供的精彩对鼠标消息转换的代码
代码
//----------开源代码----------------------
int zDelta;
private const int MK_LBUTTON = 0x0001;
private const int MK_RBUTTON = 0x0002;
private const int MK_SHIFT = 0x0004;
private const int MK_CONTROL = 0x0008;
private const int MK_MBUTTON = 0x0010;
private const int MK_XBUTTON1 = 0x0020;
private const int MK_XBUTTON2 = 0x0040;
public static int GetXLParam(int lparam) { return LowWord(lparam); }
public static int GetYLParam(int lparam) { return HighWord(lparam); }
public static int LowWord(int word) { return word & 0xFFFF; }
public static int HighWord(int word) { return word >> 16; }
public static int GetWheelDeltaWParam(int wparam) { return HighWord(wparam); }
public static MouseButtons GetMouseButtonWParam(int wparam)
{
int mask = LowWord(wparam);
if ((mask & MK_LBUTTON) == MK_LBUTTON) return MouseButtons.Left;
if ((mask & MK_RBUTTON) == MK_RBUTTON) return MouseButtons.Right;
if ((mask & MK_MBUTTON) == MK_MBUTTON) return MouseButtons.Middle;
if ((mask & MK_XBUTTON1) == MK_XBUTTON1) return MouseButtons.XButton1;
if ((mask & MK_XBUTTON2) == MK_XBUTTON2) return MouseButtons.XButton2;
return MouseButtons.None;
}
int zDelta;
private const int MK_LBUTTON = 0x0001;
private const int MK_RBUTTON = 0x0002;
private const int MK_SHIFT = 0x0004;
private const int MK_CONTROL = 0x0008;
private const int MK_MBUTTON = 0x0010;
private const int MK_XBUTTON1 = 0x0020;
private const int MK_XBUTTON2 = 0x0040;
public static int GetXLParam(int lparam) { return LowWord(lparam); }
public static int GetYLParam(int lparam) { return HighWord(lparam); }
public static int LowWord(int word) { return word & 0xFFFF; }
public static int HighWord(int word) { return word >> 16; }
public static int GetWheelDeltaWParam(int wparam) { return HighWord(wparam); }
public static MouseButtons GetMouseButtonWParam(int wparam)
{
int mask = LowWord(wparam);
if ((mask & MK_LBUTTON) == MK_LBUTTON) return MouseButtons.Left;
if ((mask & MK_RBUTTON) == MK_RBUTTON) return MouseButtons.Right;
if ((mask & MK_MBUTTON) == MK_MBUTTON) return MouseButtons.Middle;
if ((mask & MK_XBUTTON1) == MK_XBUTTON1) return MouseButtons.XButton1;
if ((mask & MK_XBUTTON2) == MK_XBUTTON2) return MouseButtons.XButton2;
return MouseButtons.None;
}
对消息的处理逻辑如下:
代码
#region IMessageFilter 成员
public bool PreFilterMessage(ref Message m)
{
////System.Diagnostics.Debug.WriteLine(m);
bool isCancel = false;
switch (m.Msg)
{
case WM_MOUSEHOVER:
ProcessMouseHover(m.HWnd);
break;
case WM_MOUSEWHEEL: //鼠标滚轮
ProcessMouseWheel(ref m, ref isCancel);
break;
case WM_LBUTTONDOWN: //要处理MouseClick事件
case WM_RBUTTONDOWN:
ProcessMouseDown(ref m, ref isCancel);
break;
case WM_LBUTTONDBLCLK: //MouseDoubleClick事件
case WM_RBUTTONDBLCLK:
ProcessMouseDBCLK(ref m, ref isCancel);
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
ProcessMouseUp(ref m, ref isCancel);
break;
case WM_MOUSEMOVE:
ShowCursor(); //显示光标。
ProcessMouseMove(ref m, ref isCancel);
break;
case WM_MOUSELEAVE:
ProcessMouseLeave(ref m, ref isCancel);
break;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
ShowCursor();
ProcessKeyDown(ref m, ref isCancel);
break;
case WM_SYSKEYUP:
case WM_KEYUP:
ProcessKeyUp(ref m, ref isCancel);
break;
}
return isCancel;
}
#endregion
public bool PreFilterMessage(ref Message m)
{
////System.Diagnostics.Debug.WriteLine(m);
bool isCancel = false;
switch (m.Msg)
{
case WM_MOUSEHOVER:
ProcessMouseHover(m.HWnd);
break;
case WM_MOUSEWHEEL: //鼠标滚轮
ProcessMouseWheel(ref m, ref isCancel);
break;
case WM_LBUTTONDOWN: //要处理MouseClick事件
case WM_RBUTTONDOWN:
ProcessMouseDown(ref m, ref isCancel);
break;
case WM_LBUTTONDBLCLK: //MouseDoubleClick事件
case WM_RBUTTONDBLCLK:
ProcessMouseDBCLK(ref m, ref isCancel);
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
ProcessMouseUp(ref m, ref isCancel);
break;
case WM_MOUSEMOVE:
ShowCursor(); //显示光标。
ProcessMouseMove(ref m, ref isCancel);
break;
case WM_MOUSELEAVE:
ProcessMouseLeave(ref m, ref isCancel);
break;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
ShowCursor();
ProcessKeyDown(ref m, ref isCancel);
break;
case WM_SYSKEYUP:
case WM_KEYUP:
ProcessKeyUp(ref m, ref isCancel);
break;
}
return isCancel;
}
#endregion
其他 ShowCursor(); 是一个附加的自动隐藏光标类的一个显示方法,当我们接收到鼠标或键盘的动作时,就必需要显示光标。
需要注意的事项:
作为一个事件的消息控件,当控件的某一子控件产生了消息,如果控件订阅了该消息,系统会自动调用控件相对应的方法,如OnMouseEnter等,如有需要对消息进行过滤,该消息就不会发往子控件了。对消息过滤也会产生副作用,如过滤了WM_MOUSELEAVE 消息后,当下一次鼠标再离开该子控件,就不会再发送鼠标离开的消息了,不是很Windows系统的消息机制,希望有经验的高手作下说明。
还有,没有鼠标进入的消息,只有鼠标移动的消息,因此,实现中要保存上一次鼠标移动的控件的标志,通过比较实现鼠标进入和移出事件。MouseUp消息也没有 wparam 也没有值,不能使用GetMouseButtonWParam方法。的MouseDown消息中要实现MouseClick事件,在MouseMove消息中实现MouseEnter,MouseLeave事件等。
至此一个对控件事先整体的消息控制功能完成了,该程序经测试通过,并应用到现有项目,效果非常好,只要应用在如浮动式窗口的自动隐藏、复合控件对鼠标进出而进行边框的渲染等,希望大家能挖掘出更多的应用,并留言说说。下面附上原码,其他有个自动隐藏光标的类,逻辑简单,不再累赘。