MFC 控件随窗口同步变大的实现

来源:互联网 发布:佳能mp258清零软件 编辑:程序博客网 时间:2024/06/11 21:10

首先建立一个基于对话框的MFC程序

打开类向导,为该对话框选中消息:WM_SIZE,双击值他会生成这样一个函数:OnSize.双击这个函数.在函数体里面添加如下代码:

CDialogEx::OnSize(nType, cx, cy);// TODO: 在此处添加消息处理程序代码float fsp[2];POINT Newp; //获取现在对话框的大小(VS2005中是CPOINT)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);//取得IDGetDlgItem(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]); //高度不可读的控件(如:combBox),不要改变此值.Rect.SetRect(TLPoint,BRPoint); GetDlgItem(woc)->MoveWindow(Rect,TRUE);hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT); }Old=Newp; 


其中这个这个Old是一个全局变量.你在"工程名Dlg.cpp"文件的这个地方添加:

// testsizeDlg.cpp : 实现文件

//

#include "stdafx.h"

#include "testsize.h"

#include "testsizeDlg.h"

#include "afxdialogex.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

CPoint Old;//存放对话框的宽和高

 

原创粉丝点击