15,978
社区成员
发帖
与我相关
我的任务
分享
因为记录了子窗口原始相位位置,因此不论缩放多少次,相对位置都不会发生变化
#include <Afxtempl.h>
class CWindowSizeMange
{
protected:
typedef struct _tagSizeData_t
{
HWND hWndCtrl; //窗口句柄
double fLeft, fTop, fRight, fBottom; //位置相对比例
_tagSizeData_t()
{
memset(this, 0, sizeof(*this));
}
//初始化 (记录窗口原始坐标位置)
void Init(HWND hParent, RECT rcParent, HWND hCtrl)
{
ASSERT(hParent && hCtrl);
hWndCtrl = hCtrl;
//获取控件的坐标
RECT rcCtrl;
GetWindowRect(hCtrl, &rcCtrl);
POINT ptLT = { rcCtrl.left, rcCtrl.top };
POINT ptRB = { rcCtrl.right, rcCtrl.bottom };
ScreenToClient(hParent, &ptLT);
ScreenToClient(hParent, &ptRB);
//计算相对位置
double cx = rcParent.right - rcParent.top;
double cy = rcParent.bottom - rcParent.top;
fLeft = ptLT.x / cx;
fTop = ptLT.y / cy;
fRight = ptRB.x / cx;
fBottom = ptRB.y / cy;
}
//获取窗口坐标
RECT GetRect(RECT rcParent)
{
RECT rcCtrl = {0,0,0,0};
if(hWndCtrl)
{
int cx = rcParent.right - rcParent.left;
int cy = rcParent.bottom - rcParent.top;
rcCtrl.left = (LONG)(cx * fLeft);
rcCtrl.top = (LONG)(cy * fTop);
rcCtrl.right = (LONG)(cx * fRight);
rcCtrl.bottom = (LONG)(cy * fBottom);
}
return rcCtrl;
}
}SIZEDATA, *LPSIZEDATA;
protected:
HWND m_hParentWnd;
CArray <SIZEDATA, const SIZEDATA &>m_SizeArray;
CRect m_rcOrgRect;
public:
//初始化
INT_PTR Init(HWND hParent)
{
ASSERT(hParent && IsWindow(hParent));
m_hParentWnd = hParent;
GetClientRect(hParent, &m_rcOrgRect);
m_SizeArray.RemoveAll();
HWND hWndT = NULL;
while(1)
{
hWndT = FindWindowEx(hParent, hWndT, NULL, NULL);
if(hWndT)
{
SIZEDATA m;
m.Init(hParent, m_rcOrgRect, hWndT);
m_SizeArray.Add(m);
}
else
{
break;
}
}
return m_SizeArray.GetSize();
}
//缩放窗口控件
void ResizeWindow()
{
CRect rcClient;
GetClientRect(m_hParentWnd, &rcClient);
if(m_hParentWnd && m_SizeArray.GetSize() > 0
&& rcClient != m_rcOrgRect)
{
for(INT_PTR i=0; i<m_SizeArray.GetSize(); i++)
{
SIZEDATA *pm = &m_SizeArray[i];
CRect rcCtrl = pm->GetRect(rcClient);
MoveWindow(pm->hWndCtrl,
rcCtrl.left, rcCtrl.top, rcCtrl.Width(), rcCtrl.Height(), TRUE);
}
m_rcOrgRect = rcClient;
}
}
};
//应用实例
//Dlg6Dlg.h
class CDlg6Dlg : public CDialog
{
public:
CWindowSizeMange m_WinsizeManage;
protected:
virtual BOOL OnInitDialog();
afx_msg void OnSize(UINT nType, int cx, int cy);
//Dlg6Dlg.cpp
BEGIN_MESSAGE_MAP(CDlg6Dlg, CDialog)
//{{AFX_MSG_MAP(CDlg6Dlg)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CDlg6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
//记录控件位置
m_WinsizeManage.Init(m_hWnd);
return TRUE;
}
void CDlg6Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
m_WinsizeManage.ResizeWindow();
}
---------------------
作者:zgl7903
来源:CSDN
原文:https://blog.csdn.net/zgl7903/article/details/79316100?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!
void CFMDTouchWorkshopView::ReSize()
{
float fsp[2];
POINT Newp; //获取现在对话框的大小
CRect recta;
GetClientRect(&recta); //取客户区大小
Newp.x=recta.right-recta.left;
Newp.y=recta.bottom-recta.top;
if((Newp.x == 0)&&(Newp.y == 0))
return;
fsp[0]=(float)Newp.x/m_old.x;
fsp[1]=(float)Newp.y/m_old.y;
CRect Rect;
int woc;
CPoint OldTLPoint,TLPoint; //左上角
CPoint OldBRPoint,BRPoint; //右下角
HWND hwndChild=::GetWindow(m_hWnd,GW_CHILD); //列出所有控件
while(hwndChild)
{
woc=::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowRect(Rect);
ScreenToClient(Rect);
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]);
BRPoint.y = long(OldBRPoint.y *fsp[1]);
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(woc)->MoveWindow(Rect,TRUE);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
m_old=Newp;
}