如何实现子窗体跟随父窗体一起移动?

cfji225
2013-10-14 · TA获得超过4531个赞
知道小有建树答主
回答量:1054
采纳率:0%
帮助的人:2005万
展开全部
在主窗体上产生一个子窗体,父窗体有标题栏,在移动父窗体时如何让子窗体跟随父窗体一起移动,相对位置移动前后相同; 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; } } } 截图:
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式