Reputation: 2616
I have 2 windows, or rather window handles. I want to get the position and size of one window, and apply them to the other window, so that the second window covers the first completely.
Right now I'm using GetWindowRect()
to get the position and size of one window, for example,
IntPtr hWnd1 = some window handle;
Rect rect = new Rect();
GetWindowRect(hWnd1, ref rect);
and after some googling, it seems I have to use SetWindowPos()
to move and resize my other window on top.
SetWindowPos(hWnd2, some coords here);
However, I don't know how to convert the RECT
struct returned by GetWindowRect()
into coordinates I can use with SetWindowPos()
, any help appreciated.
Upvotes: 1
Views: 368
Reputation: 13089
I think you have to convert "Left, Top, Right, Bottom" to "X, Y, Width, Height":
RECT rct = GetWindowRect(windowHandler);
SetWindowPos(windowHandler, HWND_TOP, rct.Left, rct.Top, rct.Right - rct.Left, rct.Bottom - rct.Top, SWP_SHOWWINDOW);
You probably should use GetWindowPlacement instead of GetWindowRect, like this:
WINDOWPLACEMENT place;
bool ok = GetWindowPlacement(hwndTarget, out place);
ok = SetWindowPos(hwndSrc, new IntPtr(0), place.NormalPosition.Left, place.NormalPosition.Top, place.NormalPosition.Width, place.NormalPosition.Height, SetWindowPosFlags.ShowWindow);
Note this will only work if the window is in a "restored" position (not maximized).
I'm omitting the interop imports and structures, you can find it in http://www.pinvoke.net/default.aspx/user32/GetWindowPlacement.html and http://www.pinvoke.net/default.aspx/user32/SetWindowPos.html
Upvotes: 2