2017-10-17 58 views
1

我有一些独特的按钮,我只想一次显示其中的一个。我希望它们居中,所以我有第一个按钮居中对话框。如果我想显示第三个按钮,我想给它第一个按钮坐标并隐藏第一个按钮。如何将对话框位置坐标复制到另一个对话框中?

如何复制按钮坐标并将其他按钮坐标设置为复制值?

Ex。可以说我有...

PB_ONE 
PB_TWO 

我怎样才能抓住PB_ONE的坐标,并设定PB_TWO的坐标PB_ONE?

RECT rcButton; 

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

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

像SetDlgItem()?

用新的代码已更新我尝试基于OFF答案

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); 

有电流,PX和rcButton.top硬值替换rcButton.left和rcButton.top得到按钮位置上对话框。

这将在SetWindowPos中返回一个错误,其中参数3不能将LONG *转换为INT。

+0

确保'p.x = 0; p.y = 0;'在调用'ClientToScreen'之前。同时隐藏'PB_ONE'。你说你已经将'&rcButton.left'改为'rcButton.left'? –

+0

是的,我改变了他们。但是,这些坐标始终不在屏幕上。我必须使用px而不是rcButton.left才能真正获得正确的按钮位置 – user3622460

回答

3

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中使用。

+0

这给我一个SetWindowPos调用的错误 - 参数3不能设置LONG *为INT – user3622460

+0

我的错 - 我正在使用& - 代码工程精细。该按钮不在屏幕上,但我假设它位于0,0? – user3622460

+0

尝试*编辑*部分。您应该使用调试断点或'OutputDebugString'来查看您获得的值。如果可能,将对话框移动到桌面屏幕的左上角,以便屏幕和客户端坐标大致相同(仅用于测试)。 –

相关问题