GetWindowRect返回0

时间:2016-01-27 01:00:07

标签: c# .net dll window

我正在尝试找到我正在打开的新进程的窗口大小,但它的高度和宽度返回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();
}

2 个答案:

答案 0 :(得分:1)

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

IntPtr hWnd = process.MainWindowHandle;
在进程窗口实际上有机会打开之前

答案 1 :(得分:-1)

签名错误应该是:

import sys
import os
import glob
directory = ["A", "B", "C", "D", "E"]

for folder in directory:
    for file in glob.glob(os.path.join(folder, "savedrecs(*).txt")):
        print file
        with open(file) as f:
            a=sum(not line.strip() for line in f)
            print a

相应地更改调用代码:

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