【问题标题】:MDI child capture mouse input to move parent MDI containerMDI 子捕获鼠标输入以移动父 MDI 容器
【发布时间】:2012-10-21 18:07:54
【问题描述】:

我有一个适合 1280x800 的 MDI 父容器(表单样式:无)。子表单正好适合,比如在子边缘和父边缘之间可能有 1 或 2 个像素的填充。意味着看起来无缝。

我知道如何使单独的表单可拖动(无论您单击并按住表单的哪个位置),但是是否可以让孩子指示 MDI 父级在屏幕上移动的位置?原因是父项上没有用户可以单击的内容。我们不能将对象添加到父对象(如菜单),因为它会与我们的设计冲突。

对这个有什么建议吗?目标是用户可以单击并拖动孩子的任何位置,它会移动整个应用程序。

【问题讨论】:

    标签: c# winforms mdi


    【解决方案1】:

    试试这个:

    public partial class Form1 : Form { 
      private const int WM_NCLBUTTONDOWN = 0xA1;
      private const int HT_CAPTION = 0x2;
    
      [DllImportAttribute("user32.dll")]
      private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
      [DllImportAttribute("user32.dll")]
      private static extern bool ReleaseCapture();
    
      public Form1() {
        InitializeComponent();
        Form f = new Form();
        f.MouseDown += ChildForm_MouseDown;
        f.MdiParent = this;
        f.Show();
      }
    
      void ChildForm_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
          ReleaseCapture();
          SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
      }
    }
    

    MDI Parent 窗体正在监听子窗体的 mousedown 事件,当用户单击子窗体时,它会像用户单击主窗体的标题栏一样。

    【讨论】:

    • 效果很好,谢谢 - 只需要确保包含 using System.Runtime.InteropServices;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多