MFC中視窗隨最小化最大化按鈕最大最小化
轉載於:https://blog.csdn.net/Bettyshasha/article/details/51603752
1.選中對話方塊,右鍵-》屬性-》訊息,新增WM_SIZE函式。
2.在對話方塊類標頭檔案.h裡面新增:
void ReSize();
POINT old;
3.在初始化函式OnInitDialog()中新增;
GetClientRect(&m_rect);
old.x = m_rect.right - m_rect.left;
old.y = m_rect.bottom - m_rect.top;
//ShowWindow(SW_SHOWMAXIMIZED);//初始化為最大視窗
4.Resize函式實現:
void Copencv_mfc_showDlg::ReSize()
{
float fsp[2];
POINT Newp; //獲取現在對話方塊的大小
CRect recta;
GetClientRect(&recta); //取客戶區大小
Newp.x = recta.right - recta.left;
Newp.y = recta.bottom - recta.top;
fsp[0] = (float)Newp.x / old.x;
fsp[1] = (float)Newp.y / 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);
}
old = Newp;
}
5.在OnSize函式中新增如下程式碼:
if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED){
ReSize();
}
最大最小化視窗時,控制元件就會隨著改變大小了。