“人物”是MDI父表格 “新”是MDI子表格
如何阻止“新”被拖到“人物”边缘外?
我发现代码工作正常
protected override void OnMove(EventArgs e)
{
//
// Get the MDI Client window reference
//
MdiClient mdiClient = null;
foreach (Control ctl in MdiParent.Controls)
{
mdiClient = ctl as MdiClient;
if (mdiClient != null)
break;
}
//
// Don't allow moving form outside of MDI client bounds
//
if (Left < mdiClient.ClientRectangle.Left)
Left = mdiClient.ClientRectangle.Left;
if (Top < mdiClient.ClientRectangle.Top)
Top = mdiClient.ClientRectangle.Top;
if (Top + Height > mdiClient.ClientRectangle.Height)
Top = mdiClient.ClientRectangle.Height - Height;
if (Left + Width > mdiClient.ClientRectangle.Width)
Left = mdiClient.ClientRectangle.Width - Width;
base.OnMove(e);
}
答案 0 :(得分:0)
MDI父表单重新定位新表单类似于显示多个表单级联。要重新定位窗口,您必须在Form_Loading
事件处理程序中设置位置。
答案 1 :(得分:0)
将子表单StartPosition设置为CenterParent
答案 2 :(得分:0)
隐藏mdi容器的滚动条,示例代码:
internal sealed class NonScrollableWindow : NativeWindow
{
private readonly MdiClient _mdiClient;
public NonScrollableWindow(MdiClient parent)
{
_mdiClient = parent;
ReleaseHandle();
AssignHandle(_mdiClient.Handle);
}
internal void OnHandleDestroyed(object sender, EventArgs e)
{
ReleaseHandle();
}
private const int SB_BOTH = 3;
[DllImport("user32.dll")]
private static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow);
protected override void WndProc(ref Message m)
{
ShowScrollBar(m.HWnd, SB_BOTH, 0);
base.WndProc(ref m);
}
}
用法(在mdi父加载事件中),
foreach (MdiClient control in Controls.OfType<MdiClient>())
{
if (control != null)
{
new NonScrollableWindow(control);
break;
}
}
答案 3 :(得分:0)
在孩子的移动事件中,您可以使用代码来检测当前位置是否超出您想要的位置。您需要先建立父窗口的顶部,左侧,底部和右侧边缘。
代码如:
BufferWidth = 10; // 10 pixel buffer to edge
// ParentTop is the top Y co-ordinate of the parent window
if (this.location.Y > (ParentTop-BufferWidth))
{
int LocX = this.Location.X;
this.location = new Point(LocX, (ParentTop-BufferWidth));
}
您需要为每一方重复此操作。通过预先用缓冲区计算边缘可以更加简化代码,因为它们只会在父窗口移动时改变。