假设我有两个sdi表单,当应用程序运行时,显示一个sdi表单,从那里我显示另一个sdi表单,但问题是我可以在任何地方拖动第二个sdi表单,这是我不想要的。在MDI的情况下,MDI子窗体不能被拖出mdi boundary.so,在我的例子中,我想要模拟同样的事情。我不希望其他的sdi表单不能从我的主sdi表单的边界中拖出。所以只要指导我怎么做就行了。
我可以猜测我必须处理表单拖动事件,并从那里我必须检查表单顶部和左侧,但需要更多建议。
private void Form1_Move(object sender, EventArgs e)
{
this.Location = defaultLocation;
}我试着这样做,但它不起作用。
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);
}
}
}请告诉我哪里弄错了。谢谢
更新
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);
}发布于 2014-02-10 19:18:54
我建议您遵循@ProgrammerV5推荐。但是如果你确实需要控制表单的移动,请看Cursor.Clip属性的用法
以下是一些信息:http://www.codeproject.com/Tips/375046/The-Cursor-Clip-Property
另外,你可能需要做一个MouseCapture。
https://stackoverflow.com/questions/21673362
复制相似问题