본문 바로가기

개발/mfc

MFC, CDialog 창 크기 변화에 따라 내부 control 크기 자동 변경, sample source

반응형

퍼왔읍니다.

 

// 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.
// How to add OnSize:
// [1] add to .h:                     afx_msg void OnSize(UINT nType, int cW, int cH);
// [2] add to message map in .cpp:    ON_WM_SIZE()
// [3] add this OnSize function.
void CDlg_Log::OnSize(UINT nType, int cWidth, int cHeight)
{
   CDialog::OnSize(nType, cWidth, cHeight); // Let dialog resize itself.
 
   // get pointer to the control to be resized dynamically
   CWnd* pCtl = GetDlgItem(IDC_Log_EDIT_Log);
 
   if (!pCtl) { return; } // control may not exist yet.
 
   CRect rectCtl;                 // Allocate CRect for control's position.
   pCtl->GetWindowRect(&rectCtl); // Get control's position.
   ScreenToClient(&rectCtl);      // Convert from absolute screen coordinates to dialog-relative coordinates.
 
   // Now resize the control dynamically by calling MoveWindow
   // rectCtl.left is assumed to be the left, bottom and right margin for the control. 
   pCtl->MoveWindow(
      rectCtl.left,                               // x. remains unchanged
      rectCtl.top,                                // y. remains unchanged
      cWidth - 2 * rectCtl.left,            // w. Grow to fill horizontally
      cHeight - rectCtl.top - rectCtl.left ,  // h. Grow to fill vertically
      TRUE)
   ;
   return;
} // OnSize()

 

위에 다 설명 되어 있지만,

 

1.  해더 파일(.h)에 "afx_msg void OnSize(UINT n, int cW, int cH);" 추가

2.  .cpp 의 message map 에 "ON_WM_SIZE()"  추가

3.  .cpp 에 위 "OnSize(UINT nType, int cWidth, int cHeight)" 함수 추가

  > void CDlg_Log::OnSize(UINT nType, int cWidth, int cHeight)   줄에서 "CDlg_Log" 는 맞는 Class 로 변경

  > 크기 변경할 control 의 ID로  "CWnd* pCtl = GetDlgItem(IDC_Log_EDIT_Log);" 줄 에서

      IDC_Log_EDIT_Log  대신 바꾸어 넣는다.  

 

 

DLG_Log-240509.zip
0.03MB

반응형