And*_*ndy 16 c++ user-interface winapi
我希望能够编写如下代码:
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);
Run Code Online (Sandbox Code Playgroud)
并且它什么都不做.但是,我无法弄清楚如何编写一个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;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这个函数,我得到相对于父窗口左上角的SetWindowPos()坐标,但是想要相对于标题栏下方区域的坐标(我假设这是"客户区",但是win32术语对我来说有点新鲜).
解决方案
这是工作GetWindowPos()功能(感谢Sergius):
void GetWindowPos(HWND hWnd, int *x, int *y)
{
HWND hWndParent = GetParent(hWnd);
POINT p = {0};
MapWindowPoints(hWnd, hWndParent, &p, 1);
(*x) = p.x;
(*y) = p.y;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12613 次 |
| 最近记录: |