简体   繁体   English

如何在 .NET 1.1 中显示无模式拥有的对话框

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

I need to show a modeless dialog in .NET 1.1 .我需要在.NET 1.1中显示一个无模式对话框。 The following code works in .NET 2.0 or higher:以下代码适用于 .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);
}

The call Show(IWin32Window) was introduced in .NET 2.0.调用Show(IWin32Window)是在 .NET 2.0 中引入的。 Do you know how to trick this code to work in .NET 1.1 ?你知道如何欺骗这个代码在.NET 1.1中工作吗? Maybe any unmanaged call?也许有任何非托管电话?

Here is an article from way back in 1999, showing how you call SetWindowLong to accomplish this.是 1999 年的一篇文章,展示了如何调用SetWindowLong来完成此操作。 My condolences to you for having to use .NET version 1.我对您不得不使用 .NET 版本 1 表示哀悼。

This is the way to assign an owner to a managed form in .NET 1.1.这是在 .NET 1.1 中将所有者分配给托管表单的方法。 I extracted the following code from @Dave Markle answer and the Show(IWin32Window) implemantation of .NET 2.0.我从@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 states that after creating the window ownership cannot be transferred. MSDN 指出,在创建 window 后,所有权无法转移。
Because creating the window occurs in Form constructor, this poses a problem for you.因为创建 window 发生在Form构造函数中,这给您带来了问题。

However Raymond Chen says :然而,Raymond Chen

Ownership is a concept that relates top-level windows.所有权是一个与顶层 windows 相关的概念。 A top-level window can optionally have an owner, which is also specified when you call CreateWindowEx , and which you can change by a complicated mechanism described in my talk .顶级 window 可以选择拥有所有者,在调用CreateWindowEx时也会指定所有者,并且可以通过我的演讲中描述的复杂机制进行更改

I assume the talk in question is from PDC 05 but I can't be certain.我认为有问题的谈话来自 PDC 05 ,但我不能确定。

Did you give SetParent a try?你试过SetParent吗?

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

There is a MessageBox.Show overload taking and IWin32Window in .net 1.1 .net 1.1 中有一个MessageBox.Show 过载和 IWin32Window

http://msdn.microsoft.com/en-us/library/aa335416(v=VS.71).aspx 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
);

And this and example of getting an IWin2Window here以及在这里获取 IWin2Window 的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这是使用WPF从可停靠窗格显示无模式对话框的一种方法吗? - Is this a way to show a modeless dialog from a dockable pane using WPF? 如何关闭无模式对话框的所有打开的实例? - How to close all open instances of a modeless dialog box? 如何在C#控制台应用程序中创建无模式对话框 - How to create a modeless dialog box within a C# console app C#无模式对话框:如何从无模式对话框返回3个不同的值到主窗体,并将它们放在一起用作Color.FromARGB? - C# Modeless Dialog: How to return 3 different values from modeless dialog to main form and put them together to use as Color.FromARGB? WPF:关于模式窗口和无模式窗口,显示模式窗口后如何访问无模式窗口? - WPF: About modal window and modeless window, how to access modeless window after show modal window? App Store中的无模式对话框 - Modeless dialog box in App store 如何将“页面设置”和“打印机设置”显示为无模式表单? - How to show "page setup" and "printer setup" as modeless forms? 如何在ASP.net上显示另存为对话框 - How to show Save As Dialog on ASP.net 如何从网络标准显示警报对话框 - How to Show Alert Dialog From net standard 如何在C#.Net 3.5 SP1中显示身份验证对话框 - How to show authentication dialog in C# .Net 3.5 SP1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM