I'm new to WinAPI and now I've met a strange problem.
class Program {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
static void Main(string[] args) {
foreach (Process p in Process.GetProcesses()) {
if (p.MainWindowTitle == "Test") {
IntPtr hWnd = p.MainWindowHandle;
RECT rect = new RECT();
GetWindowRect(hWnd, ref rect);
Console.WriteLine($"Left={rect.Left}\nTop={rect.Top}\nRight={rect.Right}\nBottom={rect.Bottom}");
break;
}
}
Console.ReadLine();
}
}
I opened a window whose title is "Test" and tried to get the window position using GetWindowPos.
The process was found successfully and the hWnd seemed to be correct, but Console output the following content:
Left=-25600
Top=-25600
Right=-25441
Bottom=-25573
Furthermore, no matter how I resize the window, I always got the same result as above.
May anyone help figure out the bugs in the code snippet? Thanks in advance!