我希望能够写出这样的代码:如何获得控件相对于窗口客户端矩形的位置?
HWND hwnd = <the hwnd of a button in a window>;
int positionX;
int positionY;
GetWindowPos(hwnd, &positionX, &positionY);
SetWindowPos(hwnd, 0, positionX, positionY, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
而且有它什么也不做。不过,我不知道如何写一个GetWindowPos()
功能,让我回答了正确的单位:
void GetWindowPos(HWND hWnd, int *x, int *y)
{
HWND hWndParent = GetParent(hWnd);
RECT parentScreenRect;
RECT itemScreenRect;
GetWindowRect(hWndParent, &parentScreenRect);
GetWindowRect(hWnd, &itemScreenRect);
(*x) = itemScreenRect.left - parentScreenRect.left;
(*y) = itemScreenRect.top - parentScreenRect.top;
}
如果我用这个功能,我得到的是相对坐标左上角父窗口,但SetWindowPos()
希望相对于标题栏下面的区域的坐标(我假设这是“客户区”,但win32术语对我来说都是有点新的)。
解决方案 这是工作GetWindowPos()
功能(感谢谢尔盖):
它是一个窗口应用程序 – 2009-12-23 06:24:51
它是如何工作的,DirectX有什么用处。新手在directx。我做了我自己的功能 – 2009-12-23 06:26:05
是的,它是一个Windows应用程序,因此使用win32 api。 – Andy 2009-12-23 06:43:49