控件随着父窗口大小改变而改变位置或大小
1.获得控件句柄
HWND hEdit = GetDlgItem(m_hWnd, 10001); //10001是编辑框控件的ID
2.获得控件的屏幕布矩形坐标
RECT rcEdit;
GetWindowRect(hEdit, &rcEdit);
3.把屏幕坐标转为父窗口的客服区坐标
先把矩形坐标转为点坐标再转换
POINT ptLT = { rcEdit.left,rcEdit.top };
POINT ptRB = { rcEdit.right,rcEdit.bottom };
ScreenToClient(m_hWnd, &ptLT);
ScreenToClient(m_hWnd, &ptRB);
4.获得父窗口的客服区坐标
RECT rcParent;
GetClientRect(m_hWnd, &rcParent);
5.设置控件的位置或大小
我这只是改变控件右边距到父窗口右边距10的距离,所以只修改控件右边距值,
如想改别的,修改相对应的值就好.
rcEdit.left = ptLT.x; rcEdit.top = ptLT.y; rcEdit.bottom = ptRB.y; rcEdit.right = rcParent.right - 10; MoveWindow(hEdit, rcEdit.left, rcEdit.top, rcEdit.right - rcEdit.left, rcEdit.bottom - rcEdit.top, TRUE);
6.效果:
1.修改前
2.修改后
标签:控件,right,hWnd,top,改变,坐标,rcEdit,大小 From: https://www.cnblogs.com/greenleaf1976/p/16991747.html