15,979
社区成员
发帖
与我相关
我的任务
分享
if (pWnd)
{
CRect rect;
pWnd->GetWindowRect(&rect); //获取控件变化前的大小
ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
rect.left=rect.left*cx/m_rect.Width();//调整控件大小 ,cx/m_rect.Width()为对话框在横向的变化比例
rect.right=rect.right*cx/m_rect.Width(); //cx存储的是变化后的宽度,cy存储的是变化后的高度
rect.top=rect.top*cy/m_rect.Height(); //m_rect.height()表示的是变化前主窗体的高度
rect.bottom=rect.bottom*cy/m_rect.Height();
pWnd->MoveWindow(rect);//设置控件大小
}
#define USE_CLIENT
void CScalerDlg::GetCtrlClientRects()
{
int iCount = 0;
// right and bottom not used
CWnd* pWnd = GetTopWindow();// child window
CRect rc;
while(pWnd != NULL)
{
pWnd->GetWindowRect(&rc);
#ifdef USE_CLIENT
ScreenToClient(&rc); // Screen dlg's window to dlg's Client.
#endif
m_CtrlRcOrig[iCount++]=rc;
pWnd = pWnd->GetNextWindow();
}
}
//
BOOL CScalerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// get dlg orignal W H
CRect rc;
GetClientRect(&rc);
m_dlgW=rc.Width();
m_dlgH=rc.Height();
// afxDump << m_dlgW << ";" << m_dlgH << "\n";// 720; 450
GetWindowRect(&rc);// (L 0, T 0, R 730, B 488)
// afxDump << rc << "\n";
m_s2cOffsetX=(rc.Width()-m_dlgW)/2;
m_s2cOffsetY=rc.Height()-m_s2cOffsetX-m_dlgH;
// afxDump << m_s2cOffsetX << ";" << m_s2cOffsetY << "\n";// 5;33
// 5=Border; 33=caption height=28+border=5
m_pStatic= new CStatic;
m_pStatic->Create(TEXT("Hello"),WS_CHILD|WS_VISIBLE|SS_LEFT, CRect(10,10,200,100),this,0x0201);
m_pStatic->ModifyStyle(0,SS_OWNERDRAW);
m_pStatic->ShowWindow(SW_SHOW);//TRUE);
// init rects
GetCtrlClientRects();
//
return TRUE; // return TRUE unless you set the focus to a control
}
//
void CScalerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
double xRate=(double)cx/(double)m_dlgW;
double yRate=(double)cy/(double)m_dlgH;
int iCount=0;
CRect rcNow(0);
CWnd* pWnd = GetTopWindow();
while(pWnd != NULL)
{
#ifndef USE_CLIENT
rcNow.left =(int)((m_CtrlRcOrig[iCount].left-m_s2cOffsetX)*xRate);
rcNow.right =(int)((m_CtrlRcOrig[iCount].right-m_s2cOffsetX)*xRate);
rcNow.top =(int)((m_CtrlRcOrig[iCount].top-m_s2cOffsetY)*yRate);
rcNow.bottom=(int)((m_CtrlRcOrig[iCount].bottom-m_s2cOffsetY)*yRate);
#else // has ScreenToClient(&rc); // Screen dlg's window to dlg's Client.
rcNow.left = (int)(m_CtrlRcOrig[iCount].left*xRate);
rcNow.right = (int)(m_CtrlRcOrig[iCount].right*xRate);
rcNow.top = (int)(m_CtrlRcOrig[iCount].top*yRate);
rcNow.bottom= (int)(m_CtrlRcOrig[iCount].bottom*yRate);
#endif
pWnd->MoveWindow(&rcNow,TRUE);
pWnd = pWnd->GetNextWindow();
iCount++;
}
}
// in .h
protected:
HICON m_hIcon;
int m_dlgW;
int m_dlgH;
int m_s2cOffsetX;
int m_s2cOffsetY;
CRect m_CtrlRcOrig[10];
CStatic *m_pStatic;
init:
//
CRect rc;
GetClientRect(&rc);
m_dlgW=rc.Width();
m_dlgH=rc.Height();
afxDump << m_dlgW << ";" << m_dlgH << "\n";// 720;450
m_Button1.GetWindowRect(m_btRectOrig);
afxDump << m_btRectOrig << "\n";// (L 244, T 148, R 460, B 240)
ScreenToClient(&m_btRectOrig); //
afxDump << m_btRectOrig << "\n";// (L 239, T 115, R 455, B 207)
。。。。。
void CScalerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
double xRate=(double)cx/(double)m_dlgW;
double yRate=(double)cy/(double)m_dlgH;
// afxDump << xRate << ";" << xRate << "\n";
if(::IsWindow(m_Button1.m_hWnd))
{
CRect rcNow;
rcNow.left=(int)(m_btRectOrig.left*xRate);
rcNow.right=(int)(m_btRectOrig.right*xRate);
rcNow.top=(int)(m_btRectOrig.top*yRate);
rcNow.bottom=(int)(m_btRectOrig.bottom*yRate);
afxDump << rcNow << "\n";
//
m_Button1.MoveWindow(&rcNow);
}
}