GetWindowRect 返回 0
GetWindowRect returning 0
我正在尝试查找我正在打开的新进程的 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();
}
签名错误应该是:
[DllImport("user32.dll", SetLastError=true)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
相应地更改调用代码:
RECT rect;
process.WaitForInputIdle();
GetWindowRect(hWnd, out rect);
谢谢大家的回答,但实际问题是我在做
IntPtr hWnd = process.MainWindowHandle;
在进程window之前实际上有机会打开。
我正在尝试查找我正在打开的新进程的 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();
}
签名错误应该是:
[DllImport("user32.dll", SetLastError=true)]
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
相应地更改调用代码:
RECT rect;
process.WaitForInputIdle();
GetWindowRect(hWnd, out rect);
谢谢大家的回答,但实际问题是我在做
IntPtr hWnd = process.MainWindowHandle;
在进程window之前实际上有机会打开。