MFC creates child windows and uses them

1. Create a new Dialog in the resource view dialog

2. Enter the Dialog view interface, right-click to add a class, this step is to add a class belonging to this dialog box, just like the class of the main window corresponds to the relationship of the main dialog box

3. Right-click on the initialization function interface of the added sub-dialog -> add virtual function OnInitDialog, and put the things that need to be initialized inside

For example: a new sub-dialog class named MFCListDlg

MFCListDlg.h
1  #pragma once
 2  
3  
4  // MFCListDlg dialog 
5  
6  class MFCListDlg : public CDialogEx
 7  {
 8      DECLARE_DYNAMIC(MFCListDlg)
 9  
10  public :
 11      MFCListDlg(CWnd* pParent = NULL);    // standard constructor 
12      virtual ~ MFCListDlg( );
 13  
14  // Dialog data 
15      enum { IDD = IDD_LISTTEST };
 16  
17  protected :
 18      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
19 
20     DECLARE_MESSAGE_MAP()
21 protected:
22     HICON m_hIcon;
23 public:
24     virtual BOOL OnInitDialog();
25 };
MFCListDlg.cpp
 1 // MFCListDlg.cpp : 实现文件
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "MFCTest.h"
 6 #include "MFCListDlg.h"
 7 #include "afxdialogex.h"
 8 
 9 
10 // MFCListDlg 对话框
11 
12 IMPLEMENT_DYNAMIC(MFCListDlg, CDialogEx)
13 
14 MFCListDlg::MFCListDlg(CWnd* pParent /*=NULL*/)
15     : CDialogEx(MFCListDlg::IDD, pParent)
16  {
 17      m_hIcon = AfxGetApp()-> LoadIcon(IDR_MAINFRAME);
 18  }
 19  
20 MFCListDlg::~ MFCListDlg()
 21  {
 22  }
 23  
24  void MFCListDlg::DoDataExchange(CDataExchange* pDX )
 25  {
 26 CDialogEx      ::DoDataExchange( pDX);
 27  }
 28  
29  
30  BEGIN_MESSAGE_MAP(MFCListDlg, CDialogEx)
 31  END_MESSAGE_MAP()
 32  
33  
34  // MFCListDlg message handler -- overloaded dialog initialization function 
35  BOOL MFCListDlg::OnInitDialog()
36  {
 37      CDialogEx::OnInitDialog();
 38  
39      SetIcon(m_hIcon, TRUE);             // Set the big icon 
40      SetIcon(m_hIcon, FALSE);         // Set the small icon 
41  
42      CenterWindow(); // Display the window in the front central 
43  
44      return TRUE;
 45 }

4. Use sub-dialogs in the main dialog

①Create a sub-dialog object in the class of the main dialog

MFCListDlg m_ListDlg;

②Add the create window statement in the OnInitDialog initialization function of the main dialog box

m_ListDlg.Create(MFCListDlg::IDD, this);

③Use statements where you need to use them

m_ListDlg.ShowWindow(SW_SHOW);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325251476&siteId=291194637