Post by gw0506", 09-28-2007, 11:04
-----------------------------------------------------
MS Button的Click比较特殊,Mouse 和 Space 以及Enter key都可以触发。但是mouse点击button过程中,MouseDown后发KeyDown、KeyUp(任意Key 除了 Space 和Enter)然后Click和MouseClick就不会发了,只有MouseUp。
根据MSDN描述:
Standard Click Event Behavior:
1.MouseDown event.
2.Click event.
3.MouseClick event.
4.MouseUp event.
Note
If the user moves the pointer out of the toggle control while the mouse button is down (such as moving the mouse off the Button control while it is pressed), the toggle control will paint in the raised state and only the MouseUp event occurs. The Click or MouseClick events will not occur in this situation.
所以,现在有两种Case,MS Button 的Click和MouseClick不会触发。但是我没有找到原因。向各位求助。
跟了一下各种正常,非正常case下的事件和消息信息,如下所示:
/-- 正常状态--/
- MouseDown
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- {msg=0x202 (WM_LBUTTONUP) hwnd=0x4d0b08 wparam=0x0 lparam=0xe002a result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- {msg=0x84 (WM_NCHITTEST) hwnd=0x4d0b08 wparam=0x0 lparam=0x13e0260 result=0x0}
- Click
- MouseUp
复制代码
/-- MouseDown 后发KeyDown KeyUp 消息 然后 MouseUp (非space key) --/
- MouseDown
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x100 (WM_KEYDOWN) hwnd=0x4d0b08 wparam=0x90 lparam=0x1450001 result=0x0}
- KeyDown
- {msg=0x101 (WM_KEYUP) hwnd=0x4d0b08 wparam=0x90 lparam=0xffffffffc1450001 result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- KeyUp
- {msg=0x202 (WM_LBUTTONUP) hwnd=0x4d0b08 wparam=0x0 lparam=0xa0022 result=0x0}
- MouseUp
复制代码 /-- MouseDown 后发KeyDown KeyUp 消息 然后 MouseUp (space key)--/
- MouseDown
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x100 (WM_KEYDOWN) hwnd=0x4d0b08 wparam=0x90 lparam=0x1450001 result=0x0}
- KeyDown
- {msg=0x101 (WM_KEYUP) hwnd=0x4d0b08 wparam=0x90 lparam=0xffffffffc1450001 result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- KeyUp
- {msg=0x2a1 (WM_MOUSEHOVER) hwnd=0x4d0b08 wparam=0x1 lparam=0xd0031 result=0x0}
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x100 (WM_KEYDOWN) hwnd=0x4d0b08 wparam=0x20 lparam=0x390001 result=0x0}
- KeyDown
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x87 (WM_GETDLGCODE) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- {msg=0x102 (WM_CHAR) hwnd=0x4d0b08 wparam=0x20 lparam=0x390001 result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- {msg=0x101 (WM_KEYUP) hwnd=0x4d0b08 wparam=0x20 lparam=0xffffffffc0390001 result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- Click
- KeyUp
- {msg=0x202 (WM_LBUTTONUP) hwnd=0x4d0b08 wparam=0x0 lparam=0xd0031 result=0x0}
- MouseUp
复制代码 /-- mousedown -> move out of control -> mouse up (MSDN situation)--/
- MouseDown
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- {msg=0x200 (WM_MOUSEMOVE) hwnd=0x4d0b08 wparam=0x1 lparam=0xd003b result=0x0}
- {msg=0x200 (WM_MOUSEMOVE) hwnd=0x4d0b08 wparam=0x1 lparam=0xd003c result=0x0}
- ......
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- {msg=0x202 (WM_LBUTTONUP) hwnd=0x4d0b08 wparam=0x0 lparam=0x3700a5 result=0x0}
- {msg=0xf (WM_PAINT) hwnd=0x4d0b08 wparam=0x0 lparam=0x0 result=0x0}
- Paint
- MouseUp
复制代码 Reply by "KevinShan", 09-28-2007, 11:11
-----------------------------------------------------
在ButtonBase类的OnKeyUp这里,^_^
- protected override void OnKeyUp(KeyEventArgs kevent)
- {
- if (this.GetFlag(2) && !base.ValidationCancelled)
- {
- if (this.OwnerDraw)
- {
- this.ResetFlagsandPaint();
- }
- else
- {
- this.SetFlag(4, false);
- this.SetFlag(2, false);
- base.SendMessage(0xf3, 0, 0);
- }
- if ((kevent.KeyCode == Keys.Return) || (kevent.KeyCode == Keys.Space))
- {
- this.OnClick(EventArgs.Empty);
- }
- kevent.Handled = true;
- }
- base.OnKeyUp(kevent);
- }
复制代码 Reply by "winking", 10-04-2007, 13:49
-----------------------------------------------------
Kevin好歹也给解释清楚嘛!!
其实Kevin给出了问题的关键所在,在ButtonBase类上处理KeyUp时清楚了一个内部标志,而发Click事件恰好需要验证它:
- protected override void OnKeyUp(KeyEventArgs kevent)
- {
- if (this.GetFlag(2) && !base.ValidationCancelled)
- {
- if (this.OwnerDraw)
- {
- this.ResetFlagsandPaint();
- }
- else
- {
- this.SetFlag(4, false);
- this.SetFlag(2, false); //关键在就在这里,所有Key消息处理时,清除Flag(2)这个标志
- base.SendMessage(0xf3, 0, 0);
- }
- if ((kevent.KeyCode == Keys.Return) || (kevent.KeyCode == Keys.Space))
- {
- this.OnClick(EventArgs.Empty);
- }
- kevent.Handled = true;
- }
- base.OnKeyUp(kevent);
- }
复制代码 而在Button类上override了OnMouseUp:
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- if ((mevent.Button == MouseButtons.Left) && base.MouseIsPressed)
- {
- bool mouseIsDown = base.MouseIsDown; // 此处取了MouseIsDown,
- if (base.GetStyle(ControlStyles.UserPaint))
- {
- base.ResetFlagsandPaint();
- }
- if (mouseIsDown) //这里验证了MouseIsDown,只有它为True才会走里面逻辑,发Click和MouseClick事件。
- {
- Point point = base.PointToScreen(new Point(mevent.X, mevent.Y));
- if ((UnsafeNativeMethods.WindowFromPoint(point.X, point.Y) == base.Handle) && !base.ValidationCancelled)
- {
- if (base.GetStyle(ControlStyles.UserPaint))
- {
- this.OnClick(mevent);
- }
- this.OnMouseClick(mevent);
- }
- }
- }
- base.OnMouseUp(mevent);
- }
复制代码 很显然你想问那MouseIsDown里又是什么东西,这个是ButtonBase上的属性,定义如下:
- internal bool MouseIsDown
- {
- get
- {
- return this.GetFlag(2);
- }
- }
复制代码 |
|