MFC -窗体自适应大小

void CserialcommunicationDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);
	// TODO: 在此处添加消息处理程序代码
	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)
	{		
		Resize();
	}
}
void CserialcommunicationDlg::Resize(void) 
{
	float fsp[2];
	POINT newPoint;//获取当前对话框大小
	CRect newRect;//获取当前对话框的坐标
	GetClientRect(&newRect);
	newPoint.x = newRect.right - newRect.left;
	newPoint.y = newRect.bottom - newRect.top;
	fsp[0] = (float)newPoint.x / Old.x;
	fsp[1] = (float)newPoint.y / Old.y;

	int woc;
	CRect rect;
	CPoint oldTLPoint, newTLPoint;//左上角
	CPoint oldBRPoint, newBRPoint;//右下角
								  //列出所有的子空间
	HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD);
	while (hwndChild) {
		woc = ::GetDlgCtrlID(hwndChild);//取得ID
		GetDlgItem(woc)->GetWindowRect(rect);
		ScreenToClient(rect);

		oldTLPoint = rect.TopLeft();
		newTLPoint.x = long(oldTLPoint.x*fsp[0]);
		newTLPoint.y = long(oldTLPoint.y*fsp[1]);
		oldBRPoint = rect.BottomRight();
		newBRPoint.x = long(oldBRPoint.x*fsp[0]);
		newBRPoint.y = long(oldBRPoint.y*fsp[1]);

		rect.SetRect(newTLPoint, newBRPoint);
		GetDlgItem(woc)->MoveWindow(rect, TRUE);
		hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
	}
	Old = newPoint;
	return;
}

初始化代码:

    CRect rect2;
	GetClientRect(&rect2); //取客户区大小   
	Old.x = rect2.right - rect2.left;
	Old.y = rect2.bottom - rect2.top;
	//int cx = GetSystemMetrics(SM_CXFULLSCREEN);
	//int cy = GetSystemMetrics(SM_CYFULLSCREEN);
	//CRect rt;
	//SystemParametersInfo(SPI_GETWORKAREA, 0, &rt, 0);
	//cy = rt.bottom;
	//MoveWindow(0, 0, cx, cy);

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值