DockPanel.MdiClientController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DockPanel.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.ComponentModel.Design;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  16. {
  17. /// <summary>
  18. /// 类文件
  19. /// </summary>
  20. partial class DockPanel
  21. {
  22. // This class comes from Jacob Slusser's MdiClientController class:
  23. // http://www.codeproject.com/cs/miscctrl/mdiclientcontroller.asp
  24. private class MdiClientController : NativeWindow, IComponent, IDisposable
  25. {
  26. private bool m_autoScroll = true;
  27. private BorderStyle m_borderStyle = BorderStyle.Fixed3D;
  28. private MdiClient m_mdiClient = null;
  29. private Form m_parentForm = null;
  30. private ISite m_site = null;
  31. public MdiClientController()
  32. {
  33. }
  34. public void Dispose()
  35. {
  36. Dispose(true);
  37. GC.SuppressFinalize(this);
  38. }
  39. protected virtual void Dispose(bool disposing)
  40. {
  41. if (disposing)
  42. {
  43. lock (this)
  44. {
  45. if (Site != null && Site.Container != null)
  46. Site.Container.Remove(this);
  47. if (Disposed != null)
  48. Disposed(this, EventArgs.Empty);
  49. }
  50. }
  51. }
  52. public bool AutoScroll
  53. {
  54. get
  55. {
  56. return m_autoScroll;
  57. }
  58. set
  59. {
  60. // By default the MdiClient control scrolls. It can appear though that
  61. // there are no scrollbars by turning them off when the non-client
  62. // area is calculated. I decided to expose this method following
  63. // the .NET vernacular of an AutoScroll property.
  64. m_autoScroll = value;
  65. if (MdiClient != null)
  66. UpdateStyles();
  67. }
  68. }
  69. public BorderStyle BorderStyle
  70. {
  71. set
  72. {
  73. // Error-check the enum.
  74. if (!Enum.IsDefined(typeof(BorderStyle), value))
  75. throw new InvalidEnumArgumentException();
  76. m_borderStyle = value;
  77. if (MdiClient == null)
  78. return;
  79. // This property can actually be visible in design-mode,
  80. // but to keep it consistent with the others,
  81. // prevent this from being show at design-time.
  82. if (Site != null && Site.DesignMode)
  83. return;
  84. // There is no BorderStyle property exposed by the MdiClient class,
  85. // but this can be controlled by Win32 functions. A Win32 ExStyle
  86. // of WS_EX_CLIENTEDGE is equivalent to a Fixed3D border and a
  87. // Style of WS_BORDER is equivalent to a FixedSingle border.
  88. // This code is inspired Jason Dori's article:
  89. // "Adding designable borders to user controls".
  90. // http://www.codeproject.com/cs/miscctrl/CsAddingBorders.asp
  91. if (!Win32Helper.IsRunningOnMono)
  92. {
  93. // Get styles using Win32 calls
  94. int style = NativeMethods.GetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_STYLE);
  95. int exStyle = NativeMethods.GetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_EXSTYLE);
  96. // Add or remove style flags as necessary.
  97. switch (m_borderStyle)
  98. {
  99. case BorderStyle.Fixed3D:
  100. exStyle |= (int)Win32.WindowExStyles.WS_EX_CLIENTEDGE;
  101. style &= ~((int)Win32.WindowStyles.WS_BORDER);
  102. break;
  103. case BorderStyle.FixedSingle:
  104. exStyle &= ~((int)Win32.WindowExStyles.WS_EX_CLIENTEDGE);
  105. style |= (int)Win32.WindowStyles.WS_BORDER;
  106. break;
  107. case BorderStyle.None:
  108. style &= ~((int)Win32.WindowStyles.WS_BORDER);
  109. exStyle &= ~((int)Win32.WindowExStyles.WS_EX_CLIENTEDGE);
  110. break;
  111. }
  112. // Set the styles using Win32 calls
  113. NativeMethods.SetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_STYLE, style);
  114. NativeMethods.SetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_EXSTYLE, exStyle);
  115. }
  116. // Cause an update of the non-client area.
  117. UpdateStyles();
  118. }
  119. }
  120. public MdiClient MdiClient
  121. {
  122. get
  123. {
  124. return m_mdiClient;
  125. }
  126. }
  127. [Browsable(false)]
  128. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  129. public Form ParentForm
  130. {
  131. get
  132. {
  133. return m_parentForm;
  134. }
  135. set
  136. {
  137. // If the ParentForm has previously been set,
  138. // unwire events connected to the old parent.
  139. if (m_parentForm != null)
  140. {
  141. m_parentForm.HandleCreated -= new EventHandler(ParentFormHandleCreated);
  142. m_parentForm.MdiChildActivate -= new EventHandler(ParentFormMdiChildActivate);
  143. }
  144. m_parentForm = value;
  145. if (m_parentForm == null)
  146. return;
  147. // If the parent form has not been created yet,
  148. // wait to initialize the MDI client until it is.
  149. if (m_parentForm.IsHandleCreated)
  150. {
  151. InitializeMdiClient();
  152. RefreshProperties();
  153. }
  154. else
  155. m_parentForm.HandleCreated += new EventHandler(ParentFormHandleCreated);
  156. m_parentForm.MdiChildActivate += new EventHandler(ParentFormMdiChildActivate);
  157. }
  158. }
  159. public ISite Site
  160. {
  161. get
  162. {
  163. return m_site;
  164. }
  165. set
  166. {
  167. m_site = value;
  168. if (m_site == null)
  169. return;
  170. // If the component is dropped onto a form during design-time,
  171. // set the ParentForm property.
  172. IDesignerHost host = (value.GetService(typeof(IDesignerHost)) as IDesignerHost);
  173. if (host != null)
  174. {
  175. Form parent = host.RootComponent as Form;
  176. if (parent != null)
  177. ParentForm = parent;
  178. }
  179. }
  180. }
  181. public void RenewMdiClient()
  182. {
  183. // Reinitialize the MdiClient and its properties.
  184. InitializeMdiClient();
  185. RefreshProperties();
  186. }
  187. public event EventHandler Disposed;
  188. public event EventHandler HandleAssigned;
  189. public event EventHandler MdiChildActivate;
  190. public event LayoutEventHandler Layout;
  191. protected virtual void OnHandleAssigned(EventArgs e)
  192. {
  193. // Raise the HandleAssigned event.
  194. if (HandleAssigned != null)
  195. HandleAssigned(this, e);
  196. }
  197. protected virtual void OnMdiChildActivate(EventArgs e)
  198. {
  199. // Raise the MdiChildActivate event
  200. if (MdiChildActivate != null)
  201. MdiChildActivate(this, e);
  202. }
  203. protected virtual void OnLayout(LayoutEventArgs e)
  204. {
  205. // Raise the Layout event
  206. if (Layout != null)
  207. Layout(this, e);
  208. }
  209. public event PaintEventHandler Paint;
  210. protected virtual void OnPaint(PaintEventArgs e)
  211. {
  212. // Raise the Paint event.
  213. if (Paint != null)
  214. Paint(this, e);
  215. }
  216. protected override void WndProc(ref Message m)
  217. {
  218. switch (m.Msg)
  219. {
  220. case (int)Win32.Msgs.WM_NCCALCSIZE:
  221. // If AutoScroll is set to false, hide the scrollbars when the control
  222. // calculates its non-client area.
  223. if (!AutoScroll)
  224. if (!Win32Helper.IsRunningOnMono)
  225. NativeMethods.ShowScrollBar(m.HWnd, (int)Win32.ScrollBars.SB_BOTH, 0 /*false*/);
  226. break;
  227. }
  228. base.WndProc(ref m);
  229. }
  230. private void ParentFormHandleCreated(object sender, EventArgs e)
  231. {
  232. // The form has been created, unwire the event, and initialize the MdiClient.
  233. this.m_parentForm.HandleCreated -= new EventHandler(ParentFormHandleCreated);
  234. InitializeMdiClient();
  235. RefreshProperties();
  236. }
  237. private void ParentFormMdiChildActivate(object sender, EventArgs e)
  238. {
  239. OnMdiChildActivate(e);
  240. }
  241. private void MdiClientLayout(object sender, LayoutEventArgs e)
  242. {
  243. OnLayout(e);
  244. }
  245. private void MdiClientHandleDestroyed(object sender, EventArgs e)
  246. {
  247. // If the MdiClient handle has been released, drop the reference and
  248. // release the handle.
  249. if (m_mdiClient != null)
  250. {
  251. m_mdiClient.HandleDestroyed -= new EventHandler(MdiClientHandleDestroyed);
  252. m_mdiClient = null;
  253. }
  254. ReleaseHandle();
  255. }
  256. private void InitializeMdiClient()
  257. {
  258. // If the mdiClient has previously been set, unwire events connected
  259. // to the old MDI.
  260. if (MdiClient != null)
  261. {
  262. MdiClient.HandleDestroyed -= new EventHandler(MdiClientHandleDestroyed);
  263. MdiClient.Layout -= new LayoutEventHandler(MdiClientLayout);
  264. }
  265. if (ParentForm == null)
  266. return;
  267. // Get the MdiClient from the parent form.
  268. foreach (Control control in ParentForm.Controls)
  269. {
  270. // If the form is an MDI container, it will contain an MdiClient control
  271. // just as it would any other control.
  272. m_mdiClient = control as MdiClient;
  273. if (m_mdiClient == null)
  274. continue;
  275. // Assign the MdiClient Handle to the NativeWindow.
  276. ReleaseHandle();
  277. AssignHandle(MdiClient.Handle);
  278. // Raise the HandleAssigned event.
  279. OnHandleAssigned(EventArgs.Empty);
  280. // Monitor the MdiClient for when its handle is destroyed.
  281. MdiClient.HandleDestroyed += new EventHandler(MdiClientHandleDestroyed);
  282. MdiClient.Layout += new LayoutEventHandler(MdiClientLayout);
  283. break;
  284. }
  285. }
  286. private void RefreshProperties()
  287. {
  288. // Refresh all the properties
  289. BorderStyle = m_borderStyle;
  290. AutoScroll = m_autoScroll;
  291. }
  292. private void UpdateStyles()
  293. {
  294. // To show style changes, the non-client area must be repainted. Using the
  295. // control's Invalidate method does not affect the non-client area.
  296. // Instead use a Win32 call to signal the style has changed.
  297. if (!Win32Helper.IsRunningOnMono)
  298. NativeMethods.SetWindowPos(MdiClient.Handle, IntPtr.Zero, 0, 0, 0, 0,
  299. Win32.FlagsSetWindowPos.SWP_NOACTIVATE |
  300. Win32.FlagsSetWindowPos.SWP_NOMOVE |
  301. Win32.FlagsSetWindowPos.SWP_NOSIZE |
  302. Win32.FlagsSetWindowPos.SWP_NOZORDER |
  303. Win32.FlagsSetWindowPos.SWP_NOOWNERZORDER |
  304. Win32.FlagsSetWindowPos.SWP_FRAMECHANGED);
  305. }
  306. }
  307. private MdiClientController m_mdiClientController = null;
  308. private MdiClientController GetMdiClientController()
  309. {
  310. if (m_mdiClientController == null)
  311. {
  312. m_mdiClientController = new MdiClientController();
  313. m_mdiClientController.HandleAssigned += new EventHandler(MdiClientHandleAssigned);
  314. m_mdiClientController.MdiChildActivate += new EventHandler(ParentFormMdiChildActivate);
  315. m_mdiClientController.Layout += new LayoutEventHandler(MdiClient_Layout);
  316. }
  317. return m_mdiClientController;
  318. }
  319. private void ParentFormMdiChildActivate(object sender, EventArgs e)
  320. {
  321. if (GetMdiClientController().ParentForm == null)
  322. return;
  323. IDockContent content = GetMdiClientController().ParentForm.ActiveMdiChild as IDockContent;
  324. if (content == null)
  325. return;
  326. if (content.DockHandler.DockPanel == this && content.DockHandler.Pane != null)
  327. content.DockHandler.Pane.ActiveContent = content;
  328. }
  329. private bool MdiClientExists
  330. {
  331. get
  332. {
  333. return GetMdiClientController().MdiClient != null;
  334. }
  335. }
  336. private void SetMdiClientBounds(Rectangle bounds)
  337. {
  338. GetMdiClientController().MdiClient.Bounds = bounds;
  339. }
  340. private void SuspendMdiClientLayout()
  341. {
  342. if (GetMdiClientController().MdiClient != null)
  343. GetMdiClientController().MdiClient.SuspendLayout();
  344. }
  345. private void ResumeMdiClientLayout(bool perform)
  346. {
  347. if (GetMdiClientController().MdiClient != null)
  348. GetMdiClientController().MdiClient.ResumeLayout(perform);
  349. }
  350. private void PerformMdiClientLayout()
  351. {
  352. if (GetMdiClientController().MdiClient != null)
  353. GetMdiClientController().MdiClient.PerformLayout();
  354. }
  355. // Called when:
  356. // 1. DockPanel.DocumentStyle changed
  357. // 2. DockPanel.Visible changed
  358. // 3. MdiClientController.Handle assigned
  359. private void SetMdiClient()
  360. {
  361. MdiClientController controller = GetMdiClientController();
  362. if (this.DocumentStyle == DocumentStyle.DockingMdi)
  363. {
  364. controller.AutoScroll = false;
  365. controller.BorderStyle = BorderStyle.None;
  366. if (MdiClientExists)
  367. controller.MdiClient.Dock = DockStyle.Fill;
  368. }
  369. else if (DocumentStyle == DocumentStyle.DockingSdi || DocumentStyle == DocumentStyle.DockingWindow)
  370. {
  371. controller.AutoScroll = true;
  372. controller.BorderStyle = BorderStyle.Fixed3D;
  373. if (MdiClientExists)
  374. controller.MdiClient.Dock = DockStyle.Fill;
  375. }
  376. else if (this.DocumentStyle == DocumentStyle.SystemMdi)
  377. {
  378. controller.AutoScroll = true;
  379. controller.BorderStyle = BorderStyle.Fixed3D;
  380. if (controller.MdiClient != null)
  381. {
  382. controller.MdiClient.Dock = DockStyle.None;
  383. controller.MdiClient.Bounds = SystemMdiClientBounds;
  384. }
  385. }
  386. }
  387. internal Rectangle RectangleToMdiClient(Rectangle rect)
  388. {
  389. if (MdiClientExists)
  390. return GetMdiClientController().MdiClient.RectangleToClient(rect);
  391. else
  392. return Rectangle.Empty;
  393. }
  394. }
  395. }