如何实现子窗体跟随父窗体一起移动?
展开全部
在主窗体上产生一个子窗体,父窗体有标题栏,在移动父窗体时如何让子窗体跟随父窗体一起移动,相对位置移动前后相同; public Tool() { InitializeComponent(); } #region[私有变量] Main MFrom { set; get; } private Point _parentPoint, _basePoint,ItemPoint,StartPoint,EndPoint; private int _MoveWidth = 25; private bool _adsorb; //是否吸附 #endregion public Tool(Main parent) { InitializeComponent(); MFrom = parent; this.Location = new Point(parent.Location.X, parent.Location.Y + MFrom.Height - this.Height - MFrom.statusBar1.Height); MFrom.LocationChanged +=new EventHandler(MFrom_LocationChanged); this.LocationChanged +=new EventHandler(Tool_LocationChanged); this._toolBar.ButtonClick+=new ToolBarButtonClickEventHandler(_toolBar_ButtonClick); } /// <summary> /// 父窗体移动带动子窗体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MFrom_LocationChanged(object sender, EventArgs e) { //? } /// <summary> /// 子窗体移动吸附 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Tool_LocationChanged(object sender, EventArgs e) { _basePoint = this.Location; _parentPoint = MFrom.Location; ItemPoint = MFrom.PointToScreen(MFrom.toolBar1.Location); if (_parentPoint.X - _basePoint.X <= _MoveWidth && _basePoint.X - _parentPoint.X <= _MoveWidth) { this.Location = new Point(_parentPoint.X, this.Location.Y); _adsorb = true; } else if (_parentPoint.X + MFrom.Width - _basePoint.X <= this.Width + _MoveWidth && _parentPoint.X + MFrom.Width - _basePoint.X >= this.Width -_MoveWidth) { this.Location = new Point(_parentPoint.X + MFrom.Width - this.Width, this.Location.Y); _adsorb = true; } else if (ItemPoint.Y + MFrom.toolBar1.Height - _basePoint.Y <= _MoveWidth && _basePoint.Y - ItemPoint.Y - MFrom.toolBar1.Height <= _MoveWidth) { this.Location = new Point(this.Location.X, ItemPoint.Y + MFrom.toolBar1.Height); _adsorb = true; } else if (_parentPoint.Y + MFrom.Height - _basePoint.Y - this.Height <= _MoveWidth + MFrom.statusBar1.Height && _basePoint.Y + this.Height - _parentPoint.Y - MFrom.Height <= _MoveWidth + MFrom.statusBar1.Height) { this.Location = new Point(this.Location.X, _parentPoint.Y + MFrom.Height - this.Height - MFrom.statusBar1.Height - 5); _adsorb = true; } else { _adsorb = false; } } } 截图:
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询