我有一个适合 1280x800 的 MDI 父容器(表单样式:无)。子表单正好适合,比如在子边缘和父边缘之间可能有 1 或 2 个像素的填充。意味着看起来无缝。
我知道如何使单独的表单可拖动(无论您单击并按住表单的位置)但是是否可以让孩子指示 MDI 父级在屏幕上移动的位置?原因是父项上没有用户可以单击的内容。我们不能将对象添加到父对象(如菜单),因为它会与我们的设计相冲突。
对这个有什么建议吗?目标是用户可以单击并拖动孩子的任何地方,它会移动整个应用程序。
试试这个:
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 父窗体正在监听子窗体的 mousedown 事件,当用户单击子窗体时,它的行为就像用户单击主窗体的标题栏一样。