MFC 学习笔记( 四 )

MFC菜单:

MFC 学习笔记

文章插图
菜单的使用:
MFC 学习笔记

文章插图
方法一:
MFC 学习笔记

文章插图
方法二:
#include <afxwin.h>#include "resource.h"class CMyFrameWnd : public CFrameWnd{ DECLARE_MESSAGE_MAP( )public: afx_msg int OnCreate( LPCREATESTRUCT pcs ); afx_msg void OnNew( );public: CMenu menu;};BEGIN_MESSAGE_MAP( CMyFrameWnd, CFrameWnd ) ON_COMMAND( ID_NEW, OnNew ) ON_WM_CREATE( )END_MESSAGE_MAP( )void CMyFrameWnd::OnNew( ){ AfxMessageBox( "框架类处理了新建菜单项被点击" );}int CMyFrameWnd::OnCreate( LPCREATESTRUCT pcs ){ //调用api(win32)的loadmenu方法拿到句柄,将菜单句柄和menu对象建立一对一的关系 。menu.LoadMenu( IDR_MENU1 );// this->SetMenu( &menu ); ::SetMenu( this->m_hWnd, menu.m_hMenu); return CFrameWnd::OnCreate( pcs );}class CMyWinApp : public CWinApp{ DECLARE_MESSAGE_MAP()public: virtual BOOL InitInstance( ); afx_msg void OnNew( );};BEGIN_MESSAGE_MAP(CMyWinApp, CWinApp) ON_COMMAND( ID_NEW , OnNew )END_MESSAGE_MAP( )void CMyWinApp::OnNew( ){ AfxMessageBox( "应用程序类处理了新建被点击" );}BOOL CMyWinApp::InitInstance( ){CMyFrameWnd* pFrame = new CMyFrameWnd;pFrame->Create(NULL, "MFCCreate");m_pMainWnd = pFrame;pFrame->ShowWindow( SW_SHOW );pFrame->UpdateWindow( );return TRUE;}CMyWinApp theApp;菜单项没有对应的消息处理,就会默认灰显 。
命令消息处理顺序:
(仅针对command消息类,其他类只有框架类处理一条路径)
框架类 ——> 应用程序处理类
设置菜单项状态:
MFC 学习笔记

文章插图
设置右键菜单(上下文菜单):
MFC 学习笔记

文章插图
代码:
#include <afxwin.h>#include "resource.h"class CMyFrameWnd : public CFrameWnd{ DECLARE_MESSAGE_MAP( )public://afx_msg占位符 标识这个方法是处理消息的 afx_msg int OnCreate( LPCREATESTRUCT pcs ); afx_msg void OnNew( ); afx_msg void OnInitMenuPopup( CMenu *pPopup, UINT nPos, BOOL i ); afx_msg void OnContextMenu( CWnd* pWnd, CPoint pt );public: CMenu menu;};BEGIN_MESSAGE_MAP( CMyFrameWnd, CFrameWnd )// ON_COMMAND( ID_NEW, OnNew ) ON_WM_CREATE( ) ON_WM_INITMENUPOPUP() ON_WM_CONTEXTMENU( )END_MESSAGE_MAP( )void CMyFrameWnd::OnContextMenu( CWnd* pWnd, CPoint pt ){//获取整个大菜单顶层为0的子菜单// HMENU hPopup = ::GetSubMenu(menu.m_hMenu,0);// ::TrackPopupMenu( hPopup, TPM_LEFTALIGN|TPM_TOPALIGN, pt.x, pt.y,//0, this->m_hWnd, NULL ); CMenu* pPopup = menu.GetSubMenu(0); pPopup->TrackPopupMenu( TPM_LEFTALIGN|TPM_TOPALIGN, pt.x, pt.y, this );}void CMyFrameWnd::OnInitMenuPopup( CMenu* pPopup, UINT nPos, BOOL i){// pPopup->CheckMenuItem( ID_NEW, MF_CHECKED ); ::CheckMenuItem( pPopup->m_hMenu, ID_NEW, MF_CHECKED );}class CMyWinApp : public CWinApp{public: virtual BOOL InitInstance( );};BOOL CMyWinApp::InitInstance( ){CMyFrameWnd* pFrame = new CMyFrameWnd;pFrame->Create(NULL, "MFCCreate");m_pMainWnd = pFrame;pFrame->ShowWindow( SW_SHOW );pFrame->UpdateWindow( );return TRUE;}CMyWinApp theApp;效果:
MFC 学习笔记

文章插图
四.MFC工具栏以及两大机制:工具栏:相关类:
MFC 学习笔记

文章插图
工具栏的使用:
MFC 学习笔记

文章插图
代码:
#include <afxwin.h>#include "resource.h"#include <afxext.h>class CMyFrameWnd : public CFrameWnd{ DECLARE_MESSAGE_MAP( )public: afx_msg void OnNew( ); afx_msg void OnSet( ); afx_msg int OnCreate( LPCREATESTRUCT pcs );public: CToolBar toolbar;};BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd ) ON_COMMAND( ID_SET, OnSet) ON_COMMAND( ID_NEW, OnNew ) ON_WM_CREATE()END_MESSAGE_MAP()void CMyFrameWnd::OnSet( ){ AfxMessageBox( "绿色工具按钮被点击" );}int CMyFrameWnd::OnCreate( LPCREATESTRUCT pcs ){ toolbar.CreateEx( this, TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP|CBRS_GRIPPER); toolbar.LoadToolBar( IDR_TOOLBAR1 ); //想停哪儿 toolbar.EnableDocking( CBRS_ALIGN_ANY); //允许停哪儿 this->EnableDocking( CBRS_ALIGN_ANY ); //临时停哪儿 this->DockControlBar( &toolbar, AFX_IDW_DOCKBAR_BOTTOM ); return CFrameWnd::OnCreate( pcs );}void CMyFrameWnd::OnNew(){ AfxMessageBox( "新建菜单项被点击" );}class CMyWinApp : public CWinApp{public: virtual BOOL InitInstance( );};BOOL CMyWinApp::InitInstance(){ CMyFrameWnd* pFrame = new CMyFrameWnd; pFrame->Create( NULL, "MFCToolBar", WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault,NULL, (CHAR*)IDR_MENU1); m_pMainWnd = pFrame; pFrame->ShowWindow( SW_SHOW ); pFrame->UpdateWindow( ); return TRUE;}CMyWinApp theApp;

经验总结扩展阅读