如何设置按钮控件点击弹出新菜单栏
函数介绍:CButtonST应用
DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint)
{
HINSTANCE hInstResource = NULL;
// Destroy any previous menu
if (m_hMenu)
{
::DestroyMenu(m_hMenu);
m_hMenu = NULL;
m_hParentWndMenu = NULL;
m_bMenuDisplayed = FALSE;
} // if
// Load menu
if (nMenu)
{
// Find correct resource handle
hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nMenu), RT_MENU);
// Load menu resource
m_hMenu = ::LoadMenu(hInstResource, MAKEINTRESOURCE(nMenu));
m_hParentWndMenu = hParentWnd;
// If something wrong
if (m_hMenu == NULL) return BTNST_INVALIDRESOURCE;
} // if
// Repaint the button
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetMenu
DWORD CButtonST::SetMenu(UINT nMenu, HWND hParentWnd, BOOL bWinXPStyle, UINT nToolbarID, CSize sizeToolbarIcon, COLORREF crToolbarBk, BOOL bRepaint) //除前两个参数,其他参数都有初始值
{
BOOL bRetValue = FALSE;
// Destroy any previous menu
if (m_menuPopup.m_hMenu)
{
m_menuPopup.DestroyMenu();
m_hParentWndMenu = NULL;
m_bMenuDisplayed = FALSE;
} // if
// Load menu
if (nMenu)
{
m_menuPopup.SetMenuDrawMode(bWinXPStyle);
// Load menu
bRetValue = m_menuPopup.LoadMenu(nMenu);
// If something wrong
if (bRetValue == FALSE) return BTNST_INVALIDRESOURCE;
// Load toolbar
if (nToolbarID)
{
m_menuPopup.SetBitmapBackground(crToolbarBk);
m_menuPopup.SetIconSize(sizeToolbarIcon.cx, sizeToolbarIcon.cy);
bRetValue = m_menuPopup.LoadToolbar(nToolbarID);
// If something wrong
if (bRetValue == FALSE)
{
m_menuPopup.DestroyMenu();
return BTNST_INVALIDRESOURCE;
} // if
} // if
m_hParentWndMenu = hParentWnd;
} // if
// Repaint the button
if (bRepaint) Invalidate();
return BTNST_OK;
} // End of SetMenu
程序过程:
头文件:
CButtonST m_btnHelp;
源文件:
1)在当前对话类的初始化函数中添加:
OnInitDialog()
m_btnHelp.SetIcon(IDI_HELP, (int)BTNST_AUTO_GRAY); //设置图标,未点击时变灰
m_btnHelp.SetTooltipText(_T("Help")); //输出文字
#ifdef BTNST_USE_BCMENU
m_btnHelp.SetMenu(IDR_MENU, m_hWnd); //点击时弹出菜单栏
#else
m_btnHelp.SetMenu(IDR_MENU, m_hWnd);
#endif
2)设置控件交换信息:
DoDataExchange(CDataExchange* pDX)函数中
DDX_Control(pDX,IDC_BUTTON1,m_btnHelp); //输出
3)新菜单栏响应函数:
新建一菜单栏:IDR_MENUNEW,设置为POP-UP;
设置子菜单:IDR_ITEM1.点击ClassWizard,选择当前文档类点击ON_COMMAND设置响应函数。