| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Security.Permissions;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 弹出控件
- /// </summary>
- [ToolboxItem(false)]
- public class PopupControlHost : ToolStripDropDown
- {
- #region 事件声明
- #region SelectedOK
- /// <summary>
- /// 当确认选择项时发生。
- /// </summary>
- private static readonly object EventSelectedOK = new object();
- /// <summary>
- /// 当确认选择项时发生。
- /// </summary>
- [Description("当确认选择项时发生。"), Category("CustomerEx")]
- internal event EventHandler SelectedOK
- {
- add
- {
- base.Events.AddHandler(EventSelectedOK, value);
- }
- remove
- {
- base.Events.AddHandler(EventSelectedOK, value);
- }
- }
- /// <summary>
- /// 引发 SelectedOK 事件
- /// </summary>
- /// <param name="e">包含事件数据的 EventArgs</param>
- protected virtual void OnSelectedOK(EventArgs e)
- {
- EventHandler eventHandler = (EventHandler)base.Events[EventSelectedOK];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- #endregion
- #region SelectedCancel
- /// <summary>
- /// 当取消选择项时发生。
- /// </summary>
- private static readonly object EventSelectedCancel = new object();
- /// <summary>
- /// 当取消选择项时发生。
- /// </summary>
- [Description("当取消选择项时发生。"), Category("CustomerEx")]
- internal event EventHandler SelectedCancel
- {
- add
- {
- base.Events.AddHandler(EventSelectedCancel, value);
- }
- remove
- {
- base.Events.AddHandler(EventSelectedCancel, value);
- }
- }
- /// <summary>
- /// 引发 SelectedCancel 事件
- /// </summary>
- /// <param name="e">包含事件数据的 EventArgs</param>
- protected virtual void OnSelectedCancel(EventArgs e)
- {
- EventHandler eventHandler = (EventHandler)base.Events[EventSelectedCancel];
- if (eventHandler != null)
- {
- eventHandler(this, e);
- }
- }
- #endregion
- #endregion
- #region 常量
- //private static readonly Size GRIP_SIZE = new Size(0x10, 0x10);
- #endregion
- #region 成员变量
- /// <summary>
- /// 弹出控件的父控件
- /// </summary>
- private Control _owner = null;
- /// <summary>
- /// 承载自定义控件或 Windows 窗体控件。
- /// </summary>
- private ToolStripControlHost _controlHost = null;
- /// <summary>
- /// 弹出的内容
- /// </summary>
- private Control _popupControl = null;
- /// <summary>
- /// 是否改变控件形状
- /// </summary>
- private bool _changeRegion = false;
- /// <summary>
- /// 显示时是否获得焦点
- /// </summary>
- private bool _focusOnOpen = true;
- /// <summary>
- /// 是否接受alt键输入
- /// </summary>
- private bool _acceptAlt = true;
- /// <summary>
- /// 能否改变控件大小
- /// </summary>
- private bool _canResize = true;
- /// <summary>
- /// 控件是否打开
- /// </summary>
- private bool _isOpen = false;
- private PopupControlHost _ownerPopup;
- private PopupControlHost _childPopup;
- private bool _resizableTop = false;
- private bool _resizableLeft = false;
- //private Color _borderColor = Color.FromArgb(23, 169, 254);
- //private VisualStyleRenderer sizeGripRenderer;
- private bool _sizeHasChanged = false;
- #endregion
- #region 构造函数
- /// <summary>
- /// 弹出控件
- /// </summary>
- public PopupControlHost(Control owner)
- {
- this.DoubleBuffered = true;
- this.ResizeRedraw = true;
- this.AutoSize = false;
- this.Padding = Padding.Empty;
- this.Margin = Padding.Empty;
- this.ResizeRedraw = true;
- this.SetOwner(owner);
- }
- #endregion
- #region 属性
- /// <summary>
- /// 获取弹出的内容
- /// </summary>
- public Control Content
- {
- get
- {
- return this._popupControl;
- }
- set
- {
- if (this._popupControl != value)
- {
- this.CreateHost(value);
- }
- }
- }
- /// <summary>
- /// 获取弹出的内容
- /// </summary>
- public ToolStripControlHost Host
- {
- get
- {
- return this._controlHost;
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示弹出控件是否是异形控件。
- /// </summary>
- public bool ChangeRegion
- {
- get
- {
- return this._changeRegion;
- }
- set
- {
- this._changeRegion = value;
- this.UpdateRegion();
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示控件显示时,是否获得焦点。
- /// </summary>
- public bool FocusOnOpen
- {
- get
- {
- return this._focusOnOpen;
- }
- set
- {
- this._focusOnOpen = value;
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示控件是否接受Alt输入。
- /// </summary>
- public bool AcceptAlt
- {
- get
- {
- return this._acceptAlt;
- }
- set
- {
- this._acceptAlt = value;
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示用户能否改变控件大小。
- /// </summary>
- public bool CanResize
- {
- get
- {
- return this._canResize;
- }
- set
- {
- this._canResize = value;
- }
- }
- ///// <summary>
- ///// 获取或设置一个值,该值指示控件边框颜色。
- ///// </summary>
- //public Color BorderColor
- //{
- // get
- // {
- // return this._borderColor;
- // }
- // set
- // {
- // this._borderColor = value;
- // }
- //}
- /// <summary>
- /// 获取一个值,该值指示控件是否打开状态。
- /// </summary>
- public bool IsOpen
- {
- get
- {
- return this._isOpen;
- }
- }
- #endregion
- #region 重写事件
- /// <summary>
- /// 控件正打开时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnOpening(CancelEventArgs e)
- {
- if (this._popupControl.IsDisposed ||
- this._popupControl.Disposing)
- {
- e.Cancel = true;
- base.OnOpening(e);
- return;
- }
- //UpdateRegion();
- base.OnOpening(e);
- }
- /// <summary>
- /// 在打开控件时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnOpened(EventArgs e)
- {
- if (this._focusOnOpen)
- {
- if (this._popupControl is IPopupControl)
- {
- (this._popupControl as IPopupControl).FocusOnShown();
- }
- else
- {
- this._popupControl.Focus();
- }
- }
- this._isOpen = true;
- base.OnOpened(e);
- }
- /// <summary>
- /// 控件正关闭时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnClosing(ToolStripDropDownClosingEventArgs e)
- {
- base.OnClosing(e);
- }
- /// <summary>
- /// 在关闭控件时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
- {
- this._isOpen = false;
- base.OnClosed(e);
- }
- /// <summary>
- /// 大小改变时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnSizeChanged(EventArgs e)
- {
- base.OnSizeChanged(e);
- if (this._controlHost != null)
- {
- this._controlHost.Size = this.Size - this.Padding.Size;
- }
- if (this._isOpen)
- {
- this._sizeHasChanged = true;
- }
- }
- /// <summary>
- /// 绘制控件时
- /// </summary>
- /// <param name="e"></param>
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- }
- #endregion
- #region 重写方法
- /// <summary>
- /// 获取新窗口的参数
- /// </summary>
- protected override CreateParams CreateParams
- {
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- get
- {
- CreateParams cp = base.CreateParams;
- cp.ExStyle |= NativeMethods.WS_EX_NOACTIVATE;
- //cp.ExStyle |= 5242880;
- //cp.ExStyle &= -12289;
- return cp;
- }
- }
- /// <summary>
- /// 系统消息处理
- /// </summary>
- /// <param name="m"></param>
- [SecurityPermission(SecurityAction.LinkDemand,
- Flags = SecurityPermissionFlag.UnmanagedCode)]
- protected override void WndProc(ref Message m)
- {
- if (this.ProcessGrip(ref m))
- {
- return;
- }
- base.WndProc(ref m);
- }
- /// <summary>
- /// 处理对话框键
- /// </summary>
- /// <param name="keyData"></param>
- /// <returns></returns>
- [UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
- protected override bool ProcessDialogKey(Keys keyData)
- {
- //if (this._acceptAlt && ((keyData & Keys.Alt) == Keys.Alt))
- //{
- // if ((keyData & Keys.F4) != Keys.F4)
- // {
- // return false;
- // }
- // else
- // {
- // this.Close();
- // }
- //}
- // ESC
- if ((keyData & Keys.KeyCode) == Keys.Escape)
- {
- this.OnSelectedCancel(EventArgs.Empty);
- return base.ProcessDialogKey(Keys.Escape);
- //bool result = base.ProcessDialogKey(Keys.Escape);
- //if (result)
- //{
- // this.OnSelectedCancel(EventArgs.Empty);
- //}
- //return result;
- //return true;
- }
- // Enter
- if ((keyData & Keys.KeyCode) == Keys.Enter)
- {
- this.OnSelectedOK(EventArgs.Empty);
- return base.ProcessDialogKey(Keys.Escape);
- //bool result = base.ProcessDialogKey(Keys.Escape);
- //if (result)
- //{
- // this.OnSelectedOK(EventArgs.Empty);
- //}
- //return result;
- //return true;
- }
- return base.ProcessDialogKey(keyData);
- }
- #endregion
- #region 事件处理
- /// <summary>
- /// 控件显示范围改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PopupControlRegionChanged(object sender, EventArgs e)
- {
- this.UpdateRegion();
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 显示控件
- /// </summary>
- public new void Show()
- {
- this.ShowInternal();
- }
- /// <summary>
- /// 显示控件
- /// </summary>
- /// <param name="control"></param>
- public void Show(Control control)
- {
- this.SetOwner(control);
- this.ShowInternal();
- }
- #endregion
- #region 保护方法
- /// <summary>
- /// 更新空间显示范围
- /// </summary>
- protected virtual void UpdateRegion()
- {
- if (!this._changeRegion)
- {
- return;
- }
- if (base.Region != null)
- {
- base.Region.Dispose();
- base.Region = null;
- }
- if (this._popupControl.Region != null)
- {
- base.Region = this._popupControl.Region.Clone();
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 显示控件
- /// </summary>
- private void ShowInternal()
- {
- if (this._owner == null)
- {
- return;
- }
- if (this._canResize && !this._changeRegion)
- {
- this.Padding = new Padding(1, 1, 1, 3);
- }
- else if (!this._changeRegion)
- {
- this.Padding = new Padding(1);
- }
- else
- {
- this.Padding = Padding.Empty;
- }
- Size s = this._popupControl.Size + this.Padding.Size;
- if (!this._sizeHasChanged && this._owner != null)
- {
- s.Width = this._owner.Width;
- }
- this.Size = s;
- base.Show(this._owner, 0, this._owner.Height);
- }
- /// <summary>
- /// 设定Owner
- /// </summary>
- /// <param name="owner"></param>
- private void SetOwner(Control owner)
- {
- if (owner == null)
- {
- return;
- }
- this._owner = owner;
- this.Font = owner.Font;
- this.SetOwnerItem(owner);
- }
- /// <summary>
- /// 设定OwnerItem
- /// </summary>
- /// <param name="owner"></param>
- private void SetOwnerItem(Control owner)
- {
- if (owner == null)
- {
- return;
- }
- if (owner is PopupControlHost)
- {
- PopupControlHost popupControl = owner as PopupControlHost;
- this._ownerPopup = popupControl;
- this._ownerPopup._childPopup = this;
- this.OwnerItem = popupControl.Items[0];
- return;
- }
- if (owner.Parent != null)
- {
- this.SetOwnerItem(owner.Parent);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="control"></param>
- private void CreateHost(Control control)
- {
- if (this._controlHost != null)
- {
- this._controlHost.Dispose();
- }
- this._popupControl = control;
- if (control == null)
- {
- //throw new ArgumentException("control");
- return;
- }
- this._controlHost = new ToolStripControlHost(control, "popupControlHost");
- this._controlHost.Font = this.Font;
- this._controlHost.AutoSize = false;
- this._controlHost.Padding = Padding.Empty;
- this._controlHost.Margin = Padding.Empty;
- Size maxSize = new Size(500, 500);
- Size minSize = new Size(5, 5);
- if (control.MaximumSize.Width > 0 &&
- maxSize.Width > control.MaximumSize.Width)
- {
- maxSize.Width = control.MaximumSize.Width;
- }
- if (control.MaximumSize.Height > 0 &&
- maxSize.Height > control.MaximumSize.Height)
- {
- maxSize.Height = control.MaximumSize.Height;
- }
- if (minSize.Width < control.MinimumSize.Width)
- {
- minSize.Width = control.MinimumSize.Width;
- }
- if (minSize.Height < control.MinimumSize.Height)
- {
- minSize.Height = control.MinimumSize.Height;
- }
- this.MaximumSize = maxSize;
- this.MinimumSize = minSize;
- base.Items.Add(this._controlHost);
- this._popupControl.RegionChanged += new EventHandler(PopupControlRegionChanged);
- this.UpdateRegion();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="m"></param>
- /// <returns></returns>
- [SecurityPermission(SecurityAction.LinkDemand,
- Flags = SecurityPermissionFlag.UnmanagedCode)]
- private bool ProcessGrip(ref Message m)
- {
- //if (m.Msg == NativeMethods.WM_NCACTIVATE &&
- // m.WParam != IntPtr.Zero &&
- // this._childPopup != null &&
- // this._childPopup.Visible)
- //{
- // this._childPopup.Hide();
- //}
- if (this._canResize && !this._changeRegion)
- {
- if (m.Msg == NativeMethods.WM_NCHITTEST)
- {
- return this.OnNcHitTest(ref m);
- }
- if (m.Msg == NativeMethods.WM_GETMINMAXINFO)
- {
- return this.OnGetMinMaxInfo(ref m);
- }
- }
- return false;
- }
- /// <summary>
- /// 改变大小的范围
- /// </summary>
- /// <param name="m"></param>
- /// <returns></returns>
- [SecurityPermission(SecurityAction.LinkDemand,
- Flags = SecurityPermissionFlag.UnmanagedCode)]
- private bool OnGetMinMaxInfo(ref Message m)
- {
- NativeMethods.MINMAXINFO minmax =
- (NativeMethods.MINMAXINFO)Marshal.PtrToStructure(
- m.LParam, typeof(NativeMethods.MINMAXINFO));
- minmax.maxTrackSize = this.MaximumSize;
- minmax.minTrackSize = this.MinimumSize;
- Marshal.StructureToPtr(minmax, m.LParam, false);
- return true;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="m"></param>
- /// <returns></returns>
- private bool OnNcHitTest(ref Message m)
- {
- Point location = this.PointToClient(new Point(
- NativeMethods.LOWORD(m.LParam), NativeMethods.HIWORD(m.LParam)));
- Rectangle gripRect = Rectangle.Empty;
- if (this._canResize && !this._changeRegion)
- {
- if (this._resizableLeft)
- {
- if (this._resizableTop)
- {
- gripRect = new Rectangle(0, 0, 6, 6);
- }
- else
- {
- gripRect = new Rectangle(
- 0,
- this.Height - 6,
- 6,
- 6);
- }
- }
- else
- {
- if (this._resizableTop)
- {
- gripRect = new Rectangle(this.Width - 6, 0, 6, 6);
- }
- else
- {
- gripRect = new Rectangle(
- this.Width - 6,
- this.Height - 6,
- 6,
- 6);
- }
- }
- }
- if (gripRect.Contains(location))
- {
- if (this._resizableLeft)
- {
- if (this._resizableTop)
- {
- m.Result = (IntPtr)NativeMethods.HTTOPLEFT;
- return true;
- }
- else
- {
- m.Result = (IntPtr)NativeMethods.HTBOTTOMLEFT;
- return true;
- }
- }
- else
- {
- if (this._resizableTop)
- {
- m.Result = (IntPtr)NativeMethods.HTTOPRIGHT;
- return true;
- }
- else
- {
- m.Result = (IntPtr)NativeMethods.HTBOTTOMRIGHT;
- return true;
- }
- }
- }
- else
- {
- Rectangle rectClient = this.ClientRectangle;
- if (location.X > rectClient.Right - 3 &&
- location.X <= rectClient.Right &&
- !this._resizableLeft)
- {
- m.Result = (IntPtr)NativeMethods.HTRIGHT;
- return true;
- }
- else if (location.Y > rectClient.Bottom - 3 &&
- location.Y <= rectClient.Bottom &&
- !this._resizableTop)
- {
- m.Result = (IntPtr)NativeMethods.HTBOTTOM;
- return true;
- }
- else if (location.X > -1 &&
- location.X < 3 &&
- this._resizableLeft)
- {
- m.Result = (IntPtr)NativeMethods.HTLEFT;
- return true;
- }
- else if (location.Y > -1 &&
- location.Y < 3 &&
- this._resizableTop)
- {
- m.Result = (IntPtr)NativeMethods.HTTOP;
- return true;
- }
- }
- return false;
- }
- #endregion
- /*
- private bool OnNcHitTest(ref Message m, bool contentControl)
- {
- int x = NativeMethods.LOWORD(m.LParam);
- int y = NativeMethods.HIWORD(m.LParam);
- Point clientLocation = PointToClient(new Point(x, y));
- GripBounds gripBouns = new GripBounds(contentControl ? content.ClientRectangle : ClientRectangle);
- IntPtr transparent = new IntPtr(NativeMethods.HTTRANSPARENT);
- if (resizableTop)
- {
- if (resizableRight && gripBouns.TopLeft.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOPLEFT;
- return true;
- }
- if (!resizableRight && gripBouns.TopRight.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOPRIGHT;
- return true;
- }
- if (gripBouns.Top.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOP;
- return true;
- }
- }
- else
- {
- if (resizableRight && gripBouns.BottomLeft.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOMLEFT;
- return true;
- }
- if (!resizableRight && gripBouns.BottomRight.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOMRIGHT;
- return true;
- }
- if (gripBouns.Bottom.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOM;
- return true;
- }
- }
- if (resizableRight && gripBouns.Left.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTLEFT;
- return true;
- }
- if (!resizableRight && gripBouns.Right.Contains(clientLocation))
- {
- m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTRIGHT;
- return true;
- }
- return false;
- }
- private VS.VisualStyleRenderer sizeGripRenderer;
- /// <summary>
- /// Paints the size grip.
- /// </summary>
- /// <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs" /> instance containing the event data.</param>
- public void PaintSizeGrip(PaintEventArgs e)
- {
- if (e == null || e.Graphics == null || !resizable)
- {
- return;
- }
- Size clientSize = content.ClientSize;
- if (Application.RenderWithVisualStyles)
- {
- if (this.sizeGripRenderer == null)
- {
- this.sizeGripRenderer = new VS.VisualStyleRenderer(VS.VisualStyleElement.Status.Gripper.Normal);
- }
- this.sizeGripRenderer.DrawBackground(e.Graphics, new Rectangle(clientSize.Width - 0x10, clientSize.Height - 0x10, 0x10, 0x10));
- }
- else
- {
- ControlPaint.DrawSizeGrip(e.Graphics, content.BackColor, clientSize.Width - 0x10, clientSize.Height - 0x10, 0x10, 0x10);
- }
- }
- */
- }
- }
|