VC dynamic menu item generation method

  • 2020-04-02 02:35:12
  • OfStack

This example is a class file written using VC that dynamically generates menu items. Here is the core code, which is fully annotated and relatively simple to understand.

The main function code is as follows:


#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//The CAboutDlg dialog box for the application about menu item

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

//Dialog data
 enum { IDD = IDD_ABOUTBOX };

 protected:
 virtual void DoDataExchange(CDataExchange* pDX);  //DDX/DDV support

//implementation
protected:
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

//CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent )
 : CDialog(CTestDlg::IDD, pParent)
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 //}}AFX_MSG_MAP
 ON_BN_CLICKED(IDC_BTNADDSYSTEMMENU, OnBnClickedBtnaddsystemmenu)
END_MESSAGE_MAP()

//CTestDlg message handler
BOOL CTestDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 //Will be about...  "menu item added to system menu.

 //IDM_ABOUTBOX must be within the scope of the system command.
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

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

 //Set the icon for this dialog. When the main window of the application is not a dialog box, the frame will automatically
 //Do this
 SetIcon(m_hIcon, TRUE);  //Set the big icon
 SetIcon(m_hIcon, FALSE); //Set small icon

 //TODO: add additional initialization code here
 
 return TRUE; //Returns TRUE unless the focus of the control is set
}

void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if(nID==ID_FILE_OPEN)
 {
 MessageBox(" Test the add system menu function "," message ",MB_OK);
 }
 else if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
 CAboutDlg dlgAbout;
 dlgAbout.DoModal();
 }
 else
 {
 CDialog::OnSysCommand(nID, lParam);
 }
}

//If you add a minimize button to the dialog, you need the following code
//To draw the icon. For MFC applications that use the document/view model,
//This is done automatically by the framework.

void CTestDlg::OnPaint() 
{
 if (IsIconic())
 {
 CPaintDC dc(this); //Device context for drawing

 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

 //Center the icon in the working rectangle
 int cxIcon = GetSystemMetrics(SM_CXICON);
 int cyIcon = GetSystemMetrics(SM_CYICON);
 CRect rect;
 GetClientRect(&rect);
 int x = (rect.Width() - cxIcon + 1) / 2;
 int y = (rect.Height() - cyIcon + 1) / 2;

 //Draw the icon
 dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
 CDialog::OnPaint();
 }
}

//The system calls this function to get the cursor displayed when the user drags the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}

void CTestDlg::OnBnClickedBtnaddsystemmenu()
{
 CMenu *pMenu=GetSystemMenu(FALSE);
 pMenu->AppendMenu(0,ID_FILE_OPEN," Display prompt message ");
 MessageBox(" Added system menu operation successfully! "," message ",MB_OK);
}

Related articles: