Button右键弹出式菜单
关键点
用类来实现 的
实现过程
新建1个类 类名CButtonPopMenu 基类CButton
新建1个菜单资源 IDR_MENU1
// ButtonPopMenu.h : header file
// Attributes
public:
CRect m_Rect;
CMenu m_Menu;
int m_MenuID;
// ButtonPopMenu.cpp : implementation file
void CButtonPopMenu::OnRButtonDown(UINT nFlags, CPoint point)
{
GetWindowRect( &m_Rect);
point.x += m_Rect.left;
point.y += m_Rect.top;
VERIFY(m_Menu.LoadMenu(m_MenuID));
CMenu* pPopup = m_Menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while ( pWndPopupOwner->GetStyle() & WS_CHILD )
{
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, point.x, point.y, pWndPopupOwner);
}
m_Menu.Detach();
m_Menu.DestroyMenu();
CButton::OnRButtonDown(nFlags, point);
}
// project02Dlg.h : header file
#include "ButtonPopMenu.h"
CButtonPopMenu m_btn1;
// project02Dlg.cpp : implementation file
BOOL CProject02Dlg::OnInitDialog()
// TODO: Add extra initialization here
m_btn1.m_MenuID=IDR_MENU1;
|
图-1
备注
此文可以实现任意控件的右键菜单弹出
像一些控件默认所没有的功能都可以通过定义类 然后关联类来实现
相关链接
控件上弹出菜单