#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);
}
}
|