标题: DirectX Sample分析:Framework中的控件事件绑定(一)
- Junhot 2004-12-16 13:40 阅读:1610
- 评论:2 查看评论 | 添加评论

作者:Junhot
更新时间:2004-12-15
-------------------------------------------------------------------------------
总的流程描述图:

[图片]


从具体控件看起,以鼠标点击Button控件的事件为例,Button控件代码包含在dxmutgui.cs文件中:

 

Button的Click事件的原型:

在Button中声明了如下的事件Click及事件触发方法RaiseClickEvent
[图片][图片]        Event code#region Event code
[图片]        public event EventHandler Click;
[图片][图片]        /**//// <summary>Create new button instance</summary>
[图片]        protected void RaiseClickEvent(Button sender, bool wasTriggeredByUser)
[图片][图片]        [图片]{
[图片]            // Discard events triggered programatically if these types of events haven't been
[图片]            // enabled
[图片]            if (!Parent.IsUsingNonUserEvents && !wasTriggeredByUser)
[图片]                return;
[图片]
[图片]            if (Click != null)
[图片]                Click(sender, EventArgs.Empty);
[图片]        }
[图片]        #endregion

Button内部如何触发Click事件:

HandleMouse这个方法调用RaiseClickEvent方法从而导致Click事件的触发,另外还有如HandleKeyboard方法等,同样也是事件触发的方法之一。

[图片]
[图片][图片]        /**//// <summary>
[图片]        /// Handle mouse messages from the buttons
[图片]        /// </summary>
[图片]        public override bool HandleMouse(NativeMethods.WindowMessage msg, System.Drawing.Point pt, IntPtr wParam, IntPtr lParam)
[图片][图片]        [图片]{
[图片]            if (!IsEnabled || !IsVisible)
[图片]                return false;
[图片]
[图片]            switch(msg)
[图片][图片]            [图片]{
[图片]                case NativeMethods.WindowMessage.LeftButtonDoubleClick:
[图片]                case NativeMethods.WindowMessage.LeftButtonDown:
[图片][图片]                [图片]{
[图片]                    if (ContainsPoint(pt))
[图片][图片]                    [图片]{
[图片]                        // Pressed while inside the control
[图片]                        isPressed = true;
[图片]                        NativeMethods.SetCapture(Parent.SampleFramework.Window);
[图片]                        if (!hasFocus)
[图片]                            Dialog.RequestFocus(this);
[图片]
[图片]                        return true;
[图片]                    }
[图片]                }
[图片]                    break;
[图片]                case NativeMethods.WindowMessage.LeftButtonUp:
[图片][图片]                [图片]{
[图片]                    if (isPressed)
[图片][图片]                    [图片]{
[图片]                        isPressed = false;
[图片]                        NativeMethods.ReleaseCapture();
[图片]                        if (!parentDialog.IsUsingKeyboardInput)
[图片]                            Dialog.ClearFocus();
[图片]
[图片]                        // Button click
[图片]                        if (ContainsPoint(pt))
[图片]                            RaiseClickEvent(this, true);
[图片]                    }
[图片]                }
[图片]                    break;
[图片]            }
[图片]
[图片]            return false;
[图片]        }

控件如何知道何时该触发Click事件:

在Dialog类中(Dialog类代码同样包含在dxmutgui.cs中)通过MessageProc捕获WindowMessage,然后调用Control的HandleKeyboard或者HandleMouse方法,下面是调用HandleMouse方法的代码:

[图片]
[图片]                // Mouse messages
[图片]                case NativeMethods.WindowMessage.MouseMove:
[图片]                case NativeMethods.WindowMessage.MouseWheel:
[图片]                case NativeMethods.WindowMessage.LeftButtonUp:
[图片]                case NativeMethods.WindowMessage.LeftButtonDown:
[图片]                case NativeMethods.WindowMessage.LeftButtonDoubleClick:
[图片]                case NativeMethods.WindowMessage.RightButtonUp:
[图片]                case NativeMethods.WindowMessage.RightButtonDown:
[图片]                case NativeMethods.WindowMessage.RightButtonDoubleClick:
[图片]                case NativeMethods.WindowMessage.MiddleButtonUp:
[图片]                case NativeMethods.WindowMessage.MiddleButtonDown:
[图片]                case NativeMethods.WindowMessage.MiddleButtonDoubleClick:
[图片]                case NativeMethods.WindowMessage.XButtonUp:
[图片]                case NativeMethods.WindowMessage.XButtonDown:
[图片]                case NativeMethods.WindowMessage.XButtonDoubleClick:
[图片][图片]                [图片]{
[图片]                    // If not accepting mouse input, return false to indicate the message should still 
[图片]                    // be handled by the application (usually to move the camera).
[图片]                    if (!usingMouseInput)
[图片]                        return false;
[图片]
[图片]                    // Current mouse position
[图片]                    short mouseX = NativeMethods.LoWord((uint)lParam.ToInt32());
[图片]                    short mouseY = NativeMethods.HiWord((uint)lParam.ToInt32());
[图片]                    System.Drawing.Point mousePoint = new System.Drawing.Point(mouseX, mouseY);
[图片]                    // Offset mouse point
[图片]                    mousePoint.X -= dialogX;
[图片]                    mousePoint.Y -= dialogY;
[图片]
[图片]                    // If caption is enabled, offset the Y coordinate by the negative of its height.
[图片]                    if (hasCaption)
[图片]                        mousePoint.Y -= captionHeight;
[图片]
[图片]                    // If a control is in focus, it belongs to this dialog, and it's enabled, then give
[图片]                    // it the first chance at handling the message.
[图片]                    if (controlFocus != null && 
[图片]                        controlFocus.Parent == this && 
[图片]                        controlFocus.IsEnabled)
[图片][图片]                    [图片]{
[图片]                        // If the control MsgProc handles it, then we don't.
[图片]                        if (controlFocus.HandleMouse(msg, mousePoint, wParam, lParam))
[图片]                            return true;
[图片]                    }
[图片]
[图片]                    // Not yet handled, see if the mouse is over any controls
[图片]                    Control control = GetControlAtPoint(mousePoint);
[图片]                    if ((control != null) && (control.IsEnabled))
[图片][图片]                    [图片]{
[图片]                        // Let the control handle the mouse if it wants (and return true if it handles it)
[图片]                        if (control.HandleMouse(msg, mousePoint, wParam, lParam))
[图片]                            return true;
[图片]                    }
[图片]                    else
[图片][图片]                    [图片]{
[图片]                        // Mouse not over any controls in this dialog, if there was a control
[图片]                        // which had focus it just lost it
[图片]                        if (msg == NativeMethods.WindowMessage.LeftButtonDown &&
[图片]                            controlFocus != null &&
[图片]                            controlFocus.Parent == this)
[图片][图片]                        [图片]{
[图片]                            controlFocus.OnFocusOut();
[图片]                            controlFocus = null;
[图片]                        }
[图片]                    }


查看评论 | 添加评论
返回顶部 | 返回首页