I am trying to get the position of another processes main window. This is the code I have right now:
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
private void UpdatePosition()
{
IntPtr windowHandle = new IntPtr();
Process[] processes = Process.GetProcessesByName("Player");
try
{
windowHandle = processes[0].MainWindowHandle;
}
catch //Player.exe is not open
{
Application.Exit();
return;
}
Rectangle rect;
GetWindowRect(windowHandle, out rect);
Location = new Point(rect.X, rect.Y);
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdatePosition();
}
For some reason, the position of rect
is always the middle of my screen (960, 540).
I have no clue why this happens, am I doing something wrong?