MFC,在CDialog应用程序中调整控件大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC,在CDialog应用程序中调整控件大小相关的知识,希望对你有一定的参考价值。

This OnSize function resizes one large control in a dialog.
The one control grows horizontally and vertically to fill the dialog. It's position remains unchanged.
Other controls (buttons etc) would typically be above the one resizable control.
  1. // This OnSize function resizes one large control in a dialog.
  2. // The one control grows horizontally and vertically to fill the dialog. It's position remains unchanged.
  3. // Other controls (buttons etc) would typically be above the one resizable control.
  4. // How to add OnSize:
  5. // [1] add to .h: afx_msg void OnSize(UINT nType, int cx, int cy);
  6. // [2] add to message map in .cpp: ON_WM_SIZE()
  7. // [3] add this OnSize function.
  8. void CMyDlg::OnSize(UINT nType, int formWidthArg, int formHeightArg)
  9. {
  10. CDialog::OnSize(nType, formWidthArg, formHeightArg); // Let dialog resize itself.
  11.  
  12. // http://www.codersource.net/mfc_resize_controls.html
  13. // http://wwwusers.brookes.ac.uk/p0071643/resize.htm
  14.  
  15. // get pointer to the control to be resized dynamically
  16. CWnd* pCtl = GetDlgItem(IDC_MSFLEXGRID1);
  17.  
  18. if (!pCtl) { return; } // control may not exist yet.
  19.  
  20. CRect rectCtl; // Allocate CRect for control's position.
  21. pCtl->GetWindowRect(&rectCtl); // Get control's position.
  22. ScreenToClient(&rectCtl); // Convert from absolute screen coordinates to dialog-relative coordinates.
  23.  
  24. // Now resize the control dynamically by calling MoveWindow
  25. // rectCtl.left is assumed to be the left, bottom and right margin for the control.
  26. pCtl->MoveWindow(
  27. rectCtl.left, // x. remains unchanged
  28. rectCtl.top, // y. remains unchanged
  29. formWidthArg - 2 * rectCtl.left, // w. Grow to fill horizontally
  30. formHeightArg - rectCtl.top - rectCtl.left, // h. Grow to fill vertically
  31. TRUE)
  32. ;
  33.  
  34. return;
  35. } // OnSize()

以上是关于MFC,在CDialog应用程序中调整控件大小的主要内容,如果未能解决你的问题,请参考以下文章

C++ MFC - 在 CDialog::OnSize 事件 (GetWindowRect) 上没有引发运行时错误的代码执行失败

MFC中获取dialog窗体的宽度及大小等信息

在 MFC 应用程序中动态创建控件

在 MFC 中调整控件大小

如何在 MFC CDialog 类中自动删除事件处理程序?

MFC CDialog::Create 失败