我希望能够编写如下代码:
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 = …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2005(以及C++中的win32 API),我有一个使用内置对话框编辑器设计的窗口.
在运行时,我希望能够使用它CreateWindow()来添加新的控件.这是有效的,但是我创建的控件看起来都很难看.出于具体的目的,我添加的控件是一个TabControl,当我使用内置的对话框编辑器添加它时,选项卡标签中的文本看起来不错.当我在运行时创建它时CreateWindow(),文本是大而粗的,看起来不合适.
我发现在MSDN上使用Windows XP视觉样式,它似乎描述了正确区域中的内容,但是当我按照其中的说明(嵌入清单)时,动态创建的控件似乎是比使用的更新的样式.对话框编辑器(选项卡控件的背景颜色更浅).
我也发现了这个SetWindowTheme()功能.我不太确定如何使用这个函数...我希望我可以GetWindowTheme()在窗口上使用,然后将其结果传递SetWindowTheme()给它们看起来一样,但是GetWindowTheme()返回HTHEME,我不知道你甚至可以用这些做什么......你绝对不能把它们传递给它们SetWindowTheme().