using System; using System.Drawing; using System.Windows.Forms; namespace Microsoft.Windows.Forms { partial class UIControl { private IUIControl m_UIParent; private IUIControl UIParentInternal { get { return this.m_UIParent; } set { if (value != this.m_UIParent) { this.m_UIParent = value; this.OnParentChanged(EventArgs.Empty); } } } /// /// 获取或设置父控件 /// public IUIControl UIParent { get { return this.m_UIParent; } set { if (value != this.m_UIParent) { if (value != null) value.UIControls.Add(this); else this.m_UIParent.UIControls.Remove(this); } } } private UIControlCollection m_UIControls; /// /// 获取子控件集合 /// public UIControlCollection UIControls { get { this.CheckDisposed(); if (this.m_UIControls == null) this.m_UIControls = new UIControlCollection(this); return this.m_UIControls; } } private Region m_Region; /// /// 获取或设置控件区域 /// public Region Region { get { return this.m_Region; } set { if (this.m_Region != null) this.m_Region.Dispose(); this.m_Region = value == null ? null : value.Clone(); } } private DockStyle m_Dock; /// /// 获取或设置停靠方式 /// public DockStyle Dock { get { return this.m_Dock; } set { if (value != this.m_Dock) { this.m_Dock = value; if (this.m_UIParent != null) this.m_UIParent.DoLayout(); } } } private AnchorStyles m_Anchor = AnchorStyles.Left | AnchorStyles.Top; /// /// 获取或设置锚定方式 /// public AnchorStyles Anchor { get { return this.m_Anchor; } set { if (value != this.m_Anchor) { this.m_Anchor = value; this.m_LeftToParent = null; this.m_TopToParent = null; this.m_RightToParent = null; this.m_BottomToParent = null; } } } //初始左边距 private int? m_LeftToParent; //初始上边距 private int? m_TopToParent; //初始右边距 private int? m_RightToParent; //初始下边距 private int? m_BottomToParent; private int m_PreferredX; private int m_X; /// /// 获取或设置距离父控件的左边距 /// public int Left { get { return this.m_X; } set { if (value != this.m_X) { this.m_PreferredX = value; this.SetBounds(); } } } private int m_PreferredY; private int m_Y; /// /// 获取或设置距离父控件的上边距 /// public int Top { get { return this.m_Y; } set { if (value != this.m_Y) { this.m_PreferredY = value; this.SetBounds(); } } } private int m_PreferredWidth; private int m_Width; /// /// 获取或设置宽度 /// public int Width { get { return this.m_Width; } set { if (value != this.m_Width) { this.m_PreferredWidth = value; this.SetBounds(); } } } private int m_PreferredHeight; private int m_Height; /// /// 获取或设置高度 /// public int Height { get { return this.m_Height; } set { if (value != this.m_Height) { this.m_PreferredHeight = value; this.SetBounds(); } } } /// /// 获取距离父控件的右边距 /// public int Right { get { return this.m_X + this.m_Width; } } /// /// 获取距离父控件的下边距 /// public int Bottom { get { return this.m_Y + this.m_Height; } } /// /// 获取或设置控件左上角的坐标 /// public Point Location { get { return new Point(this.m_X, this.m_Y); } set { if (value != this.Location) { this.m_PreferredX = value.X; this.m_PreferredY = value.Y; this.SetBounds(); } } } /// /// 获取或设置控件的大小 /// public Size Size { get { return new Size(this.m_Width, this.m_Height); } set { if (value != this.Size) { this.m_PreferredWidth = value.Width; this.m_PreferredHeight = value.Height; this.SetBounds(); } } } /// /// 获取或设置控件的位置和大小 /// public Rectangle Bounds { get { return new Rectangle(this.m_X, this.m_Y, this.m_Width, this.m_Height); } set { if (value != this.Bounds) { this.m_PreferredX = value.X; this.m_PreferredY = value.Y; this.m_PreferredWidth = value.Width; this.m_PreferredHeight = value.Height; this.SetBounds(); } } } /// /// 获取控件客户区大小 /// public Size ClientSize { get { return this.Size; } set { this.Size = value; } } /// /// 获取控件客户区的位置和大小 /// public Rectangle ClientRectangle { get { return new Rectangle(0, 0, this.m_Width, this.m_Height); } } private Padding m_Padding; /// /// 获取或设置内边距 /// public Padding Padding { get { return this.m_Padding; } set { if (value != this.m_Padding) { this.m_Padding = value; this.Invalidate(); } } } private int m_LayoutSuspendCount; /// /// 获取布局操作是否被挂起 /// public bool LayoutSuspended { get { return this.m_LayoutSuspendCount != 0 || (this.m_UIParent != null && this.m_UIParent.LayoutSuspended); } } /// /// 设置控件位置和大小,先计算后设置 /// private void SetBounds() { if (this.m_UIParent == null || this.Dock == DockStyle.None) { this.SetBoundsCore(); this.DoLayout(); } else { this.m_UIParent.DoLayout(); } } /// /// 设置控件位置和大小,直接设置 /// private void SetBoundsCore() { if (this.m_PreferredX != this.m_X || this.m_PreferredY != this.m_Y) { this.m_X = this.m_PreferredX; this.m_Y = this.m_PreferredY; if (this.m_UIParent != null) this.m_UIParent.Invalidate(); this.OnLocationChanged(EventArgs.Empty); } if (this.m_PreferredWidth != this.m_Width || this.m_PreferredHeight != this.m_Height) { this.m_Width = this.m_PreferredWidth; this.m_Height = this.m_PreferredHeight; this.Invalidate(); this.OnSizeChanged(EventArgs.Empty); } } /// /// 挂起布局操作 /// public void SuspendLayout() { this.m_LayoutSuspendCount++; } /// /// 恢复挂起的布局操作 /// public void ResumeLayout() { this.ResumeLayout(false); } /// /// 恢复挂起的布局操作,可选择是否强制执行布局 /// /// 如果强制则为 true, 否则为 false public void ResumeLayout(bool performLayout) { this.m_LayoutSuspendCount--; this.DoLayoutCore(performLayout); } /// /// 如果未挂起布局操作则重新计算子控件布局 /// public void DoLayout() { this.DoLayoutCore(false); } /// /// 重新计算子控件布局,可选择是否强制执行布局 /// /// 如果强制则为 true, 否则为 false protected void DoLayoutCore(bool performLayout) { if (performLayout || !this.LayoutSuspended) DoLayoutInternal(this); } /// /// 将控件置于 Z 轴底层 /// public void SendToBack() { IUIControl parent = this.m_UIParent; if (parent == null) return; parent.SuspendLayout(); try { parent.UIControls.Remove(this); parent.UIControls.Insert(0, this); } finally { parent.ResumeLayout(); } } /// /// 将控件置于 Z 轴顶层 /// public void BringToFront() { IUIControl parent = this.m_UIParent; if (parent == null) return; parent.SuspendLayout(); try { parent.UIControls.Remove(this); parent.UIControls.Add(this); } finally { parent.ResumeLayout(); } } /// /// 将屏幕坐标系的点转换为控件坐标系的点 /// /// 点 /// 转换后的点 public Point PointToClient(Point p) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { p.Offset(-parent.Left, -parent.Top); parent = parent.UIParent; } return window.PointToClient(p); } /// /// 将控件坐标系的点转换为屏幕坐标系的点 /// /// 点 /// 转换后的点 public Point PointToScreen(Point p) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { p.Offset(parent.Left, parent.Top); parent = parent.UIParent; } return window.PointToScreen(p); } /// /// 将屏幕坐标系的矩形转换为控件坐标系的矩形 /// /// 矩形 /// 转换后的矩形 public Rectangle RectangleToClient(Rectangle r) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { r.Offset(-parent.Left, -parent.Top); parent = parent.UIParent; } return window.RectangleToClient(r); } /// /// 将控件坐标系的矩形转换为屏幕坐标系的矩形 /// /// 矩形 /// 转换后的矩形 public Rectangle RectangleToScreen(Rectangle r) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { r.Offset(parent.Left, parent.Top); parent = parent.UIParent; } return window.RectangleToScreen(r); } /// /// 将所在 Win32 控件坐标系的点转换为控件坐标系的点 /// /// 点 /// 转换后的点 public Point PointToUIControl(Point p) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { p.Offset(-parent.Left, -parent.Top); parent = parent.UIParent; } return p; } /// /// 将控件坐标系的点转换为所在 Win32 控件坐标系的点 /// /// 点 /// 转换后的点 public Point PointToUIWindow(Point p) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { p.Offset(parent.Left, parent.Top); parent = parent.UIParent; } return p; } /// /// 将所在 Win32 控件坐标系的矩形转换为控件坐标系的矩形 /// /// 矩形 /// 转换后的矩形 public Rectangle RectangleToUIControl(Rectangle r) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { r.Offset(-parent.Left, -parent.Top); parent = parent.UIParent; } return r; } /// /// 将控件坐标系的矩形转换为所在 Win32 控件坐标系的矩形 /// /// 矩形 /// 转换后的矩形 public Rectangle RectangleToUIWindow(Rectangle r) { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) { r.Offset(parent.Left, parent.Top); parent = parent.UIParent; } return r; } /// /// 查找所在的 Win32 控件 /// /// 所在的 Win32 控件 public IUIWindow FindUIWindow() { IUIControl parent = this; IUIWindow window = null; while (parent != null && (window = parent as IUIWindow) == null) parent = parent.UIParent; return window; } /// /// 根据坐标查找子控件 /// /// 坐标 /// 子控件 public UIControl FindUIChild(Point p) { return FindUIChildInternal(this, p); } /// /// 根据名称查找子控件 /// /// 名称 /// 子控件 public UIControl FindUIChild(string name) { return FindUIChildInternal(this, name); } } }