C# MDI - 如何防止滚动条?

发布于 2024-12-12 02:47:38 字数 284 浏览 0 评论 0原文

最好用图像来显示,我可以将表单拖离 MDI 父级的边缘,然后就会出现滚动条。

我宁愿将子窗体锁定到父窗体的区域。

我已经完成了寻找解决方案的工作,(其中很多都是两年多前回答的,所以我希望它们已经过时)并且只遇到人们检查“移动”事件的坐标......很好,但是如果我有怎么办20 份表格.. 或 100 份表格。我无法对所有这些进行编码,这有点愚蠢。当然,我可以在父窗体上设置一个属性。

MDI 滚动条问题

Best shown with an image, I can drag the forms off the edge of the MDI parent, and scroll bars appear.

Id much rather that child forms are locked to the area of the Parent form.

Ive done searching for a solution, (A lot of these are answered 2+ years ago, so im hoping they are outdated) and only come across people checking co-ordinates on the Move event.... Great, but what if I have 20 forms.. or 100 forms. I cant code all of them, its a bit silly. Surely there is a property somewhere I can just set on the parent form.

MDI Scrollbar problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤凫 2024-12-19 02:47:38

第 1 步:
您应该创建一个从 NativeWindow 类继承的新类并重写其 WndProc 方法。

第 2 步:
在 MDI 窗体中创建这个新类的新对象,并将 MDIClient 控件传递给其构造函数。

第 1 步代码:

  internal class MyNativeMDIclient : NativeWindow
    {
        private MdiClient mdiClient;

        public MyNativeMDIclient(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 /*false*/);
            base.WndProc(ref m);
        }
    }

第 2 步代码:

  foreach (Control control in this.Controls)
            {
                if (control is MdiClient)
                {
                    MyNativeMDIclient nw = new MyNativeMDIclient((MdiClient)control);
                    break;
                }
            }

Step 1:
You should create a new class inherited from NativeWindow class and override its WndProc method.

Step 2:
In your MDI form create a new object of this new class and pass the MDIClient control to its constructor.

Step 1 code:

  internal class MyNativeMDIclient : NativeWindow
    {
        private MdiClient mdiClient;

        public MyNativeMDIclient(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 /*false*/);
            base.WndProc(ref m);
        }
    }

Step 2 code:

  foreach (Control control in this.Controls)
            {
                if (control is MdiClient)
                {
                    MyNativeMDIclient nw = new MyNativeMDIclient((MdiClient)control);
                    break;
                }
            }
半边脸i 2024-12-19 02:47:38

你可以做这样的事情...

步骤!:你必须制作基本表单(如用户控件)

并将此代码放入该表单中

 namespace Controls
 {
  public partial class BaseForm : Form
  {
    public BaseForm()
    {
      InitializeComponent();
      StartPosition = FormStartPosition.WindowsDefaultLocation;
      MaximizeBox = false;
      Width = 806;
      //Width = 850;
      //Height = 760;
      Height = 730;
      //Width = 790;
      //Height = 617;
    }


    protected override void WndProc(ref Message m)
    {
      const int WM_SYSCOMMAND = 0x0112;
      const int SC_MOVE = 0xF010;
      //ShowScrollBar(this.Handle, (int)ScrollBarDirection.SB_BOTH, false);
      switch (m.Msg)
      {
        case WM_SYSCOMMAND:
          int command = m.WParam.ToInt32() & 0xfff0;
          if (command == SC_MOVE)
            return;
          break;
      }
      base.WndProc(ref m);
    }
  }
}

,然后在每个表单中你必须这样指定...

public partial class childform : BaseForm
{
   .......
}

确保所有子表单size 应该是以基本形式指定的大小

  • minsize 是 0,0

  • max Sixze 也是 0,0

  • 起始位置 - windowsdefaultlocation

  • windowstate - 正常

我希望它能帮助你..

you can do something like this...

Step !: you have to make base form( like user control)

and place this code in that form

 namespace Controls
 {
  public partial class BaseForm : Form
  {
    public BaseForm()
    {
      InitializeComponent();
      StartPosition = FormStartPosition.WindowsDefaultLocation;
      MaximizeBox = false;
      Width = 806;
      //Width = 850;
      //Height = 760;
      Height = 730;
      //Width = 790;
      //Height = 617;
    }


    protected override void WndProc(ref Message m)
    {
      const int WM_SYSCOMMAND = 0x0112;
      const int SC_MOVE = 0xF010;
      //ShowScrollBar(this.Handle, (int)ScrollBarDirection.SB_BOTH, false);
      switch (m.Msg)
      {
        case WM_SYSCOMMAND:
          int command = m.WParam.ToInt32() & 0xfff0;
          if (command == SC_MOVE)
            return;
          break;
      }
      base.WndProc(ref m);
    }
  }
}

and then in every form you have to specify like this...

public partial class childform : BaseForm
{
   .......
}

Make sure all your child forms size is should be size specified in base form

  • minsize is 0,0

  • max sixze is also 0,0

  • startposition - windowsdefaultlocation

  • windowstate - normal

I hope it will helps you ..

无声情话 2024-12-19 02:47:38

您始终可以覆盖父类中的移动功能,然后让所有表单继承自该类。

没有神奇的 API/属性(据我所知,如果我错了,抱歉)可以告诉 MDI 父级将子级锁定在其范围内。

You could always override the move functionality in a parent class, then have all of your forms inherit from that class.

Theres no magic API/property (that I know of, apologies if I'm wrong) to tell an MDI parent to lock children within its bounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文