VC++ MFC控件随对话框放大缩小功能实现

 

最近在调试MFC的对话框显示界面,用于显示抓捕的波形,非全屏状态显示下,在数据量比较大的时候,感觉太紧凑,少了全屏有感觉空旷,所以还是有必要做一个灵活大小的对话框。

1、xxDlg.h文件中定义对话框原始位置信息参数

public:
	POINT oldPiont;

2、xxDlg.cpp文件中获取对话框原始位置信息


// CWaveDisplayDlg 消息处理程序
BOOL CWaveDisplayDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	//获取显示的对话框大小
	CRect rect;
	GetClientRect(&rect);
	oldPiont.x = rect.right - rect.left;
	oldPiont.y = rect.bottom - rect.top;
    //自己的代码
}

3、添加WM_SIZE消息处理函数OnSize()

// 实现
protected:
	afx_msg void OnSize(UINT nType, int cx, int cy);
	
public:
	POINT oldPiont;
	void Resize(void);
BEGIN_MESSAGE_MAP(CmyApplicationDlg, CDialogEx)
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
void CmyApplicationDlg::OnSize(UINT nType, int cx, int cy) { 
	//窗体大小发生变动。处理函数resize
	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)
	{
		Resize();
	}
}
void CmyApplicationDlg::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 / oldPiont.x;
	fsp[1] = (float)newPoint.y / oldPiont.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);
	}
	oldPiont = newPoint;
	return;
}

源码地址:点我下载实例源码

 

 

 

    原文作者:jobschu
    原文地址: https://blog.csdn.net/u014159143/article/details/86598332
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞