Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525183
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-02-07 23:09:15

这个程序非常适合于练手,我作了一些修改,主要是用SCROLLINFO来处理滚动条消息,SetScrollPos要考虑的东西太多了!
头文件:

#ifndef ACCEL_H
#define ACCEL_H
class CMyApp : public CWinApp {
    virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd {
public:
    CMainWindow();
protected:
    int m_nCellWidth;
    int m_nCellHeight;
    int m_nRibbonWidth;
    int m_nViewWidth;
    int m_nViewHeight;
    int m_nVscrollPos;
    int m_nHscrollPos;
    // 这个似乎更方便的

    SCROLLINFO si;

    afx_msg int OnCreate(LPCREATESTRUCT);
    afx_msg void OnSize(UINT, int, int);
    afx_msg void OnPaint();
    afx_msg void OnVScroll(UINT, UINT, CScrollBar*);
    afx_msg void OnHScroll(UINT, UINT, CScrollBar*);
    DECLARE_MESSAGE_MAP()
};
#endif

实现文件:

#include <afxwin.h>
#include "Accel.h"

CMyApp myApp;

BOOL CMyApp::InitInstance() {
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_PAINT()
    ON_WM_HSCROLL()
    ON_WM_VSCROLL()
END_MESSAGE_MAP()

CMainWindow::CMainWindow() {
    Create(NULL, _T("大一点的程序怎么了?"), WS_OVERLAPPEDWINDOW |
        WS_HSCROLL | WS_VSCROLL);
}
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    if(CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    CClientDC dc(this);
    // 把宽设为一个逻辑英寸长

    m_nCellWidth = dc.GetDeviceCaps(LOGPIXELSX);
    // 把高设为1/4逻辑英寸长

    m_nCellHeight = m_nCellWidth / 4;
    m_nRibbonWidth = m_nCellWidth / 2;
    // 范围宽设为26个单元宽加上带状条的宽度

    m_nViewWidth = 26 * m_nCellWidth + m_nRibbonWidth;
    // 范围高设为100个单元高度

    m_nViewHeight = 100 * m_nCellHeight;
    return 0;
}

void CMainWindow::OnSize(UINT nType, int cx, int cy) {
    // CFrameWnd::OnSize(nType, cx, cy);

    si.cbSize = sizeof(si);
    si.fMask = SIF_ALL;
    si.nMin = 0;
    si.nMax = m_nViewHeight - 1;
    si.nPos = 0;
    si.nPage = cy;

    SetScrollInfo(SB_VERT, &si, TRUE);

    si.nMin = 0;
    si.nMax = m_nViewWidth - 1;
    si.nPos = 0;
    si.nPage = cx;

    SetScrollInfo(SB_HORZ, &si, TRUE);
}

void CMainWindow::OnPaint() {
    CPaintDC dc(this);

    CPen pen(PS_SOLID, 0, RGB(192, 192, 192));
    CPen* oldPen = dc.SelectObject(&pen);
    GetScrollInfo(SB_VERT, &si);
    m_nVscrollPos = si.nPos;
    GetScrollInfo(SB_HORZ, &si);
    m_nHscrollPos = si.nPos;
    dc.SetWindowOrg(m_nHscrollPos, m_nVscrollPos);

    for(int i = 0; i < 99; i++) {
        dc.MoveTo(0, (i + 1) * m_nCellHeight);
        dc.LineTo(m_nViewWidth, (i + 1) * m_nCellHeight);
    }
    for(int i = 0; i < 26; i++) {
        dc.MoveTo(i * m_nCellWidth + m_nRibbonWidth, 0);
        dc.LineTo(i * m_nCellWidth + m_nRibbonWidth, m_nViewHeight);
    }
    dc.SelectObject(oldPen);

    CBrush brush;
    brush.CreateStockObject(LTGRAY_BRUSH);

    CRect rcTop(0, 0, m_nViewWidth, m_nCellHeight);
    dc.FillRect(&rcTop, &brush);
    CRect rcLeft(0, 0, m_nRibbonWidth, m_nViewHeight);
    dc.FillRect(&rcLeft, &brush);

    dc.MoveTo(0, m_nCellHeight);
    dc.LineTo(m_nViewWidth, m_nCellHeight);
    dc.MoveTo(m_nRibbonWidth, 0);
    dc.LineTo(m_nRibbonWidth, m_nViewHeight);

    dc.SetBkMode(TRANSPARENT);

    for(int i = 0; i < 99; i++) {
        int y = (i + 1) * m_nCellHeight;
        dc.MoveTo(0, y);
        dc.LineTo(m_nRibbonWidth, y);

        CString str;
        str.Format(_T("%d"), i + 1);
        CRect rect(0, y, m_nRibbonWidth, y + m_nCellHeight);
        dc.DrawText(str, &rect, DT_SINGLELINE |
            DT_CENTER | DT_VCENTER);
        dc.Draw3dRect(&rect, RGB(255, 255, 255),
            RGB(128, 128, 128));
    }

    for(int i = 0; i < 26; i++) {
        int x = i * m_nCellWidth + m_nRibbonWidth;
        dc.MoveTo(x, 0);
        dc.LineTo(x, m_nCellHeight);

        CString str;
        str.Format(_T("%c"), i + 'A');
        CRect rect(x, 0, x + m_nCellWidth, m_nCellHeight);
        dc.DrawText(str, &rect, DT_SINGLELINE |
            DT_CENTER | DT_VCENTER);
        dc.Draw3dRect(&rect, RGB(255, 255, 255),
            RGB(128, 128, 128));
    }
}

void CMainWindow::OnVScroll(UINT nCode,
                            UINT nPos,
                            CScrollBar* pScrollBar)
{
    si.fMask = SIF_ALL;
    GetScrollInfo(SB_VERT, &si);
    m_nVscrollPos = si.nPos;

    switch(nCode) {
        case SB_LINEUP:
            si.nPos -= m_nCellHeight;
            break;
        case SB_LINEDOWN:
            si.nPos += m_nCellHeight;
            break;
        case SB_PAGEUP:
            si.nPos -= si.nPage;
            break;
        case SB_PAGEDOWN:
            si.nPos += si.nPage;
            break;
        case SB_THUMBTRACK:
            si.nPos = nPos;
            break;
        default:
            break;
    }
    si.fMask = SIF_POS;
    SetScrollInfo(SB_VERT, &si, TRUE);
    GetScrollInfo(SB_VERT, &si, SIF_POS);
    if(si.nPos != m_nVscrollPos) {
        ScrollWindow(0, m_nVscrollPos - si.nPos, NULL, NULL);
        // InvalidateRect(NULL, TRUE);

    }
}

void CMainWindow::OnHScroll(UINT nCode,
                            UINT nPos,
                            CScrollBar* pScrollBar)
{
    si.fMask = SIF_ALL;
    GetScrollInfo(SB_HORZ, &si);
    m_nHscrollPos = si.nPos;

    switch(nCode) {
        case SB_LINELEFT:
            si.nPos -= m_nCellWidth;
            break;
        case SB_LINERIGHT:
            si.nPos += m_nCellWidth;
            break;
        case SB_PAGELEFT:
            si.nPos -= si.nPage;
            break;
        case SB_PAGERIGHT:
            si.nPos += si.nPage;
            break;
        case SB_THUMBTRACK:
            si.nPos = nPos;
            break;
        default:
            break;
    }
    si.fMask = SIF_POS;
    SetScrollInfo(SB_HORZ, &si, TRUE);
    GetScrollInfo(SB_HORZ, &si, SIF_POS);
    if(m_nHscrollPos != si.nPos) {
        ScrollWindow(m_nHscrollPos - si.nPos, 0, NULL, NULL);
        // InvalidateRect(NULL, TRUE);

    }
}

运行结果:
其中一个比较有意思的函数是Draw3dRect, 哈哈……
阅读(2209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~