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

📄 mainfrm.cpp

📁 《Visual C++ Bible》或者说是《Visual C++ 宝典》的对应的源码文件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    ResetScrollValues();

    // Request a WM_PAINT message to redraw window.
    Invalidate();
}

//-----------------------------------------------------------------------------
// Handler for Format|Tabs command message.
void DMainFrame::CmdFormatTabs() 
{
    // Toggle "respect tab" flag.
    d_bTabs = !d_bTabs;

    // Request a WM_PAINT message to redraw window.
    Invalidate();
}

//-----------------------------------------------------------------------------
// Handler for CN_UPDATE_COMMAND_UI message.
void DMainFrame::UpdFormatTabs(CCmdUI* pCmdUI) 
{
    // Display a checkmark if user wants to respect tabs.
    int nCheck = (d_bTabs) ? 1 : 0;
    pCmdUI->SetCheck(nCheck);
}

//-----------------------------------------------------------------------------
// WM_CREATE message handler.
int DMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    // Send WM_CREATE to base class first...
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Create File.Open... dialog object.
    d_pOpenFile = new CFileDialog (TRUE);
    if (!d_pOpenFile)
        return -1;

    // Initialize file filter.
    d_pOpenFile->m_ofn.lpstrFilter = _T("All Source Files\0*.cpp;*.h;*.rc\0"
                                        "C++ Sources (*.cpp)\0*.cpp\0"
                                        "Include Files (*.h)\0*.h\0"
                                        "All Files (*.*)\0*.*\0");

    // Create Format.Font... dialog object.
    d_pSetFont = new CFontDialog(&d_lf, CF_SCREENFONTS);
    if (!d_pSetFont)
        return -1;

    // Create initial font object.
    if (!CreateNewFont())
        return -1;

    // Initialize application settings.
    OnWinIniChange(_T(""));

    // [GETTEXT] Load cursors
    d_hcrIBeam  = ::LoadCursor(NULL, IDC_IBEAM);
    d_hcrLArrow = ::LoadCursor(NULL, IDC_ARROW);
    d_hcrRArrow = AfxGetApp()->LoadCursor(IDC_RARROW);
    if (d_hcrIBeam == NULL || d_hcrRArrow == NULL)
        return -1;

    // Indicate window creation is OK.
    return 0;
}

//-----------------------------------------------------------------------------
// [GETTEXT] Handler for WM_ERASEBKGND message.
BOOL DMainFrame::OnEraseBkgnd(CDC* pDC) 
{
    // Select text background color.
    pDC->SetBkColor(d_crBackground);

    // Figure out which lines of text need blanking out.
    int iLine = d_iTopLine;
    int cLastLine = d_iTopLine + d_clHeight + 1;

    // Modify text colors based on selection state.
    int iSelectMin = -1;
    int iSelectMax = -1;
    if (d_nFocusMode == FOCUS_SELECT && GetFocus() == this)
    {
        // Get order of selected lines straight .
        int nSelectLow  = min (d_nSelectFirst, d_nSelectLast);
        int nSelectHigh = max (d_nSelectFirst, d_nSelectLast);

        if (!(nSelectLow > cLastLine || nSelectHigh < d_iTopLine))
        {
            // Figure out which lines to draw selected.
            iSelectMin = max (nSelectLow, d_iTopLine);
            iSelectMax = min (nSelectHigh, cLastLine);
        }
    }

    // Initialize first rectangle to erase.
    CRect rClient;
    GetClientRect(&rClient);
    int yClientBottom = rClient.bottom;
    rClient.bottom = rClient.top + d_cyLineHeight;

    // Loop through lines in client area.
    int cyLine = 0;
    while (rClient.top <= yClientBottom)
    {
        // Set background to "selected" colors.
        if (iLine == iSelectMin)
        {
            pDC->SetBkColor(d_crSelectBack);
        }

        // Erase background (uses 'background' color in DC)
        pDC->ExtTextOut(0, 0, ETO_OPAQUE, &rClient, 0, 0, 0);

        // Set background to "normal" colors.
        if (iLine == iSelectMax)
        {
            pDC->SetBkColor(d_crBackground);
        }

        // Increment various counts.
        iLine++;
        rClient.top    += d_cyLineHeight;
        rClient.bottom += d_cyLineHeight;
    }

    // Don't call the default handler, since it 
    // erases the wrong (white always) color.
    // ++++ NO: return CFrameWnd::OnEraseBkgnd(pDC);

    return TRUE;
}

//-----------------------------------------------------------------------------
// [GETTEXT] WM_KEYDOWN message handler.
void DMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    switch(nChar)
    {
        case VK_HOME:
            // Ctrl+Home means go to start of document.
            if (::GetKeyState(VK_CONTROL) < 0)
            {
                // Scroll to start of document
                OnVScroll(SB_TOP, 0, 0);
                MoveCaret(0,0);
            }
            else
            {
                // Move to start of current line.
                int iLine = QueryClickLine(d_ptCaret);
                MoveCaret(iLine, 0);
            }
            break;
        case VK_END:
            // Ctrl+End means go to end of document.
            if (::GetKeyState(VK_CONTROL) < 0)
            {
                // Scroll to end
                OnVScroll(SB_BOTTOM, 0, 0);
                MoveCaret(d_cLines-1,0);
            }
            else
            {
                // Move to end of current line.
                int iLine = QueryClickLine(d_ptCaret);
                int iChar = d_saTextInfo[iLine].ccLen;
                MoveCaret(iLine, iChar);
            }
            break;
        case VK_UP:
            {
                CPoint ptCaret;
                ptCaret = GetCaretPos();
                ptCaret.y -= d_cyLineHeight;
                MoveCaretClick(ptCaret);
            }
            break;
        case VK_DOWN:
            {
                CPoint ptCaret;
                ptCaret = GetCaretPos();
                ptCaret.y += d_cyLineHeight;
                MoveCaretClick(ptCaret);
            }
            break;
        case VK_LEFT:
            MoveCaretChar(-1);
            break;
        case VK_RIGHT:
            MoveCaretChar(+1);
            break;
        case VK_PRIOR: // [PgUp] key.
            {
                CPoint ptCaret;
                ptCaret = GetCaretPos();
                HideCaret();
                OnVScroll(SB_PAGEUP, 0, 0);
                ShowCaret();
                SetCaretPos(ptCaret);
            }
            break;
        case VK_NEXT:  // [PgDn] key.
            {
                CPoint ptCaret;
                ptCaret = GetCaretPos();
                HideCaret();
                OnVScroll(SB_PAGEDOWN, 0, 0);
                ShowCaret();
                SetCaretPos(ptCaret);
            }
            break;
    }
    
    CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

//-----------------------------------------------------------------------------
// [GETTEXT] WM_KILLFOCUS message handler.
void DMainFrame::OnKillFocus(CWnd* pNewWnd) 
{
    CFrameWnd::OnKillFocus(pNewWnd);
    
    switch(d_nFocusMode) 
    {
        case FOCUS_EMPTY:
            break;
        case FOCUS_CARET:
            // Query and save current caret position.
            d_ptCaret = GetCaretPos();

            // Eliminate caret.
            HideCaret();
            DestroyCaret();
            break;
        case FOCUS_SELECT:
            InvertLines(d_nSelectFirst, d_nSelectLast);
            break;
    }
}

//-----------------------------------------------------------------------------
// [GETTEXT] WM_LBUTTONDBLCLK message handler.
void DMainFrame::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
    // Our pet peeve -- Ignoring a second keystroke that looked
    // like a double-button click.
    // Our solution -- Handling the double-click message and 
    // calling the single-click handler.
    OnLButtonDown(nFlags, point);
}

//-----------------------------------------------------------------------------
// [GETTEXT] WM_LBUTTONDOWN message handler.
void DMainFrame::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // If we're activating with button click, 
    // activate window and ignore mouse message.
    if (d_bMouseActive)
    {
        SetActiveWindow();
        d_bMouseActive = FALSE;
        return;
    }

    // Hide results of previous focus mode.
    switch (d_nFocusMode)
    {
        case FOCUS_CARET:
            HideCaret();
            break;
        case FOCUS_SELECT:
            InvertLines(d_nSelectFirst, d_nSelectLast);
            d_nSelectFirst = -1;
            d_nSelectLast  = -1; 
            break;
    }

    // Set focus mode based on mouse location.
    d_nFocusMode = (point.x < d_cxLeftMargin) ? FOCUS_SELECT : FOCUS_CARET;

    // Make results of new focus mode known to user.
    switch(d_nFocusMode)
    {
        case FOCUS_EMPTY:
            break;
        case FOCUS_CARET:
            {
                int nLine = QueryClickLine(point);
                int nChar = QueryClickChar(nLine, point);
                MoveCaret(nLine, nChar);
                ShowCaret();
            }
            break;
        case FOCUS_SELECT:
            {
                // Figure out where mouse was clicked.
                d_nSelectFirst = QueryClickLine(point);
                d_nSelectLast  = d_nSelectFirst;

                // Highlight that line.
                InvertLines(d_nSelectFirst, d_nSelectLast);

                // Capture the mouse.
                SetCapture();
                d_bCapture = TRUE;
            }
            break;
    }

    CFrameWnd::OnLButtonDown(nFlags, point);
}

//-----------------------------------------------------------------------------
// [GETTEXT] Handler for WM_LBUTTONUP message.
void DMainFrame::OnLButtonUp(UINT nFlags, CPoint point) 
{
    // Release the mouse (captured during WM_LBUTTONDOWN)
    if (d_bCapture)
    {
        ReleaseCapture();
        d_bCapture = FALSE;
    }

    // Kill the timer (started during WM_MOUSEMOVE)
    if (d_bTimerStarted)
    {
        KillTimer(d_nTimerID);
        d_bTimerStarted = FALSE;
    }

    CFrameWnd::OnLButtonUp(nFlags, point);
}

//-----------------------------------------------------------------------------
// [GETTEXT] Handler for WM_MBUTTONDOWN message.
void DMainFrame::OnMButtonDown(UINT nFlags, CPoint point) 
{
    // If we're activating with button click, 
    // activate window and ignore mouse message.
    if (d_bMouseActive)
    {
        SetActiveWindow();
        d_bMouseActive = FALSE;
        return;
    }
}

//-----------------------------------------------------------------------------
// [GETTEXT] WM_MOUSEACTIVATE message handler.
int DMainFrame::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
    d_bMouseActive = TRUE;

    return MA_NOACTIVATE;
}

//-----------------------------------------------------------------------------
// [GETTEXT] Handler for WM_MOUSEMOVE message.
void DMainFrame::OnMouseMove(UINT nFlags, CPoint point) 
{
    // Pay attention to mouse moves only in select mode.
    if ((!d_bCapture) || (d_nFocusMode != FOCUS_SELECT))
    {
        CFrameWnd::OnMouseMove(nFlags, point);
        return;
    }

    // Fetch current line selected.
    CRect rClient;
    GetClientRect(&rClient);
    point.x = 1;
    if (!rClient.PtInRect(point))
    {
        // Scroll if above or below client area. 
        if (point.y < rClient.bottom)
        {

⌨️ 快捷键说明

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