在重新審視這個問題之後,我做了一些挖掘並找到了一個適合我需求的混合解決方案。
我把建議通過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。 –