写了很小的程序,想验证下一对话框上的一个edit的位置
一个按钮:
点击,GetWindowRect----->MoveWindow
但发现控件的位置,不是在原来的位置。
然后:GetClientRect----->MoveWindow
也不在控件最开始的位置?
为啥呢?
我们经常这样用:获取控件或窗口的位置GetWIndowRect/GetClientRect--->SetWindowPos/MoveWindow
但为什么,获取原始的位置,调用MoveWindow,位置却发生的变化?
------解决思路----------------------
移动到初始位置,等于没有移动。这两种都是对的:
RECT rcEdit = { 0 };
HWND hwndEdit = GetDlgItem(hwndDlg, IDC_EDIT);
GetWindowRect(hwndEdit, &rcEdit);
MapWindowPoint(HWND_DESKTOP, hwndDlg, (POINT *)&rcEdit, sizeof(RECT)/sizeof(POINT));
MoveWindow(hwndEdit, rcEdit.left, rcEdit.top, rcEdit.right - rcEdit.left, rcEdit.bottom - rcEdit.top, TRUE);
RECT rcEdit = { 0 };
HWND hwndEdit = GetDlgItem(hwndDlg, IDC_EDIT);
GetClientRect(hwndEdit, &rcEdit);
MapWindowPoint(hwndEdit, hwndDlg, (POINT *)&rcEdit, sizeof(RECT)/sizeof(POINT));
MoveWindow(hwndEdit, rcEdit.left, rcEdit.top, rcEdit.right - rcEdit.left, rcEdit.bottom - rcEdit.top, TRUE);
------解决思路----------------------
应该是坐标转换的问题。GetWindowRect获取的是edit控件相对屏幕左上角的位置。GetClientRect获取的是edit控件的大小,这个跟位置无关。MoveWindow使用的参数是相对于edit控件的父窗口的左上角的位置。所以你要将GetWindowRect获取的坐标转换到edit父窗口下的坐标,再移动。可以使用ScreenToClient函数。