首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止表单移出我的主sdi表单

防止表单移出我的主sdi表单
EN

Stack Overflow用户
提问于 2014-02-10 17:31:06
回答 1查看 100关注 0票数 0

假设我有两个sdi表单,当应用程序运行时,显示一个sdi表单,从那里我显示另一个sdi表单,但问题是我可以在任何地方拖动第二个sdi表单,这是我不想要的。在MDI的情况下,MDI子窗体不能被拖出mdi boundary.so,在我的例子中,我想要模拟同样的事情。我不希望其他的sdi表单不能从我的主sdi表单的边界中拖出。所以只要指导我怎么做就行了。

我可以猜测我必须处理表单拖动事件,并从那里我必须检查表单顶部和左侧,但需要更多建议。

代码语言:javascript
复制
private void Form1_Move(object sender, EventArgs e)
{
    this.Location = defaultLocation;
}

我试着这样做,但它不起作用。

代码语言:javascript
复制
public partial class Form2 : Form
    {
        Form1 _parent = null;
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        private void Form2_Move(object sender, EventArgs e)
        {
            if((this.Location.X+this.Width) > _parent.Width)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
            }

            if ((this.Location.Y + this.Height) > _parent.Height)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
            }

            if (this.Location.Y < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.X);
            }

            if (this.Location.X < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.Y);
            }
        }
    }

请告诉我哪里弄错了。谢谢

更新

代码语言:javascript
复制
private void Form2_Move(object sender, EventArgs e)
        {
            int left = this.Left;
            int top = this.Top;

            if (this.Left < _parent.Left)
            {
                left = _parent.Left;
            }
            if (this.Right > _parent.Right)
            {
                left = _parent.Right - this.Width;
            }
            if (this.Top < _parent.Top)
            {
                top = _parent.Top;
            }
            if (this.Bottom > _parent.Bottom)
            {
                top = _parent.Bottom - this.Height;
            }

            this.Location = new Point(left, top);
        }
EN

回答 1

Stack Overflow用户

发布于 2014-02-10 19:18:54

我建议您遵循@ProgrammerV5推荐。但是如果你确实需要控制表单的移动,请看Cursor.Clip属性的用法

以下是一些信息:http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property

另外,你可能需要做一个MouseCapture

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21673362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档