如何找到一个Win32控件/窗口相对于其父窗口的位置?

您所在的位置:网站首页 opensubwnd的运用子窗口相对于本窗口的坐标怎么看 如何找到一个Win32控件/窗口相对于其父窗口的位置?

如何找到一个Win32控件/窗口相对于其父窗口的位置?

2024-07-01 02:38| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

Given handle of a Win32 window, I need to find position of it relative to its parent window.

I know several functions (e.g.; GetWindowRect() and GetClientRect()), but none of them explicitly return the desired coordinates.

How do I do this?

推荐答案

The solution is using the combined power of GetWindowRect() and MapWindowPoints().

GetWindowRect() retrieves the coordinates of a window relative to the entire screen area you see on your monitor. We need to convert these absolute coordinates into relative coordinates of our main window area. The MapWindowPoints() transforms the coordinates given relative to one window into relative to another. So we need a "handle" of the screen area and the handle of the parent window of the control which we are trying to find coordinates of. The screen are is a "window" in Windows terminology and it is called "Desktop". We can access the handle of Desktop by the constant HWND_DESKTOP defined in WinUser.h (including Windows.h is enough). And we can get the handle of our parent window simply by calling the Win32 function GetParent(). Now we have all the parameters required to call the MapWindowPoints() function.

RECT YourClass::GetLocalCoordinates(HWND hWnd) const { RECT Rect; GetWindowRect(hWnd, &Rect); MapWindowPoints(HWND_DESKTOP, GetParent(hWnd), (LPPOINT) &Rect, 2); return Rect; }

MapWindowPoints() is defined as:

int MapWindowPoints( _In_ HWND hWndFrom, _In_ HWND hWndTo, _Inout_ LPPOINT lpPoints, _In_ UINT cPoints );

MapWindowPoints() transform the coordinates relatively from hWndFrom to hWndTo. In our case, we do the transformation from Desktop (HWND_DESKTOP) to our parent window (GetParent(hWnd)). Therefore, the resulting RECT structure holds the relative coordinates of our child window (hWnd) relative to its parent window.

其他推荐答案

that is the solution I am using either for windows or for controls (child windows)

RECT rc; GetClientRect(hWnd,&rc); MapWindowPoints(hWnd,GetParent(hWnd),(LPPOINT)&rc,2); 其他推荐答案

I know it's been answered before, but it's much easier to just get a child window's rect in screen coordinates, get it's position (POINT ptCWPos = {rectCW.left, rectCW.top};) and use the ScreenToClient() function, that will transform the screen coordinate point to the window's client coordinate point:

PS: I know that looks like a lot more code, but most of it is fiddling with the rect position; in most cases one will actually need the rect position rather than the whole rect.

HWND hwndCW, hwndPW; // the child window hwnd // and the parent window hwnd RECT rectCW; GetWindowRect(hwndCW, &rectCW); // child window rect in screen coordinates POINT ptCWPos = { rectCW.left, rectCW.top }; ScreenToClient(hwndPW, &ptCWPos); // transforming the child window pos // from screen space to parent window space LONG iWidth, iHeight; iWidth = rectCW.right - rectCW.left; iHeight = rectCW.bottom - rectCW.top; rectCW.left = ptCWPos.x; rectCW.top = ptCWPos.y; rectCW.right = rectCW.left + iWidth; rectCW.bottom = rectCW.right + iHeight; // child window rect in parent window space


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3