在重新审视这个问题之后,我做了一些挖掘并找到了一个适合我需求的混合解决方案。
我把建议通过p-daddy:https://stackoverflow.com/a/428782/654244
我修改的代码为32位和64位编译使用建议通过hans-passant工作: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);
}
它仍然阻止应用程序,但不是它。海报正在寻求一种方法,让它阻止应用程序的一个窗口,同时允许来自同一应用程序的其他窗口继续正常处理。 – tcarvin
tcarvin是正确的。我会更详细地更新我的问题。 – KyleK
尝试从主窗体打开第二个窗体(Form2a),使用“Show”,然后像以前一样继续。看看你是否可以切换到Form2a。 –