【问题标题】:GetWindowRect returning 0GetWindowRect 返回 0
【发布时间】:2016-01-27 01:00:07
【问题描述】:

我正在尝试查找正在打开的新进程的窗口大小,但它的高度和宽度返回 0。这是我的代码:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
}

static void Main(string[] args)
{
    Process process = new Process();
    process.StartInfo.FileName = @"C:\Program Files (x86)\FirstClass\fcc32.exe";
    //C:\Program Files (x86)\FirstClass\fcc32.exe
    process.Start();

    Console.WriteLine(process.HandleCount);
    IntPtr hWnd = process.MainWindowHandle;

    RECT rect = new RECT();
    process.WaitForInputIdle();
    GetWindowRect(hWnd, ref rect);
    int width = rect.Right - rect.Left;
    int height = rect.Bottom - rect.Top;

    Console.WriteLine("Height: " + height + ", Width: " + width);
    Console.ReadLine();
}

【问题讨论】:

  • 调用GetWindowRect()的返回值是多少?是真是假?
  • 如果 process.MainWindowHandle 不为 NULL,那么我会在您的 fcc32 应用程序上使用 Spy++ 来查看该窗口的属性。可能是您的顶级窗口的大小为 0,0 ....并且充当创建的实际可见窗口的父级。也尝试调用“刷新”....stackoverflow.com/questions/16185217/…

标签: c# .net dll window


【解决方案1】:

感谢大家的回答,但实际问题是我在做

IntPtr hWnd = process.MainWindowHandle;

在进程窗口实际上有机会打开之前。

【讨论】:

    【解决方案2】:

    签名错了应该是:

    [DllImport("user32.dll", SetLastError=true)]
    static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
    

    相应地更改调用代码:

    RECT rect;
    process.WaitForInputIdle();
    GetWindowRect(hWnd, out rect);
    

    【讨论】:

    • 我刚试了,还是不行,你知道还有什么可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多