在.NET中是否有相当于Mac OS X Document的模式表?

我的应用程序越来越多地要求让某些对话框的行为类似于Mac OS X Document模式表function,其中对话框只是父控件/对话框的模态,而不是整个应用程序(参见http:// en。 wikipedia.org/wiki/Window_dialog )。

当前的窗口ShowDialog()不足以满足我的应用程序的需要,因为我需要在应用程序中将对话框设置为模态到另一个对话框,但仍然允许用户访问应用程序的其他区域。

在C#.NET中是否有与Document modal Sheet相同的function? 或者甚至是某个人做过的密切实施,还是我自己尝试实现这个function? 我试着搜索Google和SO无济于事。

谢谢,

凯尔

在重新审视这个问题后,我做了一些挖掘,找到了一个适合我需求的混合解决方案。

我接受了p-daddy的建议: https : //stackoverflow.com/a/428782/654244

我使用hans-passant的建议修改了代码以适用于32位和64位编译: https : //stackoverflow.com/a/3344276/654244

结果如下:

const int GWL_STYLE = -16; const int WS_DISABLED = 0x08000000; public static int GetWindowLong(IntPtr hWnd, int nIndex) { if (IntPtr.Size == 4) { return GetWindowLong32(hWnd, nIndex); } return GetWindowLongPtr64(hWnd, nIndex); } public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) { if (IntPtr.Size == 4) { return SetWindowLong32(hWnd, nIndex, dwNewLong); } return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); } [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)] private static extern int GetWindowLong32(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)] private static extern int GetWindowLongPtr64(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)] private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)] private static extern int SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong); public static void SetNativeEnabled(IWin32Window control, bool enabled) { if (control == null || control.Handle == IntPtr.Zero) return; NativeMethods.SetWindowLong(control.Handle, NativeMethods.GWL_STYLE, NativeMethods.GetWindowLong(control.Handle, NativeMethods.GWL_STYLE) & ~NativeMethods.WS_DISABLED | (enabled ? 0 : NativeMethods.WS_DISABLED)); } public static void ShowChildModalToParent(IWin32Window parent, Form child) { if (parent == null || child == null) return; //Disable the parent. SetNativeEnabled(parent, false); child.Closed += (s, e) => { //Enable the parent. SetNativeEnabled(parent, true); }; child.Show(parent); } 

Form.ShowDialog方法允许您在调用它时指定所有者。 在这种情况下,表单仅对给定的所有者是模态的。

编辑:我尝试了这个混合结果。 我创建了一个带有主窗体的简单Windows窗体应用程序和另外两个。 从按钮单击主窗体,我使用Show方法打开Form2。 Form2上也有一个按钮,当点击时,我使用ShowDialog方法打开Form3,传递Form2作为它的所有者。 虽然Form3似乎是Form2的模态,但在关闭Form3之前,我无法切换回Form1。