UIForm.5.WndProc.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Windows.Forms;
  2. using Microsoft.Win32;
  3. namespace Microsoft.Windows.Forms
  4. {
  5. partial class UIForm
  6. {
  7. private UIControl m_CaptureControl;
  8. /// <summary>
  9. /// 获取捕获鼠标消息的虚拟控件
  10. /// </summary>
  11. protected UIControl CaptureControl
  12. {
  13. get
  14. {
  15. return m_CaptureControl;
  16. }
  17. set
  18. {
  19. if (value != this.m_CaptureControl)
  20. {
  21. if (this.m_CaptureControl != null)
  22. this.m_CaptureControl.Capture = false;
  23. this.m_CaptureControl = value;
  24. if (this.m_CaptureControl != null)
  25. this.m_CaptureControl.Capture = true;
  26. }
  27. }
  28. }
  29. /// <summary>
  30. /// 消息处理
  31. /// </summary>
  32. /// <param name="m">消息</param>
  33. protected override void WndProc(ref Message m)
  34. {
  35. this.DispatchMessage(ref m);
  36. if (m.Result == NativeMethods.TRUE)
  37. return;
  38. base.WndProc(ref m);
  39. }
  40. /// <summary>
  41. /// 分派消息
  42. /// </summary>
  43. /// <param name="m">消息</param>
  44. protected virtual void DispatchMessage(ref Message m)
  45. {
  46. switch (m.Msg)
  47. {
  48. //左键按下
  49. case NativeMethods.WM_LBUTTONDOWN:
  50. case NativeMethods.WM_LBUTTONDBLCLK:
  51. this.WmLButtonDown(ref m);
  52. break;
  53. //左键弹起
  54. case NativeMethods.WM_LBUTTONUP:
  55. this.WmLButtonUp(ref m);
  56. break;
  57. //鼠标移动
  58. case NativeMethods.WM_MOUSEMOVE:
  59. this.WmMouseMove(ref m);
  60. break;
  61. //鼠标离开
  62. case NativeMethods.WM_MOUSELEAVE:
  63. this.WmMouseLeave(ref m);
  64. break;
  65. }
  66. }
  67. /// <summary>
  68. /// 处理鼠标左键按下消息
  69. /// </summary>
  70. /// <param name="m">消息</param>
  71. protected virtual void WmLButtonDown(ref Message m)
  72. {
  73. UIControl control = this.FindUIChild(Util.GetMousePosition(m.LParam));
  74. this.CaptureControl = control = (control != null && control.Enabled) ? control : null;
  75. if (control != null)
  76. {
  77. control.WndProc(ref m);
  78. m.Result = NativeMethods.TRUE;
  79. }
  80. }
  81. /// <summary>
  82. /// 处理鼠标左键弹起消息
  83. /// </summary>
  84. /// <param name="m">消息</param>
  85. protected virtual void WmLButtonUp(ref Message m)
  86. {
  87. UIControl lastAccess = this.CaptureControl;
  88. UIControl control = this.FindUIChild(Util.GetMousePosition(m.LParam));
  89. this.CaptureControl = control = (control != null && control.Enabled) ? control : null;
  90. if (control == lastAccess && control != null)
  91. {
  92. control.WndProc(ref m);
  93. m.Result = NativeMethods.TRUE;
  94. }
  95. }
  96. /// <summary>
  97. /// 处理鼠标移动消息
  98. /// </summary>
  99. /// <param name="m">消息</param>
  100. protected virtual void WmMouseMove(ref Message m)
  101. {
  102. if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.None)//未按下左键
  103. {
  104. this.WmLButtonDown(ref m);
  105. }
  106. else//左键按下
  107. {
  108. UIControl lastAccess = this.CaptureControl;
  109. if (lastAccess != null)
  110. {
  111. lastAccess.WndProc(ref m);
  112. m.Result = NativeMethods.TRUE;
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 处理鼠标移开消息
  118. /// </summary>
  119. /// <param name="m">消息</param>
  120. protected virtual void WmMouseLeave(ref Message m)
  121. {
  122. this.CaptureControl = null;
  123. }
  124. }
  125. }