MFC-菜单
创建菜单-方式1:
#include <afxwin.h>
#include "resource.h"
class MyFrameWnd :public CFrameWnd {
};
class MyWinApp :public CWinApp {
public:
MyWinApp() {
}
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
// 挂菜单
pFrame->Create(NULL,"Test",WS_OVERLAPPEDWINDOW,CFrameWnd::rectDefault,NULL,
(CHAR*)IDR_MENU1);
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
MyWinApp mainApp;
创建菜单-方式2:
#include <afxwin.h>
#include "resource.h"
class MyFrameWnd :public CFrameWnd {
// 类内声明宏
DECLARE_MESSAGE_MAP()
public:
int OnCreate(LPCREATESTRUCT pcs);
CMenu menu;
};
// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd, CFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
menu.LoadMenuA(IDR_MENU1);
this->SetMenu(&menu);
return CFrameWnd::OnCreate(pcs);
}
class MyWinApp :public CWinApp {
public:
MyWinApp() {
}
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
pFrame->Create(NULL, "Test");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
MyWinApp mainApp;
处理消息
#include <afxwin.h>
#include "resource.h"
class MyFrameWnd :public CFrameWnd {
// 类内声明宏
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnNew();
public:
CMenu menu;
};
// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(Create_NewFile,OnNew)
END_MESSAGE_MAP()
void MyFrameWnd::OnNew() {
AfxMessageBox("新建");
}
int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
menu.LoadMenuA(IDR_MENU1);
this->SetMenu(&menu);
// ::SetMenu(this->m_hWnd,menu.m_hMenu);
return CFrameWnd::OnCreate(pcs);
}
class MyWinApp :public CWinApp {
public:
MyWinApp() {
}
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
pFrame->Create(NULL, "Test");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
MyWinApp mainApp;
消息处理顺序
#include <afxwin.h>
#include "resource.h"
class MyFrameWnd :public CFrameWnd {
// 类内声明宏
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnNew();
public:
CMenu menu;
};
// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(Create_NewFile,OnNew)
END_MESSAGE_MAP()
void MyFrameWnd::OnNew() {
AfxMessageBox("框架-新建");
}
int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
menu.LoadMenuA(IDR_MENU1);
this->SetMenu(&menu);
// ::SetMenu(this->m_hWnd,menu.m_hMenu);
return CFrameWnd::OnCreate(pcs);
}
class MyWinApp :public CWinApp {
DECLARE_MESSAGE_MAP()
public:
MyWinApp() {
}
afx_msg void OnNew();
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
pFrame->Create(NULL, "Test");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
BEGIN_MESSAGE_MAP(MyWinApp, CWinApp)
ON_COMMAND(Create_NewFile, OnNew)
END_MESSAGE_MAP()
void MyWinApp::OnNew() {
AfxMessageBox("应用程序-新建");
}
MyWinApp mainApp;
#include <afxwin.h>
#include "resource.h"
class MyFrameWnd :public CFrameWnd {
// 类内声明宏
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT pcs);
afx_msg void OnNew();
public:
CMenu menu;
};
// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd, CFrameWnd)
ON_WM_CREATE()
//ON_COMMAND(Create_NewFile,OnNew)
END_MESSAGE_MAP()
void MyFrameWnd::OnNew() {
AfxMessageBox("框架-新建");
}
int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
menu.LoadMenuA(IDR_MENU1);
this->SetMenu(&menu);
// ::SetMenu(this->m_hWnd,menu.m_hMenu);
return CFrameWnd::OnCreate(pcs);
}
class MyWinApp :public CWinApp {
DECLARE_MESSAGE_MAP()
public:
MyWinApp() {
}
afx_msg void OnNew();
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
pFrame->Create(NULL, "Test");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
BEGIN_MESSAGE_MAP(MyWinApp, CWinApp)
ON_COMMAND(Create_NewFile, OnNew)
END_MESSAGE_MAP()
void MyWinApp::OnNew() {
AfxMessageBox("应用程序-新建");
}
MyWinApp mainApp;