MainFrm.cpp
资源名称:RebarMenu.zip [点击查看]
上传用户:xywutai
上传日期:2007-01-02
资源大小:72k
文件大小:6k
源码类别:
工具条
开发平台:
Visual C++
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "RebarMenu.h"
- #include "MainFrm.h"
- #include "ToolBarCtrlEx.h"
- #include "ReBarCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define MAX_MENU_SUBMENUS 20
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_VIEW_CUST, OnViewCust)
- //}}AFX_MSG_MAP
- ON_COMMAND_RANGE(1,MAX_MENU_SUBMENUS, OnMenu)
- // required handlers for adjusting toolbar-buttons
- ON_NOTIFY(TBN_QUERYDELETE, AFX_IDW_TOOLBAR, OnQueryDelete)
- ON_NOTIFY(TBN_QUERYINSERT, AFX_IDW_TOOLBAR, OnQueryInsert)
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- //RebarMenu
- //remove system menu
- GetMenu()->DestroyMenu();
- DrawMenuBar();
- if (!m_wndToolBar.CreateEx(this) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndMenuBar.CreateEx(this))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- //RebarMenu
- //set styles of toolbars
- m_wndToolBar.ModifyStyle(0,
- TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | TBSTYLE_ALTDRAG | CCS_ADJUSTABLE);
- m_wndMenuBar.GetToolBarCtrl().ModifyStyleEx(0,WS_EX_TOOLWINDOW);
- m_wndMenuBar.ModifyStyle(0,
- TBSTYLE_TRANSPARENT | TBSTYLE_FLAT | TBSTYLE_ALTDRAG |
- TBSTYLE_LIST | TBSTYLE_WRAPABLE | CCS_ADJUSTABLE );
- //set bitmap size to minimum
- m_wndMenuBar.SetSizes(CSize(16,20),CSize(1,1));
- //create toolbar buttons based on menu (IDR_MAINFRAME)
- TBBUTTON button;
- CString strItem;
- CMenu mTopMenu;
- mTopMenu.LoadMenu(IDR_MAINFRAME);
- UINT iPos;
- for (iPos = 0; iPos < mTopMenu.GetMenuItemCount(); iPos++)
- {
- mTopMenu.GetMenuString(iPos, strItem, MF_BYPOSITION);
- button.idCommand = iPos + 1;
- button.iBitmap = -1;
- button.fsState = 0;
- button.fsStyle = TBSTYLE_AUTOSIZE | TBSTYLE_BUTTON;
- button.iString = -1;
- m_wndMenuBar.GetToolBarCtrl().InsertButton(iPos,&button);
- m_wndMenuBar.SetButtonText(iPos,strItem);
- }
- //create rebar
- if (!m_wndReBar.Create(this))
- {
- TRACE0("Failed to create rebarn");
- return -1; // fail to create
- }
- m_wndReBar.ModifyStyleEx(0,WS_EX_TOOLWINDOW);
- m_wndReBar.ModifyStyle(0,WS_BORDER);
- // add the toolbars to the rebar
- m_wndReBar.AddBar(&m_wndMenuBar);
- m_wndReBar.AddBar(&m_wndToolBar,NULL,NULL,RBBS_BREAK);
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // setup min/max sizes and ideal sizes for pieces of the rebar
- REBARBANDINFO rbbi;
- CRect rcToolBar;
- m_wndMenuBar.GetWindowRect(&rcToolBar);
- rbbi.cbSize = sizeof(rbbi);
- rbbi.fMask = RBBIM_STYLE | RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE;
- rbbi.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP;
- rbbi.cxMinChild = rcToolBar.Width();
- rbbi.cyMinChild = rcToolBar.Height();
- rbbi.cx = rbbi.cxIdeal = rcToolBar.Width() * 10;
- m_wndReBar.GetReBarCtrl().SetBandInfo(0, &rbbi);
- rbbi.cxMinChild = 0;
- m_wndToolBar.GetWindowRect(&rcToolBar);
- rbbi.cbSize = sizeof(rbbi);
- rbbi.cxMinChild = rcToolBar.Width();
- rbbi.cyMinChild = rcToolBar.Height()+2;
- rbbi.cx = rbbi.cxIdeal = rcToolBar.Width() * 10;
- rbbi.cxMinChild = 0;
- m_wndReBar.GetReBarCtrl().SetBandInfo(1, &rbbi);
- m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
- CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- return CFrameWnd::PreCreateWindow(cs);
- }
- // customized toolbar handlers
- void CMainFrame::OnQueryDelete(NMHDR* pHdr, LRESULT* pResult) {
- TRACE0("OnQueryDeleten");
- LPTBNOTIFY pN = LPTBNOTIFY(pHdr);
- *pResult = TRUE; // allow deletion of a button
- }
- void CMainFrame::OnQueryInsert(NMHDR* pHdr, LRESULT* pResult) {
- TRACE0("OnQueryInsertn");
- LPTBNOTIFY pN = LPTBNOTIFY(pHdr);
- *pResult = TRUE; // allow insertion of a button
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- // shows customize dialog
- void CMainFrame::OnViewCust()
- {
- m_wndToolBar.GetToolBarCtrl().Customize();
- }
- // handler to menu buttons
- void CMainFrame::OnMenu(UINT nID)
- {
- CMenu m_mnuTopMenu;
- CRect rWindow,rButton;
- m_mnuTopMenu.LoadMenu(IDR_MAINFRAME);
- UINT iPos;
- for (iPos = 0; iPos < m_mnuTopMenu.GetMenuItemCount(); iPos++)
- {
- if (iPos == nID-1)
- {
- m_wndMenuBar.GetWindowRect(&rWindow);
- m_wndMenuBar.GetItemRect( iPos, &rButton);
- rWindow.top += rButton.bottom;
- rWindow.left += rButton.left;
- m_wndMenuBar.GetToolBarCtrl().SetState(iPos+1,TBSTATE_PRESSED|TBSTATE_ENABLED);
- // show popup menu
- m_mnuTopMenu.GetSubMenu(iPos)->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL,rWindow.left,rWindow.top,this);
- m_wndMenuBar.GetToolBarCtrl().SetState(iPos+1,TBSTATE_ENABLED);
- break;
- }
- }
- }