如何获取对 window 的引用,然后将其转换为 windows 形式

How do I get a reference to a window and then cast it to a windows form

因此,我试图获取对特定 window 的引用,我碰巧知道它是内存中的 Windows 表单对象。不幸的是它是私人的。所以我得绕着弯子去做。

我正在尝试让它工作:

    [DllImport("user32.dll")]
    internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr hWnd = FindWindow(null, "Fiddler - HTTP Debugging Proxy");
    Form Fiddle = Form.FromHandle(hWnd);

不幸的是我遇到了这个错误。

错误 CS0236 字段初始值设定项无法引用非静态字段、方法或 属性 'Test.hWnd'

与 C++ windows 调用的互操作不是我的强项。我很难理解这试图告诉我什么。以及如何实现。

DLL 导入和 FindWindow() 需要放在方法之外。 FindWindow 是一个方法声明。

    [DllImport("user32.dll")]
    internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    void method()
    {

        IntPtr hWnd = FindWindow(null, "Fiddler - HTTP Debugging Proxy");
        Form Fiddle = FromHandle(hWnd) as Form;
    }