⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmenudlg.cpp

📁 一个在基于对话框上添加菜单的程序
💻 CPP
字号:
// TestMenuDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestMenu.h"
#include "TestMenuDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int Sel;
/////////////////////////////////////////////////////////////////////////////
// CTestMenuDlg dialog

CTestMenuDlg::CTestMenuDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestMenuDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestMenuDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestMenuDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestMenuDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestMenuDlg, CDialog)
	//{{AFX_MSG_MAP(CTestMenuDlg)
	ON_UPDATE_COMMAND_UI(ID_Menu1, OnUpdateMenu1)
	ON_COMMAND(ID_Menu1, OnMenu1)
	ON_COMMAND(ID_Menu2, OnMenu2)
	ON_UPDATE_COMMAND_UI(ID_Menu2, OnUpdateMenu2)

	ON_WM_INITMENUPOPUP()
	
	//}}AFX_MSG_MAP

	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestMenuDlg message handlers

BOOL CTestMenuDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	Sel = 1;
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	CCeCommandBar		m_wndmenu;
	HINSTANCE g_hInst = ::AfxGetInstanceHandle();
	HWND m_hWndCB;
	m_hWndCB = ::CommandBar_Create(g_hInst, m_hWnd, 1);
	if (m_hWndCB != NULL)
	{		
		//Add a menu bar
		::CommandBar_InsertMenubar(m_hWndCB, g_hInst, IDR_MENU1, 0);		
		::CommandBar_AddAdornments(m_hWndCB, 0, 0);
		RECT rcCmdBar;
		::GetClientRect(m_hWndCB, &rcCmdBar);
		
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CTestMenuDlg::OnUpdateMenu1(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(Sel == 1)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

void CTestMenuDlg::OnMenu1() 
{
	// TODO: Add your command handler code here
	Sel = 1;
	AfxMessageBox(L"你点击了大图标!");

}

void CTestMenuDlg::OnMenu2() 
{
	// TODO: Add your command handler code here
	Sel = 2;
	AfxMessageBox(L"你点击了小图标!");
}

void CTestMenuDlg::OnUpdateMenu2(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	if(Sel == 2)
			pCmdUI->SetCheck(1);
		else
			pCmdUI->SetCheck(0);
	
}

void CTestMenuDlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
{
	ASSERT(pPopupMenu != NULL);
    // Check the enabled state of various menu items.
	
    CCmdUI state;
    state.m_pMenu = pPopupMenu;
    ASSERT(state.m_pOther == NULL);
    ASSERT(state.m_pParentMenu == NULL);
	
    // Determine if menu is popup in top-level menu and set m_pOther to
    // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
    HMENU hParentMenu;
    if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
        state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.
    else if ((hParentMenu = GetMenu()->GetSafeHmenu()) != NULL)
    {
        CWnd* pParent = this;
		// Child windows don't have menus--need to go to the top!
        if (pParent != NULL &&
			(hParentMenu = GetMenu()->GetSafeHmenu()) != NULL)
        {
		int nIndexMax =  GetMenu()->GetMenuItemCount();
			for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
			{
				if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
				{
					// When popup is found, m_pParentMenu is containing menu.
					state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
					break;
				}
			}
        }
    }
	
    state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
    for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
	state.m_nIndex++)
    {
        state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
        if (state.m_nID == 0)
			continue; // Menu separator or invalid cmd - ignore it.
		
        ASSERT(state.m_pOther == NULL);
        ASSERT(state.m_pMenu != NULL);
        if (state.m_nID == (UINT)-1)
        {
			// Possibly a popup menu, route to first item of that popup.
			state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
			if (state.m_pSubMenu == NULL ||
				(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
				state.m_nID == (UINT)-1)
			{
				continue;       // First item of popup can't be routed to.
			}
			state.DoUpdate(this, TRUE);   // Popups are never auto disabled.
        }
        else
        {
			// Normal menu item.
			// Auto enable/disable if frame window has m_bAutoMenuEnable
			// set and command is _not_ a system command.
			state.m_pSubMenu = NULL;
			state.DoUpdate(this, FALSE);
        }
		
        // Adjust for menu deletions and additions.
        UINT nCount = pPopupMenu->GetMenuItemCount();
        if (nCount < state.m_nIndexMax)
        {
			state.m_nIndex -= (state.m_nIndexMax - nCount);
			while (state.m_nIndex < nCount &&
				pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
			{
				state.m_nIndex++;
			}
        }
        state.m_nIndexMax = nCount;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -