簡體   English   中英

如何在 .NET 1.1 中顯示無模式擁有的對話框

[英]How to show modeless owned dialog in .NET 1.1

我需要在.NET 1.1中顯示一個無模式對話框。 以下代碼適用於 .NET 2.0 或更高版本:

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetActiveWindow();

private void ShowModelessOwnedDialog()
{
   IntPtr wHnd = GetActiveWindow();
   NativeWindow parent = NativeWindow.FromHandle(wHnd);
   MyForm f = new MyForm();
   f.Show(parent);
}

調用Show(IWin32Window)是在 .NET 2.0 中引入的。 你知道如何欺騙這個代碼在.NET 1.1中工作嗎? 也許有任何非托管電話?

是 1999 年的一篇文章,展示了如何調用SetWindowLong來完成此操作。 我對您不得不使用 .NET 版本 1 表示哀悼。

這是在 .NET 1.1 中將所有者分配給托管表單的方法。 我從@Dave Markle 答案和 .NET 2.0 的Show(IWin32Window)實現中提取了以下代碼。

    private void AssignOwner()
    {
        AssignOwner(this, GetActiveWindow());
    }

    private void AssignOwner(Form f, IntPtr ownerHandle)
    {
        if (ownerHandle == IntPtr.Zero) return;

        NativeWindow parent = NativeWindow.FromHandle(ownerHandle);

        GetWindowLong(new HandleRef(f, f.Handle), -8);
        SetWindowLong(new HandleRef(f, f.Handle), -8, new HandleRef(parent, ownerHandle));
    }

    public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)
    {
        if (IntPtr.Size == 4)
        {
            return GetWindowLong32(hWnd, nIndex);
        }
        return GetWindowLongPtr64(hWnd, nIndex);
    }

    public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong)
    {
        if (IntPtr.Size == 4)
        {
            return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
        }
        return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern IntPtr GetActiveWindow();
    [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);
    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);
    [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong);
    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

MSDN 指出,在創建 window 后,所有權無法轉移。
因為創建 window 發生在Form構造函數中,這給您帶來了問題。

然而,Raymond Chen

所有權是一個與頂層 windows 相關的概念。 頂級 window 可以選擇擁有所有者,在調用CreateWindowEx時也會指定所有者,並且可以通過我的演講中描述的復雜機制進行更改

我認為有問題的談話來自 PDC 05 ,但我不能確定。

你試過SetParent嗎?

static extern void SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

.net 1.1 中有一個MessageBox.Show 過載和 IWin32Window

http://msdn.microsoft.com/en-us/library/aa335416(v=VS.71).aspx

public static DialogResult Show(
 IWin32Window owner,
 string text,
 string caption,
 MessageBoxButtons buttons,
 MessageBoxIcon icon,
 MessageBoxDefaultButton defaultButton
);

以及在這里獲取 IWin2Window 的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM