| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:DockPanel.cs
- * 2.功能描述:类文件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/09/01 1.00 新建
- *******************************************************************************/
- using System;
- using System.ComponentModel;
- using System.ComponentModel.Design;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Dongke.IBOSS.PRD.Basics.DockPanel
- {
- /// <summary>
- /// 类文件
- /// </summary>
- partial class DockPanel
- {
- // This class comes from Jacob Slusser's MdiClientController class:
- // http://www.codeproject.com/cs/miscctrl/mdiclientcontroller.asp
- private class MdiClientController : NativeWindow, IComponent, IDisposable
- {
- private bool m_autoScroll = true;
- private BorderStyle m_borderStyle = BorderStyle.Fixed3D;
- private MdiClient m_mdiClient = null;
- private Form m_parentForm = null;
- private ISite m_site = null;
- public MdiClientController()
- {
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- lock (this)
- {
- if (Site != null && Site.Container != null)
- Site.Container.Remove(this);
- if (Disposed != null)
- Disposed(this, EventArgs.Empty);
- }
- }
- }
- public bool AutoScroll
- {
- get
- {
- return m_autoScroll;
- }
- set
- {
- // By default the MdiClient control scrolls. It can appear though that
- // there are no scrollbars by turning them off when the non-client
- // area is calculated. I decided to expose this method following
- // the .NET vernacular of an AutoScroll property.
- m_autoScroll = value;
- if (MdiClient != null)
- UpdateStyles();
- }
- }
- public BorderStyle BorderStyle
- {
- set
- {
- // Error-check the enum.
- if (!Enum.IsDefined(typeof(BorderStyle), value))
- throw new InvalidEnumArgumentException();
- m_borderStyle = value;
- if (MdiClient == null)
- return;
- // This property can actually be visible in design-mode,
- // but to keep it consistent with the others,
- // prevent this from being show at design-time.
- if (Site != null && Site.DesignMode)
- return;
- // There is no BorderStyle property exposed by the MdiClient class,
- // but this can be controlled by Win32 functions. A Win32 ExStyle
- // of WS_EX_CLIENTEDGE is equivalent to a Fixed3D border and a
- // Style of WS_BORDER is equivalent to a FixedSingle border.
- // This code is inspired Jason Dori's article:
- // "Adding designable borders to user controls".
- // http://www.codeproject.com/cs/miscctrl/CsAddingBorders.asp
- if (!Win32Helper.IsRunningOnMono)
- {
- // Get styles using Win32 calls
- int style = NativeMethods.GetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_STYLE);
- int exStyle = NativeMethods.GetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_EXSTYLE);
- // Add or remove style flags as necessary.
- switch (m_borderStyle)
- {
- case BorderStyle.Fixed3D:
- exStyle |= (int)Win32.WindowExStyles.WS_EX_CLIENTEDGE;
- style &= ~((int)Win32.WindowStyles.WS_BORDER);
- break;
- case BorderStyle.FixedSingle:
- exStyle &= ~((int)Win32.WindowExStyles.WS_EX_CLIENTEDGE);
- style |= (int)Win32.WindowStyles.WS_BORDER;
- break;
- case BorderStyle.None:
- style &= ~((int)Win32.WindowStyles.WS_BORDER);
- exStyle &= ~((int)Win32.WindowExStyles.WS_EX_CLIENTEDGE);
- break;
- }
- // Set the styles using Win32 calls
- NativeMethods.SetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_STYLE, style);
- NativeMethods.SetWindowLong(MdiClient.Handle, (int)Win32.GetWindowLongIndex.GWL_EXSTYLE, exStyle);
- }
- // Cause an update of the non-client area.
- UpdateStyles();
- }
- }
- public MdiClient MdiClient
- {
- get
- {
- return m_mdiClient;
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public Form ParentForm
- {
- get
- {
- return m_parentForm;
- }
- set
- {
- // If the ParentForm has previously been set,
- // unwire events connected to the old parent.
- if (m_parentForm != null)
- {
- m_parentForm.HandleCreated -= new EventHandler(ParentFormHandleCreated);
- m_parentForm.MdiChildActivate -= new EventHandler(ParentFormMdiChildActivate);
- }
- m_parentForm = value;
- if (m_parentForm == null)
- return;
- // If the parent form has not been created yet,
- // wait to initialize the MDI client until it is.
- if (m_parentForm.IsHandleCreated)
- {
- InitializeMdiClient();
- RefreshProperties();
- }
- else
- m_parentForm.HandleCreated += new EventHandler(ParentFormHandleCreated);
- m_parentForm.MdiChildActivate += new EventHandler(ParentFormMdiChildActivate);
- }
- }
- public ISite Site
- {
- get
- {
- return m_site;
- }
- set
- {
- m_site = value;
- if (m_site == null)
- return;
- // If the component is dropped onto a form during design-time,
- // set the ParentForm property.
- IDesignerHost host = (value.GetService(typeof(IDesignerHost)) as IDesignerHost);
- if (host != null)
- {
- Form parent = host.RootComponent as Form;
- if (parent != null)
- ParentForm = parent;
- }
- }
- }
- public void RenewMdiClient()
- {
- // Reinitialize the MdiClient and its properties.
- InitializeMdiClient();
- RefreshProperties();
- }
- public event EventHandler Disposed;
- public event EventHandler HandleAssigned;
- public event EventHandler MdiChildActivate;
- public event LayoutEventHandler Layout;
- protected virtual void OnHandleAssigned(EventArgs e)
- {
- // Raise the HandleAssigned event.
- if (HandleAssigned != null)
- HandleAssigned(this, e);
- }
- protected virtual void OnMdiChildActivate(EventArgs e)
- {
- // Raise the MdiChildActivate event
- if (MdiChildActivate != null)
- MdiChildActivate(this, e);
- }
- protected virtual void OnLayout(LayoutEventArgs e)
- {
- // Raise the Layout event
- if (Layout != null)
- Layout(this, e);
- }
- public event PaintEventHandler Paint;
- protected virtual void OnPaint(PaintEventArgs e)
- {
- // Raise the Paint event.
- if (Paint != null)
- Paint(this, e);
- }
- protected override void WndProc(ref Message m)
- {
- switch (m.Msg)
- {
- case (int)Win32.Msgs.WM_NCCALCSIZE:
- // If AutoScroll is set to false, hide the scrollbars when the control
- // calculates its non-client area.
- if (!AutoScroll)
- if (!Win32Helper.IsRunningOnMono)
- NativeMethods.ShowScrollBar(m.HWnd, (int)Win32.ScrollBars.SB_BOTH, 0 /*false*/);
- break;
- }
- base.WndProc(ref m);
- }
- private void ParentFormHandleCreated(object sender, EventArgs e)
- {
- // The form has been created, unwire the event, and initialize the MdiClient.
- this.m_parentForm.HandleCreated -= new EventHandler(ParentFormHandleCreated);
- InitializeMdiClient();
- RefreshProperties();
- }
- private void ParentFormMdiChildActivate(object sender, EventArgs e)
- {
- OnMdiChildActivate(e);
- }
- private void MdiClientLayout(object sender, LayoutEventArgs e)
- {
- OnLayout(e);
- }
- private void MdiClientHandleDestroyed(object sender, EventArgs e)
- {
- // If the MdiClient handle has been released, drop the reference and
- // release the handle.
- if (m_mdiClient != null)
- {
- m_mdiClient.HandleDestroyed -= new EventHandler(MdiClientHandleDestroyed);
- m_mdiClient = null;
- }
- ReleaseHandle();
- }
- private void InitializeMdiClient()
- {
- // If the mdiClient has previously been set, unwire events connected
- // to the old MDI.
- if (MdiClient != null)
- {
- MdiClient.HandleDestroyed -= new EventHandler(MdiClientHandleDestroyed);
- MdiClient.Layout -= new LayoutEventHandler(MdiClientLayout);
- }
- if (ParentForm == null)
- return;
- // Get the MdiClient from the parent form.
- foreach (Control control in ParentForm.Controls)
- {
- // If the form is an MDI container, it will contain an MdiClient control
- // just as it would any other control.
- m_mdiClient = control as MdiClient;
- if (m_mdiClient == null)
- continue;
- // Assign the MdiClient Handle to the NativeWindow.
- ReleaseHandle();
- AssignHandle(MdiClient.Handle);
- // Raise the HandleAssigned event.
- OnHandleAssigned(EventArgs.Empty);
- // Monitor the MdiClient for when its handle is destroyed.
- MdiClient.HandleDestroyed += new EventHandler(MdiClientHandleDestroyed);
- MdiClient.Layout += new LayoutEventHandler(MdiClientLayout);
- break;
- }
- }
- private void RefreshProperties()
- {
- // Refresh all the properties
- BorderStyle = m_borderStyle;
- AutoScroll = m_autoScroll;
- }
- private void UpdateStyles()
- {
- // To show style changes, the non-client area must be repainted. Using the
- // control's Invalidate method does not affect the non-client area.
- // Instead use a Win32 call to signal the style has changed.
- if (!Win32Helper.IsRunningOnMono)
- NativeMethods.SetWindowPos(MdiClient.Handle, IntPtr.Zero, 0, 0, 0, 0,
- Win32.FlagsSetWindowPos.SWP_NOACTIVATE |
- Win32.FlagsSetWindowPos.SWP_NOMOVE |
- Win32.FlagsSetWindowPos.SWP_NOSIZE |
- Win32.FlagsSetWindowPos.SWP_NOZORDER |
- Win32.FlagsSetWindowPos.SWP_NOOWNERZORDER |
- Win32.FlagsSetWindowPos.SWP_FRAMECHANGED);
- }
- }
- private MdiClientController m_mdiClientController = null;
- private MdiClientController GetMdiClientController()
- {
- if (m_mdiClientController == null)
- {
- m_mdiClientController = new MdiClientController();
- m_mdiClientController.HandleAssigned += new EventHandler(MdiClientHandleAssigned);
- m_mdiClientController.MdiChildActivate += new EventHandler(ParentFormMdiChildActivate);
- m_mdiClientController.Layout += new LayoutEventHandler(MdiClient_Layout);
- }
- return m_mdiClientController;
- }
- private void ParentFormMdiChildActivate(object sender, EventArgs e)
- {
- if (GetMdiClientController().ParentForm == null)
- return;
- IDockContent content = GetMdiClientController().ParentForm.ActiveMdiChild as IDockContent;
- if (content == null)
- return;
- if (content.DockHandler.DockPanel == this && content.DockHandler.Pane != null)
- content.DockHandler.Pane.ActiveContent = content;
- }
- private bool MdiClientExists
- {
- get
- {
- return GetMdiClientController().MdiClient != null;
- }
- }
- private void SetMdiClientBounds(Rectangle bounds)
- {
- GetMdiClientController().MdiClient.Bounds = bounds;
- }
- private void SuspendMdiClientLayout()
- {
- if (GetMdiClientController().MdiClient != null)
- GetMdiClientController().MdiClient.SuspendLayout();
- }
- private void ResumeMdiClientLayout(bool perform)
- {
- if (GetMdiClientController().MdiClient != null)
- GetMdiClientController().MdiClient.ResumeLayout(perform);
- }
- private void PerformMdiClientLayout()
- {
- if (GetMdiClientController().MdiClient != null)
- GetMdiClientController().MdiClient.PerformLayout();
- }
- // Called when:
- // 1. DockPanel.DocumentStyle changed
- // 2. DockPanel.Visible changed
- // 3. MdiClientController.Handle assigned
- private void SetMdiClient()
- {
- MdiClientController controller = GetMdiClientController();
- if (this.DocumentStyle == DocumentStyle.DockingMdi)
- {
- controller.AutoScroll = false;
- controller.BorderStyle = BorderStyle.None;
- if (MdiClientExists)
- controller.MdiClient.Dock = DockStyle.Fill;
- }
- else if (DocumentStyle == DocumentStyle.DockingSdi || DocumentStyle == DocumentStyle.DockingWindow)
- {
- controller.AutoScroll = true;
- controller.BorderStyle = BorderStyle.Fixed3D;
- if (MdiClientExists)
- controller.MdiClient.Dock = DockStyle.Fill;
- }
- else if (this.DocumentStyle == DocumentStyle.SystemMdi)
- {
- controller.AutoScroll = true;
- controller.BorderStyle = BorderStyle.Fixed3D;
- if (controller.MdiClient != null)
- {
- controller.MdiClient.Dock = DockStyle.None;
- controller.MdiClient.Bounds = SystemMdiClientBounds;
- }
- }
- }
- internal Rectangle RectangleToMdiClient(Rectangle rect)
- {
- if (MdiClientExists)
- return GetMdiClientController().MdiClient.RectangleToClient(rect);
- else
- return Rectangle.Empty;
- }
- }
- }
|