C# 如何阻止将MDI子窗体拖到父窗体的边缘之外?
“人员”是MDI父窗体 “新建”是MDI子窗体 我怎样才能阻止“新”被拖出“人”的边缘 我发现代码运行良好C# 如何阻止将MDI子窗体拖到父窗体的边缘之外?,c#,C#,“人员”是MDI父窗体 “新建”是MDI子窗体 我怎样才能阻止“新”被拖出“人”的边缘 我发现代码运行良好 protected override void OnMove(EventArgs e) { // // Get the MDI Client window reference // MdiClient mdiClient = null; foreach (Control ctl in MdiPa
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);
}
移动时受保护的覆盖无效(事件参数e)
{
//
//获取MDI客户端窗口引用
//
MdiClient MdiClient=null;
foreach(MdiParent.Controls中的控制ctl)
{
mdiClient=ctl作为mdiClient;
如果(mdiClient!=null)
打破
}
//
//不允许将表单移动到MDI客户端边界之外
//
if(左mdiClient.ClientRectangle.Height)
Top=mdiClient.ClientRectangle.Height—高度;
if(左+宽>mdiClient.ClientRectangle.Width)
左=mdiClient.ClientRectangle.Width-宽度;
base.OnMove(e);
}
MDI父窗体重新定位新窗体,就像显示多个级联窗体一样。要重新定位窗口,您必须在窗体加载
事件处理程序中设置位置。将子窗体开始位置设置为CenterParent隐藏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(Controls.OfType()中的MdiClient控件)
{
if(控件!=null)
{
新的不可旋转窗口(控制);
打破
}
}
在孩子的移动事件中,您可以使用代码来检测当前位置是否超出您想要的位置。您需要事先建立父窗口的上边缘、左边缘、下边缘和右边缘
代码如下:
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));
}
您需要对每一侧重复此操作。通过提前使用缓冲区计算边,代码可以更加精简,因为它们只会在父窗口移动时更改 防止它出现在那里,或被拖到那里?我不希望它被拖到边缘考虑用这些花边信息更新你的问题。=)无论您如何努力,都无法将MDI子窗口拖动到父窗口的边缘之外。当然,您是在谈论其他问题,答案是“如果您不喜欢MDI窗口模型,请不要使用MDI”。周围有很多停靠的窗口类库。@Hans:据我目前的理解,问题不是如何将子框架移到父框架之外;显然情况恰恰相反。我得到了“如何将子帧完全保持在父帧的可见区域内?”(即:如何防止子帧被拖动到父帧的边将其切断的位置)
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));
}