⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
字号:
// mainfrm.cpp : implementation of the DMainFrame class
//

#include "stdafx.h"
#include "ctrlbars.h"

#include "mainfrm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DMainFrame

IMPLEMENT_DYNCREATE(DMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(DMainFrame, CFrameWnd)
    //{{AFX_MSG_MAP(DMainFrame)
    ON_WM_CREATE()
    ON_COMMAND(ID_TOOLBAR_CREATE, OnToolbarCreate)
    ON_COMMAND(ID_TOOLBAR_SHOW, OnToolbarShow)
    ON_UPDATE_COMMAND_UI(ID_TOOLBAR_CREATE, OnUpdateToolbarCreate)
    ON_UPDATE_COMMAND_UI(ID_TOOLBAR_SHOW, OnUpdateToolbarShow)
    ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
    ON_COMMAND(ID_EDIT_CUT, OnEditCut)
    ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
    ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
    ON_COMMAND(ID_FILE_NEW, OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
    ON_COMMAND(ID_FILE_SAVE, OnFileSave)
    ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// arrays of IDs used to initialize control bars
    
// toolbar buttons - IDs are command buttons
static UINT BASED_CODE buttons[] =
{
    // same order as in the bitmap 'bitmap1.bmp'
    ID_TOOLBAR_CREATE,
        ID_SEPARATOR,
    ID_TOOLBAR_SHOW
};

// toolbar buttons - IDs are command buttons
static UINT BASED_CODE Toolbar2Buttons[] =
{
    // same order as in the bitmap 'toolbar.bmp'
    ID_FILE_NEW,
    ID_FILE_OPEN,
    ID_FILE_SAVE,
        ID_SEPARATOR,
    ID_EDIT_CUT,
    ID_EDIT_COPY,
    ID_EDIT_PASTE,
        ID_SEPARATOR,
    ID_FILE_PRINT,
    ID_APP_ABOUT,
};

static UINT BASED_CODE indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// DMainFrame construction/destruction

DMainFrame::DMainFrame()
{
    d_pToolbar2 = 0;
    d_bToolbarVisible = FALSE;
}

DMainFrame::~DMainFrame()
{
}

int DMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    if (!m_wndToolBar.Create(this) ||
        !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
        !m_wndToolBar.SetButtons(buttons,
          sizeof(buttons)/sizeof(UINT)))
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    // TODO: Delete these three lines if you don't want the toolbar to
    //  be dockable
    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);

    // TODO: Remove this if you don't want tool tips
    m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
        CBRS_TOOLTIPS | CBRS_FLYBY);

    // Create a status bar.
    if (!m_wndStatusBar.Create(this) ||
        !m_wndStatusBar.SetIndicators(indicators,
          sizeof(indicators)/sizeof(UINT)))
    {
        TRACE0("Failed to create status bar\n");
        return -1;      // fail to create
    }

    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// DMainFrame diagnostics

#ifdef _DEBUG
void DMainFrame::AssertValid() const
{
    CFrameWnd::AssertValid();
}

void DMainFrame::Dump(CDumpContext& dc) const
{
    CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// DMainFrame message handlers

//-------------------------------------------------------------------
// WM_COMMAND handler for Toolbar|Create menu item.
void DMainFrame::OnToolbarCreate() 
{
    // Should only get here if we don't have a toolbar.
    ASSERT(d_pToolbar2 == 0);

    // Create C++ object and WinAPI window.
    d_pToolbar2 = new CToolBar();
    d_pToolbar2->Create(this, WS_CHILD | CBRS_TOP | 
                              CBRS_TOOLTIPS | CBRS_FLYBY,
                              0x9100);

    // Get bitmap and connect to tool items.
    d_pToolbar2->LoadBitmap(IDR_TOOLS);
    d_pToolbar2->SetButtons(Toolbar2Buttons, 
                            sizeof(Toolbar2Buttons)/sizeof(UINT));

    // Make toolbar dockable.
    d_pToolbar2->EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(d_pToolbar2);
}

//-------------------------------------------------------------------
// WM_COMMAND handler for Toolbar|Show menu item.
void DMainFrame::OnToolbarShow() 
{
    ASSERT(d_pToolbar2 != 0);

    // Query current visibility.
    BOOL bVisible = (d_pToolbar2->GetStyle() & WS_VISIBLE);

    // Show or hide.
    int nShow = (bVisible) ? SW_HIDE : SW_SHOWNORMAL;
    d_pToolbar2->ShowWindow(nShow);

    // Reconfigure remaining toolbar items.
    RecalcLayout();

    // Store visibility state for later.
    d_bToolbarVisible = (!bVisible);
}

//-------------------------------------------------------------------
// ON_COMMAND_UPDATE_UI handler for Toolbar|Create menu item.
void DMainFrame::OnUpdateToolbarCreate(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(d_pToolbar2 == 0);
}

//-------------------------------------------------------------------
// ON_COMMAND_UPDATE_UI handler for Toolbar|Show menu item.
void DMainFrame::OnUpdateToolbarShow(CCmdUI* pCmdUI) 
{
    pCmdUI->Enable(d_pToolbar2 != 0);
    int nCheck = (d_bToolbarVisible) ? 1 : 0;
    pCmdUI->SetCheck(nCheck);
}

//-------------------------------------------------------------------
// WM_COMMAND handler for Edit|Copy.
void DMainFrame::OnEditCopy() 
{
    AfxMessageBox(_T("Edit|Copy command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for Edit|Cut.
void DMainFrame::OnEditCut() 
{
    AfxMessageBox(_T("Edit|Cut command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for Edit|Paste.
void DMainFrame::OnEditPaste() 
{
    AfxMessageBox(_T("Edit|Paste command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for Edit|Undo.
void DMainFrame::OnEditUndo() 
{
    AfxMessageBox(_T("Edit|Undo command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for File|New.
void DMainFrame::OnFileNew() 
{
    AfxMessageBox(_T("File|New command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for File|Open.
void DMainFrame::OnFileOpen() 
{
    AfxMessageBox(_T("File|Open... command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for File|Save.
void DMainFrame::OnFileSave() 
{
    AfxMessageBox(_T("File|Save command selected."));
}

//-------------------------------------------------------------------
// WM_COMMAND handler for File|Save.
void DMainFrame::OnFilePrint() 
{
    AfxMessageBox(_T("File|Print... command selected."));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -