使控件和MFC窗口一起最大最小化

  • Post author:
  • Post category:其他


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();
		}

最大最小化窗口时,控件就会随着改变大小了。



版权声明:本文为Bettyshasha原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。