//.h
private:
typedef struct Rate//控件位置占比,ID,
{
CWnd *pWndItem;
CRect rc;
UINT uid;
double d_l;
double d_t;
double d_r;
double d_b;
Rate()
{
pWndItem=NULL;
rc.SetRect(0,0,0,0);
uid=0;
d_l=0.0;
d_t=0.0;
d_r=0.0;
d_b=0.0;
}
};
vector<Rate> itemRateVtr;
double cW;
double cH;
CRect rcDlg;
void initItemRect();
public:
virtual BOOL OnInitDialog();
/*******************************************************************************/
//.cpp
BOOL CXXDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
GetClientRect(rcDlg);
cW=(double)rcDlg.Width();
cH=(double)rcDlg.Height();
CWnd *pWndChild=GetWindow(GW_CHILD);
while (pWndChild)
{
Rate mRate;
mRate.pWndItem=pWndChild;
mRate.uid=GetWindowLong(pWndChild->GetSafeHwnd(),GWL_ID);
pWndChild->GetWindowRect(mRate.rc);
ScreenToClient(mRate.rc);
mRate.d_l=mRate.rc.left/ cW;
mRate.d_t=mRate.rc.top/ cH;
mRate.d_r=mRate.rc.right/ cW;
mRate.d_b=mRate.rc.bottom/ cH;
itemRateVtr.push_back(mRate);
pWndChild=pWndChild->GetNextWindow();
}
}
//改变控件位置,大小
void CXXDlg::initItemRect()
{
this->GetClientRect(rcDlg);
cW=(double)rcDlg.Width();
cH=(double)rcDlg.Height();
for (int i = 0; i < (int)itemRateVtr.size(); i++)
{
int left =(int)(itemRateVtr[i].d_l * cW);
int top =(int)(itemRateVtr[i].d_t * cH);
int right =(int)(itemRateVtr[i].d_r * cW);
int bottom=(int)(itemRateVtr[i].d_b * cH);
itemRateVtr[i].rc.SetRect(left,top,right,bottom);
itemRateVtr[i].pWndItem->MoveWindow(itemRateVtr[i].rc);
}
}
void CXXDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
if(SIZE_MINIMIZED==nType||SIZE_MAXHIDE==nType) return;
initItemRect();
}
对话框改变大小保持控件比例
于 2024-07-30 16:09:21 首次发布