MFC Dialog

Adding Files for the Dialog

  1. With the shortcut Ctrl + Shift + A, open the Add New Item dialog. Alternatively, right-click the Solution Name (WinSock) in the Solution Explorer and select Add > New Item.
  2. In the dialog, select MFC > MFC Class and set the name to CMainDlg, then click Add.

  3. Select CDialog as the base class.
  4. Remove the character "C" from the beginning of both the .h and .cpp files.
  5. Set the Dialog ID as IDD_MAIN for further operations and click OK.

  6. Check that both MainDlg.cpp and MainDlg.h files appear in the Solution Explorer, and ensure that IDD_MAIN is listed under Dialog in the Resource View tab.

Editing Files for the Dialog

  1. Open MainDlg.h, then comment out line 16 and 18 to activate the IDD macro.
    #pragma once
    #include "afxdialogex.h"
    
    
    // CMainDlg dialog
    
    class CMainDlg : public CDialog
    {
    	DECLARE_DYNAMIC(CMainDlg)
    
    public:
    	CMainDlg(CWnd* pParent = nullptr);   // standard constructor
    	virtual ~CMainDlg();
    
    // Dialog Data
    //#ifdef AFX_DESIGN_TIME
    	enum { IDD = IDD_MAIN };
    //#endif
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    	DECLARE_MESSAGE_MAP()
    };
  2. With the shortcut Alt > P > Z , or by selecting Project (P) > Class Wizard, open the Class Wizard.
  3. Select CWinSockView as the Class name, go to the Virtual Functions tab, type "init", and select OnInitialUpdate from the Virtual function list, click Add function, then Apply. class wizard to add a function
  4. Open WinSockView.h, declare the class name "CMainDlg" at line 6, and declare its pointer-instance at lines 20-21.
    
    // WinSockView.h : interface of the CWinSockView class
    //
    
    #pragma once
    
    class CMainDlg;
    
    
    class CWinSockView : public CView
    {
    // ...
    
    // Implementation
    public:
    	virtual ~CWinSockView();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    private:
        CMainDlg* m_pMainDlg;
    
    
    // Generated message map functions
    protected:
    	DECLARE_MESSAGE_MAP()
    public:
    	virtual void OnInitialUpdate();
    };
  5. Open WinSockView.cpp, and add the necessary code inside the OnInitialUpdate() function like at lines 8-13.
    // CWinSockView message handlers
    
    
    void CWinSockView::OnInitialUpdate()
    {
    	CView::OnInitialUpdate();
    
    	if (m_pMainDlg == nullptr)
    {
    	m_pMainDlg = new CMainDlg();
    	m_pMainDlg->Create(CMainDlg::IDD, this);
    	m_pMainDlg->ShowWindow(SW_NORMAL);
    }
    }

Compiling and Running the dialog

  1. After compiling, you should see two windows.
    • The left window is the original view.
    • The right window is the dialog created from MainDlg.cpp, MainDlg.h, and IDD_MAIN.

Adding Controls to the Dialog

  1. You can add controls like buttons and text boxes from the Toolbox. Here's an example for manipulating a socket.