MFC-工具栏_MFC


菜单栏


一 添加工具栏资源

MFC-工具栏_MFC_02


MFC-工具栏_工具栏_03

MFC-工具栏_工具栏_04

二 创建工具栏

CToolBar toolbar;

三 加载工具栏

toolbar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP);
toolbar.LoadToolBar(IDR_TOOLBAR1);

MFC-工具栏_MFC_05

#include <afxwin.h>
#include <afxext.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;
CToolBar toolbar;
};

// 类外实现宏
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);

// 设置工具栏
toolbar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP);
toolbar.LoadToolBar(IDR_TOOLBAR1);

return CFrameWnd::OnCreate(pcs);
}


class MyWinApp :public CWinApp {
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;
}
};

MyWinApp mainApp;

四 设置工具栏停靠