UIControl.1.Layout.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace Microsoft.Windows.Forms
  5. {
  6. partial class UIControl
  7. {
  8. private IUIControl m_UIParent;
  9. private IUIControl UIParentInternal
  10. {
  11. get
  12. {
  13. return this.m_UIParent;
  14. }
  15. set
  16. {
  17. if (value != this.m_UIParent)
  18. {
  19. this.m_UIParent = value;
  20. this.OnParentChanged(EventArgs.Empty);
  21. }
  22. }
  23. }
  24. /// <summary>
  25. /// 获取或设置父控件
  26. /// </summary>
  27. public IUIControl UIParent
  28. {
  29. get
  30. {
  31. return this.m_UIParent;
  32. }
  33. set
  34. {
  35. if (value != this.m_UIParent)
  36. {
  37. if (value != null)
  38. value.UIControls.Add(this);
  39. else
  40. this.m_UIParent.UIControls.Remove(this);
  41. }
  42. }
  43. }
  44. private UIControlCollection m_UIControls;
  45. /// <summary>
  46. /// 获取子控件集合
  47. /// </summary>
  48. public UIControlCollection UIControls
  49. {
  50. get
  51. {
  52. this.CheckDisposed();
  53. if (this.m_UIControls == null)
  54. this.m_UIControls = new UIControlCollection(this);
  55. return this.m_UIControls;
  56. }
  57. }
  58. private Region m_Region;
  59. /// <summary>
  60. /// 获取或设置控件区域
  61. /// </summary>
  62. public Region Region
  63. {
  64. get
  65. {
  66. return this.m_Region;
  67. }
  68. set
  69. {
  70. if (this.m_Region != null)
  71. this.m_Region.Dispose();
  72. this.m_Region = value == null ? null : value.Clone();
  73. }
  74. }
  75. private DockStyle m_Dock;
  76. /// <summary>
  77. /// 获取或设置停靠方式
  78. /// </summary>
  79. public DockStyle Dock
  80. {
  81. get
  82. {
  83. return this.m_Dock;
  84. }
  85. set
  86. {
  87. if (value != this.m_Dock)
  88. {
  89. this.m_Dock = value;
  90. if (this.m_UIParent != null)
  91. this.m_UIParent.DoLayout();
  92. }
  93. }
  94. }
  95. private AnchorStyles m_Anchor = AnchorStyles.Left | AnchorStyles.Top;
  96. /// <summary>
  97. /// 获取或设置锚定方式
  98. /// </summary>
  99. public AnchorStyles Anchor
  100. {
  101. get
  102. {
  103. return this.m_Anchor;
  104. }
  105. set
  106. {
  107. if (value != this.m_Anchor)
  108. {
  109. this.m_Anchor = value;
  110. this.m_LeftToParent = null;
  111. this.m_TopToParent = null;
  112. this.m_RightToParent = null;
  113. this.m_BottomToParent = null;
  114. }
  115. }
  116. }
  117. //初始左边距
  118. private int? m_LeftToParent;
  119. //初始上边距
  120. private int? m_TopToParent;
  121. //初始右边距
  122. private int? m_RightToParent;
  123. //初始下边距
  124. private int? m_BottomToParent;
  125. private int m_PreferredX;
  126. private int m_X;
  127. /// <summary>
  128. /// 获取或设置距离父控件的左边距
  129. /// </summary>
  130. public int Left
  131. {
  132. get
  133. {
  134. return this.m_X;
  135. }
  136. set
  137. {
  138. if (value != this.m_X)
  139. {
  140. this.m_PreferredX = value;
  141. this.SetBounds();
  142. }
  143. }
  144. }
  145. private int m_PreferredY;
  146. private int m_Y;
  147. /// <summary>
  148. /// 获取或设置距离父控件的上边距
  149. /// </summary>
  150. public int Top
  151. {
  152. get
  153. {
  154. return this.m_Y;
  155. }
  156. set
  157. {
  158. if (value != this.m_Y)
  159. {
  160. this.m_PreferredY = value;
  161. this.SetBounds();
  162. }
  163. }
  164. }
  165. private int m_PreferredWidth;
  166. private int m_Width;
  167. /// <summary>
  168. /// 获取或设置宽度
  169. /// </summary>
  170. public int Width
  171. {
  172. get
  173. {
  174. return this.m_Width;
  175. }
  176. set
  177. {
  178. if (value != this.m_Width)
  179. {
  180. this.m_PreferredWidth = value;
  181. this.SetBounds();
  182. }
  183. }
  184. }
  185. private int m_PreferredHeight;
  186. private int m_Height;
  187. /// <summary>
  188. /// 获取或设置高度
  189. /// </summary>
  190. public int Height
  191. {
  192. get
  193. {
  194. return this.m_Height;
  195. }
  196. set
  197. {
  198. if (value != this.m_Height)
  199. {
  200. this.m_PreferredHeight = value;
  201. this.SetBounds();
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 获取距离父控件的右边距
  207. /// </summary>
  208. public int Right
  209. {
  210. get
  211. {
  212. return this.m_X + this.m_Width;
  213. }
  214. }
  215. /// <summary>
  216. /// 获取距离父控件的下边距
  217. /// </summary>
  218. public int Bottom
  219. {
  220. get
  221. {
  222. return this.m_Y + this.m_Height;
  223. }
  224. }
  225. /// <summary>
  226. /// 获取或设置控件左上角的坐标
  227. /// </summary>
  228. public Point Location
  229. {
  230. get
  231. {
  232. return new Point(this.m_X, this.m_Y);
  233. }
  234. set
  235. {
  236. if (value != this.Location)
  237. {
  238. this.m_PreferredX = value.X;
  239. this.m_PreferredY = value.Y;
  240. this.SetBounds();
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 获取或设置控件的大小
  246. /// </summary>
  247. public Size Size
  248. {
  249. get
  250. {
  251. return new Size(this.m_Width, this.m_Height);
  252. }
  253. set
  254. {
  255. if (value != this.Size)
  256. {
  257. this.m_PreferredWidth = value.Width;
  258. this.m_PreferredHeight = value.Height;
  259. this.SetBounds();
  260. }
  261. }
  262. }
  263. /// <summary>
  264. /// 获取或设置控件的位置和大小
  265. /// </summary>
  266. public Rectangle Bounds
  267. {
  268. get
  269. {
  270. return new Rectangle(this.m_X, this.m_Y, this.m_Width, this.m_Height);
  271. }
  272. set
  273. {
  274. if (value != this.Bounds)
  275. {
  276. this.m_PreferredX = value.X;
  277. this.m_PreferredY = value.Y;
  278. this.m_PreferredWidth = value.Width;
  279. this.m_PreferredHeight = value.Height;
  280. this.SetBounds();
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// 获取控件客户区大小
  286. /// </summary>
  287. public Size ClientSize
  288. {
  289. get
  290. {
  291. return this.Size;
  292. }
  293. set
  294. {
  295. this.Size = value;
  296. }
  297. }
  298. /// <summary>
  299. /// 获取控件客户区的位置和大小
  300. /// </summary>
  301. public Rectangle ClientRectangle
  302. {
  303. get
  304. {
  305. return new Rectangle(0, 0, this.m_Width, this.m_Height);
  306. }
  307. }
  308. private Padding m_Padding;
  309. /// <summary>
  310. /// 获取或设置内边距
  311. /// </summary>
  312. public Padding Padding
  313. {
  314. get
  315. {
  316. return this.m_Padding;
  317. }
  318. set
  319. {
  320. if (value != this.m_Padding)
  321. {
  322. this.m_Padding = value;
  323. this.Invalidate();
  324. }
  325. }
  326. }
  327. private int m_LayoutSuspendCount;
  328. /// <summary>
  329. /// 获取布局操作是否被挂起
  330. /// </summary>
  331. public bool LayoutSuspended
  332. {
  333. get
  334. {
  335. return this.m_LayoutSuspendCount != 0 || (this.m_UIParent != null && this.m_UIParent.LayoutSuspended);
  336. }
  337. }
  338. /// <summary>
  339. /// 设置控件位置和大小,先计算后设置
  340. /// </summary>
  341. private void SetBounds()
  342. {
  343. if (this.m_UIParent == null || this.Dock == DockStyle.None)
  344. {
  345. this.SetBoundsCore();
  346. this.DoLayout();
  347. }
  348. else
  349. {
  350. this.m_UIParent.DoLayout();
  351. }
  352. }
  353. /// <summary>
  354. /// 设置控件位置和大小,直接设置
  355. /// </summary>
  356. private void SetBoundsCore()
  357. {
  358. if (this.m_PreferredX != this.m_X || this.m_PreferredY != this.m_Y)
  359. {
  360. this.m_X = this.m_PreferredX;
  361. this.m_Y = this.m_PreferredY;
  362. if (this.m_UIParent != null)
  363. this.m_UIParent.Invalidate();
  364. this.OnLocationChanged(EventArgs.Empty);
  365. }
  366. if (this.m_PreferredWidth != this.m_Width || this.m_PreferredHeight != this.m_Height)
  367. {
  368. this.m_Width = this.m_PreferredWidth;
  369. this.m_Height = this.m_PreferredHeight;
  370. this.Invalidate();
  371. this.OnSizeChanged(EventArgs.Empty);
  372. }
  373. }
  374. /// <summary>
  375. /// 挂起布局操作
  376. /// </summary>
  377. public void SuspendLayout()
  378. {
  379. this.m_LayoutSuspendCount++;
  380. }
  381. /// <summary>
  382. /// 恢复挂起的布局操作
  383. /// </summary>
  384. public void ResumeLayout()
  385. {
  386. this.ResumeLayout(false);
  387. }
  388. /// <summary>
  389. /// 恢复挂起的布局操作,可选择是否强制执行布局
  390. /// </summary>
  391. /// <param name="performLayout">如果强制则为 true, 否则为 false</param>
  392. public void ResumeLayout(bool performLayout)
  393. {
  394. this.m_LayoutSuspendCount--;
  395. this.DoLayoutCore(performLayout);
  396. }
  397. /// <summary>
  398. /// 如果未挂起布局操作则重新计算子控件布局
  399. /// </summary>
  400. public void DoLayout()
  401. {
  402. this.DoLayoutCore(false);
  403. }
  404. /// <summary>
  405. /// 重新计算子控件布局,可选择是否强制执行布局
  406. /// </summary>
  407. /// <param name="performLayout">如果强制则为 true, 否则为 false</param>
  408. protected void DoLayoutCore(bool performLayout)
  409. {
  410. if (performLayout || !this.LayoutSuspended)
  411. DoLayoutInternal(this);
  412. }
  413. /// <summary>
  414. /// 将控件置于 Z 轴底层
  415. /// </summary>
  416. public void SendToBack()
  417. {
  418. IUIControl parent = this.m_UIParent;
  419. if (parent == null)
  420. return;
  421. parent.SuspendLayout();
  422. try
  423. {
  424. parent.UIControls.Remove(this);
  425. parent.UIControls.Insert(0, this);
  426. }
  427. finally
  428. {
  429. parent.ResumeLayout();
  430. }
  431. }
  432. /// <summary>
  433. /// 将控件置于 Z 轴顶层
  434. /// </summary>
  435. public void BringToFront()
  436. {
  437. IUIControl parent = this.m_UIParent;
  438. if (parent == null)
  439. return;
  440. parent.SuspendLayout();
  441. try
  442. {
  443. parent.UIControls.Remove(this);
  444. parent.UIControls.Add(this);
  445. }
  446. finally
  447. {
  448. parent.ResumeLayout();
  449. }
  450. }
  451. /// <summary>
  452. /// 将屏幕坐标系的点转换为控件坐标系的点
  453. /// </summary>
  454. /// <param name="p">点</param>
  455. /// <returns>转换后的点</returns>
  456. public Point PointToClient(Point p)
  457. {
  458. IUIControl parent = this;
  459. IUIWindow window = null;
  460. while (parent != null && (window = parent as IUIWindow) == null)
  461. {
  462. p.Offset(-parent.Left, -parent.Top);
  463. parent = parent.UIParent;
  464. }
  465. return window.PointToClient(p);
  466. }
  467. /// <summary>
  468. /// 将控件坐标系的点转换为屏幕坐标系的点
  469. /// </summary>
  470. /// <param name="p">点</param>
  471. /// <returns>转换后的点</returns>
  472. public Point PointToScreen(Point p)
  473. {
  474. IUIControl parent = this;
  475. IUIWindow window = null;
  476. while (parent != null && (window = parent as IUIWindow) == null)
  477. {
  478. p.Offset(parent.Left, parent.Top);
  479. parent = parent.UIParent;
  480. }
  481. return window.PointToScreen(p);
  482. }
  483. /// <summary>
  484. /// 将屏幕坐标系的矩形转换为控件坐标系的矩形
  485. /// </summary>
  486. /// <param name="r">矩形</param>
  487. /// <returns>转换后的矩形</returns>
  488. public Rectangle RectangleToClient(Rectangle r)
  489. {
  490. IUIControl parent = this;
  491. IUIWindow window = null;
  492. while (parent != null && (window = parent as IUIWindow) == null)
  493. {
  494. r.Offset(-parent.Left, -parent.Top);
  495. parent = parent.UIParent;
  496. }
  497. return window.RectangleToClient(r);
  498. }
  499. /// <summary>
  500. /// 将控件坐标系的矩形转换为屏幕坐标系的矩形
  501. /// </summary>
  502. /// <param name="r">矩形</param>
  503. /// <returns>转换后的矩形</returns>
  504. public Rectangle RectangleToScreen(Rectangle r)
  505. {
  506. IUIControl parent = this;
  507. IUIWindow window = null;
  508. while (parent != null && (window = parent as IUIWindow) == null)
  509. {
  510. r.Offset(parent.Left, parent.Top);
  511. parent = parent.UIParent;
  512. }
  513. return window.RectangleToScreen(r);
  514. }
  515. /// <summary>
  516. /// 将所在 Win32 控件坐标系的点转换为控件坐标系的点
  517. /// </summary>
  518. /// <param name="p">点</param>
  519. /// <returns>转换后的点</returns>
  520. public Point PointToUIControl(Point p)
  521. {
  522. IUIControl parent = this;
  523. IUIWindow window = null;
  524. while (parent != null && (window = parent as IUIWindow) == null)
  525. {
  526. p.Offset(-parent.Left, -parent.Top);
  527. parent = parent.UIParent;
  528. }
  529. return p;
  530. }
  531. /// <summary>
  532. /// 将控件坐标系的点转换为所在 Win32 控件坐标系的点
  533. /// </summary>
  534. /// <param name="p">点</param>
  535. /// <returns>转换后的点</returns>
  536. public Point PointToUIWindow(Point p)
  537. {
  538. IUIControl parent = this;
  539. IUIWindow window = null;
  540. while (parent != null && (window = parent as IUIWindow) == null)
  541. {
  542. p.Offset(parent.Left, parent.Top);
  543. parent = parent.UIParent;
  544. }
  545. return p;
  546. }
  547. /// <summary>
  548. /// 将所在 Win32 控件坐标系的矩形转换为控件坐标系的矩形
  549. /// </summary>
  550. /// <param name="r">矩形</param>
  551. /// <returns>转换后的矩形</returns>
  552. public Rectangle RectangleToUIControl(Rectangle r)
  553. {
  554. IUIControl parent = this;
  555. IUIWindow window = null;
  556. while (parent != null && (window = parent as IUIWindow) == null)
  557. {
  558. r.Offset(-parent.Left, -parent.Top);
  559. parent = parent.UIParent;
  560. }
  561. return r;
  562. }
  563. /// <summary>
  564. /// 将控件坐标系的矩形转换为所在 Win32 控件坐标系的矩形
  565. /// </summary>
  566. /// <param name="r">矩形</param>
  567. /// <returns>转换后的矩形</returns>
  568. public Rectangle RectangleToUIWindow(Rectangle r)
  569. {
  570. IUIControl parent = this;
  571. IUIWindow window = null;
  572. while (parent != null && (window = parent as IUIWindow) == null)
  573. {
  574. r.Offset(parent.Left, parent.Top);
  575. parent = parent.UIParent;
  576. }
  577. return r;
  578. }
  579. /// <summary>
  580. /// 查找所在的 Win32 控件
  581. /// </summary>
  582. /// <returns>所在的 Win32 控件</returns>
  583. public IUIWindow FindUIWindow()
  584. {
  585. IUIControl parent = this;
  586. IUIWindow window = null;
  587. while (parent != null && (window = parent as IUIWindow) == null)
  588. parent = parent.UIParent;
  589. return window;
  590. }
  591. /// <summary>
  592. /// 根据坐标查找子控件
  593. /// </summary>
  594. /// <param name="p">坐标</param>
  595. /// <returns>子控件</returns>
  596. public UIControl FindUIChild(Point p)
  597. {
  598. return FindUIChildInternal(this, p);
  599. }
  600. /// <summary>
  601. /// 根据名称查找子控件
  602. /// </summary>
  603. /// <param name="name">名称</param>
  604. /// <returns>子控件</returns>
  605. public UIControl FindUIChild(string name)
  606. {
  607. return FindUIChildInternal(this, name);
  608. }
  609. }
  610. }