遍历对话框中的控件

在主对话框的cpp文件中 找到OnInitDialog()函数


我自己对话框的命名下额外代码处添加的的遍历代码如下

BOOL CMFCApplication2Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码
	CWnd *pWnd = GetTopWindow();
	while (pWnd != NULL)
	{
		int  i = pWnd->GetDlgCtrlID();
		CString str;
		str.Format(L"ID是:%d", i);
		pWnd->SetWindowTextW(str);
		pWnd = pWnd->GetNextWindow();
	}
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于MFC的对话框和所有控件以及所有控件上的字体进行等比例放缩的示例代码: ``` void CMyDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 获取对话框的原始尺寸和所有控件的原始位置和大小 GetClientRect(&m_rectDlg); CWnd* pWnd = GetWindow(GW_CHILD); while (pWnd) { CRect rect; pWnd->GetWindowRect(&rect); ScreenToClient(&rect); m_mapCtrlRect[pWnd] = rect; m_mapCtrlFont[pWnd] = new CFont; m_mapCtrlFont[pWnd]->CreateFontIndirect(&pWnd->GetFont()->lf); pWnd = pWnd->GetNextWindow(GW_HWNDNEXT); } } void CMyDlg::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); // 计算变化比例 double scalex = (double)cx / m_rectDlg.Width(); double scaley = (double)cy / m_rectDlg.Height(); // 设置所有控件的新位置和大小 for (auto it = m_mapCtrlRect.begin(); it != m_mapCtrlRect.end(); ++it) { CWnd* pWnd = it->first; CRect rect = it->second; int x = (int)(rect.left * scalex); int y = (int)(rect.top * scaley); int w = (int)(rect.Width() * scalex); int h = (int)(rect.Height() * scaley); pWnd->SetWindowPos(NULL, x, y, w, h, SWP_NOZORDER | SWP_NOREDRAW); } } void CMyDlg::OnSetFont(CFont* pFont) { CDialogEx::OnSetFont(pFont); // 设置所有控件的新字体 for (auto it = m_mapCtrlFont.begin(); it != m_mapCtrlFont.end(); ++it) { CWnd* pWnd = it->first; CFont* pOrigFont = it->second; LOGFONT lf; pOrigFont->GetLogFont(&lf); pFont->GetLogFont(&lf); it->second->DeleteObject(); it->second->CreateFontIndirect(&lf); pWnd->SetFont(it->second); } } void CMyDlg::OnDestroy() { // 释放所有控件的新字体 for (auto it = m_mapCtrlFont.begin(); it != m_mapCtrlFont.end(); ++it) { delete it->second; } CDialogEx::OnDestroy(); } ``` 在这个示例,我们通过获取对话框的原始尺寸和所有控件的原始位置和大小,在对话框的OnInitDialog()函数保存了这些信息和原始字体。 在对话框的OnSize()函数,我们计算了对话框的变化比例,然后遍历所有控件,根据比例计算了控件的新位置和大小,并将其设置到相应的控件上。 在对话框的OnSetFont()函数,我们获取了新的字体并设置到所有控件上。 在对话框的OnDestroy()函数,我们释放了所有控件的新字体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值