声明:只是自己在学习中的积累仅供学习和参考
创建一个Accel.h文件
代码如下:
class CMyApp :public CWinApp
{
public:
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_nHScrollPos;
int m_nHPageSize;
//垂直滚动条变量
int m_nVScrollPos;
int m_nVPageSize;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP();
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
创建一个Accel.cpp文件
代码如下:
#include
#include"Accel.h"
#define LINESIZE 8;
CMyApp app;
BOOL CMyApp::InitInstance()
{
this->m_pMainWnd = new CMainWindow;
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
this->m_pMainWnd->UpdateWindow();
return TRUE;
}
CMainWindow::CMainWindow()
{
this->Create(NULL, _T("Accel"), WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL);
}
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_VSCROLL()//垂直滚动条消息
ON_WM_CREATE()//窗口创建时的消息
ON_WM_PAINT()//绘图消息
ON_WM_HSCROLL()//水平滚动条消息
ON_WM_SIZE()//指示窗口大小的消息
END_MESSAGE_MAP()
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
CClientDC dc(this);
//对表格变量进行初始化不同电脑的分辨率不一样所以用给好的函数进行获取数据
m_nCellWidth = dc.GetDeviceCaps(LOGPIXELSX);
m_nCellHeight = dc.GetDeviceCaps(LOGPIXELSY) / 4;
m_nRibbonWidth = m_nCellWidth / 2;
m_nViewWidth = (26 * m_nCellWidth) + m_nRibbonWidth;
m_nViewHeight = m_nCellHeight * 100;
return 0;
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CFrameWnd::OnPaint()
//--------------------------------------------
//终点
//使客户区的信息随滚动条的移懂而移动,相当于改变客户区原点的坐标
dc.SetWindowOrg(m_nHScrollPos, m_nVScrollPos);
//--------------------------------------------
CPen pen(PS_SOLID,0, RGB(192,192,192));
CPen* pOldPen = dc.SelectObject(&pen);
for (int i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nViewWidth, y);
}
for (int j = 0; j < 26; j++)
{
int x = (j * m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nViewHeight);
}
dc.SelectObject(pOldPen);
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 * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nRibbonWidth, y);
CString string;
string.Format(_T("%d"), i + 1);
CRect rect(0, y, m_nRibbonWidth, y + m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
//增加3d立体感
rect.top++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
}
for (int j = 0; j < 26; j++)
{
int x = (j * m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nCellHeight);
CString string;
string.Format(_T("%c"), (j + 'A'));
CRect rect(x,0 ,x + m_nCellWidth,m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
rect.top++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
}
}
//水平滚动条
void CMainWindow::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int nDelta = 0;
switch (nSBCode)
{
case SB_LINEUP:
nDelta = -LINESIZE;
break;
case SB_LINEDOWN:
nDelta = LINESIZE;
break;
case SB_PAGEUP:
nDelta = -m_nHPageSize;
break;
case SB_PAGEDOWN:
nDelta = m_nHPageSize;
break;
case SB_THUMBTRACK:
//重点nPos是滚动条的位置这里用了一个小技巧
nDelta = (int)nPos - m_nHScrollPos;
break;
default:
return;
}
if (nDelta != 0) {
m_nHScrollPos += nDelta;
SetScrollPos(SB_HORZ, m_nHScrollPos, TRUE);
ScrollWindow(-nDelta,0 );
}
CFrameWnd::OnHScroll(nSBCode, nPos, pScrollBar);
}
//垂直滚动条
void CMainWindow::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
int nDelta=0;
switch (nSBCode)
{
case SB_LINEUP:
nDelta = -LINESIZE;
break;
case SB_LINEDOWN:
nDelta = LINESIZE;
break;
case SB_PAGEUP:
nDelta = -m_nVPageSize;
break;
case SB_PAGEDOWN:
nDelta = m_nVPageSize;
break;
case SB_THUMBTRACK:
nDelta = (int)nPos - m_nVScrollPos;
break;
default:
return;
}
if (nDelta != 0) {
m_nVScrollPos += nDelta;
SetScrollPos(SB_VERT, m_nVScrollPos, TRUE);
//重点
ScrollWindow(0, -nDelta);
}
CFrameWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CMainWindow::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
int nHScrollMax = 0;
m_nHPageSize = 0;
if (cx < m_nViewWidth)
{
m_nHPageSize = cx;
nHScrollMax = m_nViewWidth - 1;
}
SCROLLINFO si;
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = nHScrollMax;
si.nPos = m_nHScrollPos;
si.nPage = m_nHPageSize;
// TODO: 在此处添加消息处理程序代码
SetScrollInfo(SB_HORZ, &si, TRUE);
int nVScrollMax = 0;
m_nVPageSize = 0;
if (cy < m_nViewHeight)
{
nVScrollMax = m_nViewHeight - 1;
m_nVPageSize = cy;
}
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = nVScrollMax;
si.nPos = m_nVScrollPos;
si.nPage = m_nVPageSize;
SetScrollInfo(SB_VERT, &si, TRUE);
}