c - 如何将对话框位置坐标复制到另一个对话框中?

标签 c winapi dialog items

我有几个独特的按钮,我一次只想显示其中一个。我希望它们居中,所以我将第一个按钮置于对话框的中心。如果我想显示第三个按钮,我想给它第一个按钮坐标并隐藏第一个按钮。

如何复制一个按钮坐标并将另一个按钮坐标设置为复制的值?

例。可以说我有...

PB_ONE
PB_TWO

如何获取 PB_ONE 的坐标并将 PB_TWO 的坐标设置为 PB_ONE?

RECT rcButton;

GetWindowRect(GetDlgItem(hDlg, PB_ONE), &rcButton);

上面的代码获取了我想要从中复制坐标的对话框项。是否有一个简单的命令可以将另一个对话框按钮设置为该对话框坐标?

类似 SetDlgItem() 的东西?

更新了我根据答案尝试的新代码

GetWindowRect(GetDlgItem(hDlg, PB_ONE), &rcButton);
ClientToScreen(hDlg, &p);
OffsetRect(&rcButton, -p.x, -p.y);
SetWindowPos(GetDlgItem(hDlg, PB_TWO), 0, rcButton.left, rcButton.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
ShowWindow(GetDlgItem(hDlg, PB_TWO), SW_SHOW);

当前必须将 rcButton.left 和 rcButton.top 替换为 p.x 和 rcButton.top 的硬值,以使按钮在对话框屏幕上定位。

这会在 SetWindowPos 中返回错误,其中参数 3 无法将 LONG * 转换为 INT。

最佳答案

GetWindowRect 给出屏幕坐标中的矩形。您可以使用 ScreenToClient(HWND hWnd, LPPOINT lpPoint) 将其转换为客户端坐标。


编辑:

RECT rcButton;
HWND hbutton1 = GetDlgItem(hDlg, PB_ONE);
HWND hbutton2 = GetDlgItem(hDlg, PB_TWO);

//if(!hbutton1 || !hbutton2) {error...}

GetWindowRect(hbutton1, &rcButton);

//Test
char buf[50];
sprintf(buf, "%d %d", rcButton.left, rcButton.top);
MessageBoxA(0, buf, "screen coord", 0);

//Note, this will only convert the top-left corner, not right-bottom corner
//but that's okay because we only want top-left corner in this case
ScreenToClient(hDlg, (POINT*)&rcButton);

//Test
sprintf(buf, "%d %d", rcButton.left, rcButton.top);
MessageBoxA(0, buf, "client coord", 0);

ShowWindow(hbutton1, SW_HIDE);
SetWindowPos(hbutton2, 0, rcButton.left, rcButton.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);


一个稍微简单的方法是使用 ClientToScreen(HWND hWnd, LPPOINT lpPoint) 如下:

RECT rcButton;
GetWindowRect(GetDlgItem(hDlg, PB_ONE), &rcButton);

POINT p{ 0 };
ClientToScreen(hDlg, &p);
//p is now (0,0) of parent window in screen coordinates
OffsetRect(&rcButton, -p.x, -p.y);

rcButton 现在是相对于父窗口左上角的坐标。您可以在 SetWindowPos 中使用它。

关于c - 如何将对话框位置坐标复制到另一个对话框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800107/

相关文章:

java - 如何在不使用任何 "ButtonType"控件的情况下在 JavaFx 中创建自定义对话框?

c - 返回包含该行从左到右学生高度的数组

c++ - FILE_NOTIFY_INFORMATION 不支持 Utf-8 文件名

c - 为什么 strcpy 向变量复制的字符比预期的多?

c - 为什么 setTimer 不起作用?

c - 将 40 个套接字绑定(bind)到 40 个不同的 IP 地址

具有绝对定位子项的 jQuery UI 对话框部分隐藏了溢出的子项

android - 一旦我显示带有 EditText 的对话框,如何使软键盘出现?

c语言: printf help

c - 我将如何创建一个每次迭代读取一个字符并存储该字符的 while 循环?