📄 scrolldemoview.cpp
字号:
// ScrollDemoView.cpp : implementation of the CScrollDemoView class
//
#include "stdafx.h"
#include "ScrollDemo.h"
#include "ScrollDemoDoc.h"
#include "ScrollDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CScrollDemoView
IMPLEMENT_DYNCREATE(CScrollDemoView, CScrollView)
BEGIN_MESSAGE_MAP(CScrollDemoView, CScrollView)
//{{AFX_MSG_MAP(CScrollDemoView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScrollDemoView construction/destruction
CScrollDemoView::CScrollDemoView()
{
// 设置字体
m_font.CreatePointFont (80, _T ("MS Sans Serif"));
}
CScrollDemoView::~CScrollDemoView()
{
}
BOOL CScrollDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CScrollDemoView drawing
void CScrollDemoView::OnDraw(CDC* pDC)
{
CScrollDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//
// 画表格线.
//
// 获得视的逻辑宽度与高度
CSize size = GetTotalSize();
// 设置画笔
CPen pen(PS_SOLID, 0, RGB(192, 192, 192));
// 将创建的画笔选入设备环境,并保存原有画笔
CPen* pOldPen = pDC->SelectObject(&pen);
// 画表格横线
for (int i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
pDC->MoveTo(0, y);
pDC->LineTo(size.cx, y);
}
// 画表格竖线
for (int j = 0; j < 26; j++)
{
int x = (j * m_nCellWidth) + m_nRibbonWidth;
pDC->MoveTo(x, 0);
pDC->LineTo(x, size.cy);
}
// 恢复旧画笔
pDC->SelectObject(pOldPen);
//
// 绘制行/列表头.
//
// 创建画刷
CBrush brush;
brush.CreateStockObject(LTGRAY_BRUSH);
// 绘制行表头
CRect rcTop(0, 0, size.cx, m_nCellHeight);
pDC->FillRect(rcTop, &brush);
// 绘制列表头
CRect rcLeft(0, 0, m_nRibbonWidth, size.cy);
pDC->FillRect(rcLeft, &brush);
// 绘制行表头横线
pDC->MoveTo(0, m_nCellHeight);
pDC->LineTo(size.cx, m_nCellHeight);
// 绘制列表头竖线
pDC->MoveTo(m_nRibbonWidth, 0);
pDC->LineTo(m_nRibbonWidth, size.cy);
// 设置背景混合模式为透明模式
pDC->SetBkMode(TRANSPARENT);
//
// 绘制列表头的按钮样式并添加数字
//
for(i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
pDC->MoveTo(0, y);
pDC->LineTo(m_nRibbonWidth, y);
CString string;
string.Format(_T ("%d"), i + 1);
CRect rect (0, y, m_nRibbonWidth, y + m_nCellHeight);
// 输出文本
pDC->DrawText(string, &rect, DT_SINGLELINE |
DT_CENTER | DT_VCENTER);
rect.top++;
// 绘制三维矩形
pDC->Draw3dRect(rect, RGB(255, 255, 255),
RGB(128, 128, 128));
}
//
// 绘制行表头的按钮样式并添加字母
//
for (j=0; j<26; j++)
{
int x = (j * m_nCellWidth) + m_nRibbonWidth;
pDC->MoveTo(x, 0);
pDC->LineTo(x, m_nCellHeight);
CString string;
string.Format(_T ("%c"), j + 'A');
CRect rect (x, 0, x + m_nCellWidth, m_nCellHeight);
// 输出文本
pDC->DrawText (string, &rect, DT_SINGLELINE |
DT_CENTER | DT_VCENTER);
rect.left++;
// 绘制三维矩形
pDC->Draw3dRect(rect, RGB(255, 255, 255),
RGB(128, 128, 128));
}
//
// 绘制每个单元格的编号
//
CRect rect;
pDC->GetClipBox (&rect);
int nStartRow = max(0, (rect.top - m_nCellHeight) / m_nCellHeight);
int nEndRow = min(98, (rect.bottom - 1) / m_nCellHeight);
int nStartCol = max(0, (rect.left - m_nRibbonWidth) / m_nCellWidth);
int nEndCol = min(25, ((rect.right + m_nCellWidth - 1) -
m_nRibbonWidth) / m_nCellWidth);
for (i = nStartRow; i <= nEndRow; i++)
for (j = nStartCol; j <= nEndCol; j++)
DrawAddress(pDC, i, j);
//
// 绘制选中单元格的颜色.
//
DrawPointer(pDC, m_nCurrentRow, m_nCurrentCol, TRUE);
}
void CScrollDemoView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// 初始化成员
m_nCurrentRow = 0;
m_nCurrentCol = 0;
m_bSmooth = FALSE;
// 获得客户区设备上下文
CClientDC dc(this);
// 获得每英寸像素点数
m_nCellWidth = dc.GetDeviceCaps(LOGPIXELSX);
m_nCellHeight = dc.GetDeviceCaps(LOGPIXELSY) / 4;
m_nRibbonWidth = m_nCellWidth / 2;
int nWidth = (26 * m_nCellWidth) + m_nRibbonWidth;
int nHeight = m_nCellHeight * 100;
// 初始化滚动尺寸
SetScrollSizes (MM_TEXT, CSize (nWidth, nHeight));
}
/////////////////////////////////////////////////////////////////////////////
// CScrollDemoView diagnostics
#ifdef _DEBUG
void CScrollDemoView::AssertValid() const
{
CScrollView::AssertValid();
}
void CScrollDemoView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CScrollDemoDoc* CScrollDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScrollDemoDoc)));
return (CScrollDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CScrollDemoView message handlers
void CScrollDemoView::OnLButtonDown(UINT nFlags, CPoint point)
{
CScrollView::OnLButtonDown(nFlags, point);
//
// 将当前点的坐标转换为逻辑坐标
//
CPoint pos = point;
CClientDC dc(this);
//
// 让MFC在输出中考虑映射模式和滚动位置的影响
//
OnPrepareDC(&dc);
dc.DPtoLP(&pos);
//
// 若击中某一单元格,移动单元格指示颜色到击中的单元格
//
CSize size = GetTotalSize();
if (pos.x > m_nRibbonWidth && pos.x < size.cx &&
pos.y > m_nCellHeight && pos.y < size.cy)
{
int row = (pos.y - m_nCellHeight) / m_nCellHeight;
int col = (pos.x - m_nRibbonWidth) / m_nCellWidth;
ASSERT (row >= 0 && row <= 98 && col >= 0 && col <= 25);
DrawPointer(&dc, m_nCurrentRow, m_nCurrentCol, FALSE);
m_nCurrentRow = row;
m_nCurrentCol = col;
DrawPointer(&dc, m_nCurrentRow, m_nCurrentCol, TRUE);
}
}
void CScrollDemoView::DrawPointer(CDC *pDC, int row, int col, BOOL bHighlight)
{
CRect rect;
// 获得单元格矩形大小
GetCellRect(row, col, &rect);
// 创建画刷
CBrush brush(bHighlight ? RGB(0, 255, 255) : ::GetSysColor(COLOR_WINDOW));
// 绘制矩形
pDC->FillRect(rect, &brush);
// 绘制单元格编号
DrawAddress(pDC, row, col);
}
void CScrollDemoView::DrawAddress(CDC *pDC, int row, int col)
{
CRect rect;
// 获得单元格矩形大小
GetCellRect(row, col, &rect);
// 创建单元格编号字符串
CString string;
string.Format(_T ("%c%d"), col + _T ('A'), row + 1);
// 设置透明模式
pDC->SetBkMode(TRANSPARENT);
// 选入字体
CFont* pOldFont = pDC->SelectObject(&m_font);
// 输出字符串
pDC->DrawText(string, rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
// 恢复原字体
pDC->SelectObject(pOldFont);
}
// 获得单元格所在的矩形位置
void CScrollDemoView::GetCellRect(int row, int col, LPRECT pRect)
{
pRect->left = m_nRibbonWidth + (col * m_nCellWidth) + 1;
pRect->top = m_nCellHeight + (row * m_nCellHeight) + 1;
pRect->right = pRect->left + m_nCellWidth - 1;
pRect->bottom = pRect->top + m_nCellHeight - 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -