UIControl.8.Util.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. using Microsoft.Drawing;
  4. namespace Microsoft.Windows.Forms
  5. {
  6. partial class UIControl
  7. {
  8. /// <summary>
  9. /// 对指定控件的所有子控件重新计算布局
  10. /// </summary>
  11. /// <param name="control">指定控件</param>
  12. internal static void DoLayoutInternal(IUIControl control)
  13. {
  14. if (control.UIControls.Count <= 0)
  15. return;
  16. control.BeginUpdate();
  17. try
  18. {
  19. Rectangle rcClient = control.ClientRectangle;
  20. Rectangle rcDock = rcClient;
  21. foreach (UIControl child in control.UIControls)
  22. {
  23. try
  24. {
  25. //停靠
  26. switch (child.Dock)
  27. {
  28. case DockStyle.Left:
  29. child.m_PreferredX = rcDock.Left;
  30. child.m_PreferredY = rcDock.Top;
  31. child.m_PreferredHeight = rcDock.Height;
  32. child.SetBoundsCore();
  33. if (child.Visible)
  34. rcDock = RectangleEx.Subtract(rcDock, new Padding(child.m_PreferredWidth, 0, 0, 0));
  35. continue;
  36. case DockStyle.Top:
  37. child.m_PreferredX = rcDock.Left;
  38. child.m_PreferredY = rcDock.Top;
  39. child.m_PreferredWidth = rcDock.Width;
  40. child.SetBoundsCore();
  41. if (child.Visible)
  42. rcDock = RectangleEx.Subtract(rcDock, new Padding(0, child.m_PreferredHeight, 0, 0));
  43. continue;
  44. case DockStyle.Right:
  45. child.m_PreferredX = rcDock.Right - child.m_PreferredWidth;
  46. child.m_PreferredY = rcDock.Top;
  47. child.m_PreferredHeight = rcDock.Height;
  48. child.SetBoundsCore();
  49. if (child.Visible)
  50. rcDock = RectangleEx.Subtract(rcDock, new Padding(0, 0, child.m_PreferredWidth, 0));
  51. continue;
  52. case DockStyle.Bottom:
  53. child.m_PreferredX = rcDock.Left;
  54. child.m_PreferredY = rcDock.Bottom - child.m_PreferredHeight;
  55. child.m_PreferredWidth = rcDock.Width;
  56. child.SetBoundsCore();
  57. if (child.Visible)
  58. rcDock = RectangleEx.Subtract(rcDock, new Padding(0, 0, 0, child.m_PreferredHeight));
  59. continue;
  60. case DockStyle.Fill:
  61. child.m_PreferredX = rcDock.Left;
  62. child.m_PreferredY = rcDock.Top;
  63. child.m_PreferredWidth = rcDock.Width;
  64. child.m_PreferredHeight = rcDock.Height;
  65. child.SetBoundsCore();
  66. if (child.Visible)
  67. rcDock = control.ClientRectangle; //下一轮重新布局
  68. continue;
  69. }
  70. //锚定左右
  71. if (child.m_LeftToParent == null || child.m_RightToParent == null)
  72. {
  73. child.m_LeftToParent = child.m_PreferredX;
  74. child.m_RightToParent = rcClient.Width - child.m_PreferredX - child.m_PreferredWidth;
  75. }
  76. else
  77. {
  78. switch (child.Anchor & (AnchorStyles.Left | AnchorStyles.Right))
  79. {
  80. case AnchorStyles.Left:
  81. child.m_PreferredX = child.m_LeftToParent.Value;
  82. break;
  83. case AnchorStyles.Right:
  84. child.m_PreferredX = rcClient.Width - child.m_RightToParent.Value - child.m_PreferredWidth;
  85. break;
  86. case AnchorStyles.Left | AnchorStyles.Right:
  87. child.m_PreferredX = child.m_LeftToParent.Value;
  88. child.m_PreferredWidth = rcClient.Width - child.m_LeftToParent.Value - child.m_RightToParent.Value;
  89. break;
  90. default:
  91. child.m_PreferredX = child.m_LeftToParent.Value + (rcClient.Width - child.m_LeftToParent.Value - child.m_PreferredWidth - child.m_RightToParent.Value) / 2;
  92. break;
  93. }
  94. }
  95. //锚定上下
  96. if (child.m_TopToParent == null || child.m_BottomToParent == null)
  97. {
  98. child.m_TopToParent = child.m_PreferredY;
  99. child.m_BottomToParent = rcClient.Height - child.m_PreferredY - child.m_PreferredHeight;
  100. }
  101. else
  102. {
  103. switch (child.Anchor & (AnchorStyles.Top | AnchorStyles.Bottom))
  104. {
  105. case AnchorStyles.Top:
  106. child.m_PreferredY = child.m_TopToParent.Value;
  107. break;
  108. case AnchorStyles.Bottom:
  109. child.m_PreferredY = rcClient.Height - child.m_BottomToParent.Value - child.m_PreferredHeight;
  110. break;
  111. case AnchorStyles.Top | AnchorStyles.Bottom:
  112. child.m_PreferredY = child.m_TopToParent.Value;
  113. child.m_PreferredHeight = rcClient.Height - child.m_TopToParent.Value - child.m_BottomToParent.Value;
  114. break;
  115. default:
  116. child.m_PreferredY = child.m_TopToParent.Value + (rcClient.Height - child.m_TopToParent.Value - child.m_PreferredHeight - child.m_BottomToParent.Value) / 2;
  117. break;
  118. }
  119. }
  120. child.SetBoundsCore();
  121. }
  122. finally
  123. {
  124. DoLayoutInternal(child);
  125. }
  126. }
  127. }
  128. finally
  129. {
  130. control.EndUpdate();
  131. }
  132. }
  133. /// <summary>
  134. /// 查找控件内指定坐标的子控件,按 Z 轴可见序查找
  135. /// </summary>
  136. /// <param name="control">控件</param>
  137. /// <param name="p">控件坐标系内的点</param>
  138. /// <returns>查找到返回子控件,否则返回 null</returns>
  139. internal static UIControl FindUIChildInternal(IUIControl control, Point p)
  140. {
  141. UIControl child;
  142. for (int i = control.UIControls.Count - 1; i >= 0; i--)//按 Z 轴顺序遍历
  143. {
  144. child = control.UIControls[i];
  145. if (child.Visible && child.Bounds.Contains(p) && (child.Region == null || child.Region.IsVisible(PointEx.Offset(p, -child.Left, -child.Top))))
  146. return FindUIChildInternal(child, PointEx.Offset(p, -child.Left, -child.Top)) ?? child;
  147. }
  148. return null;
  149. }
  150. /// <summary>
  151. /// 查找控件内指定名称的控件,按 Z 轴可见序查找
  152. /// </summary>
  153. /// <param name="control">控件</param>
  154. /// <param name="name">名称</param>
  155. /// <returns>查找到返回子控件,否则返回 null</returns>
  156. internal static UIControl FindUIChildInternal(IUIControl control, string name)
  157. {
  158. UIControl child;
  159. for (int i = control.UIControls.Count - 1; i >= 0; i--)
  160. {
  161. child = control.UIControls[i];
  162. if (child.Name == name)
  163. return child;
  164. else if (child.UIControls.Count > 0)
  165. return FindUIChildInternal(child, name);
  166. }
  167. return null;
  168. }
  169. }
  170. }