📄 cgridctrl.cpp
字号:
{
case VK_TAB:
if (IsSHIFTpressed())
{
if (next.col > m_nFixedCols)
{
next.col--;
}
else if (next.col == m_nFixedCols && next.row > m_nFixedRows)
{
next.row--;
next.col = GetColumnCount() - 1;
bChangeLine = TRUE;
}
else
{
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
}
else
{
if(next.col < (GetColumnCount() - 1))
{
next.col++;
}
else if (next.col == (GetColumnCount() - 1) && next.row < (GetRowCount() - 1) )
{
next.row++;
next.col = m_nFixedCols;
bChangeLine = TRUE;
}
else
{
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
}
break;
case VK_DOWN:
if (next.row < (GetRowCount() - 1))
{
next.row++;
}
break;
case VK_UP:
if (next.row > m_nFixedRows)
{
next.row--;
}
break;
case VK_RIGHT:
if (next.col < (GetColumnCount() - 1))
{
next.col++;
}
break;
case VK_LEFT:
if (next.col > m_nFixedCols)
{
next.col--;
}
break;
case VK_NEXT:
{
CCellID idOldTopLeft = GetTopleftNonFixedCell();
SendMessage(WM_VSCROLL, SB_PAGEDOWN, 0);
CCellID idNewTopLeft = GetTopleftNonFixedCell();
int increment = idNewTopLeft.row - idOldTopLeft.row;
if (increment)
{
next.row += increment;
if (next.row > (GetRowCount() - 1))
{
next.row = GetRowCount() - 1;
}
}
else
{
next.row = GetRowCount() - 1;
}
}
break;
case VK_PRIOR:
{
CCellID idOldTopLeft = GetTopleftNonFixedCell();
SendMessage(WM_VSCROLL, SB_PAGEUP, 0);
CCellID idNewTopLeft = GetTopleftNonFixedCell();
int increment = idNewTopLeft.row - idOldTopLeft.row;
if (increment)
{
next.row += increment;
if (next.row < m_nFixedRows)
{
next.row = m_nFixedRows;
}
}
else
{
next.row = m_nFixedRows;
}
}
break;
case VK_HOME:
SendMessage(WM_VSCROLL, SB_TOP, 0);
next.row = m_nFixedRows;
break;
case VK_END:
SendMessage(WM_VSCROLL, SB_BOTTOM, 0);
next.row = GetRowCount() - 1;
break;
default:
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
if (next != m_idCurrentCell)
{
if (m_MouseMode == MOUSE_NOTHING)
{
m_PrevSelectedCellMap.RemoveAll();
m_MouseMode = m_bListMode? MOUSE_SELECT_ROW : MOUSE_SELECT_CELLS;
if (!IsSHIFTpressed() || nChar == VK_TAB)
{
m_SelectionStartCell = next;
}
OnSelecting(next);
m_MouseMode = MOUSE_NOTHING;
}
SetFocusCell(next);
if (!IsCellVisible(next))
{
EnsureVisible(next);
switch (nChar)
{
case VK_RIGHT:
SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
break;
case VK_LEFT:
SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
break;
case VK_DOWN:
SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
break;
case VK_UP:
SendMessage(WM_VSCROLL, SB_LINEUP, 0);
break;
case VK_TAB:
if(IsSHIFTpressed())
{
if (bChangeLine)
{
SendMessage(WM_VSCROLL, SB_LINEUP, 0);
SetScrollPos32(SB_HORZ, m_nHScrollMax);
break;
}
else
{
SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
}
}
else
{
if (bChangeLine)
{
SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
SetScrollPos32(SB_HORZ, 0);
break;
}
else
{
SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
}
}
break;
}
Invalidate();
}
}
}
void CGridCtrl::OnHScroll(UINT nSBCode, UINT /*nPos*/, CScrollBar* /*pScrollBar*/)
{
if (GetFocus()->GetSafeHwnd() != GetSafeHwnd())
{
SetFocus();
}
int scrollPos = GetScrollPos32(SB_HORZ);
CCellID idTopLeft = GetTopleftNonFixedCell();
CRect rect;
GetClientRect(rect);
switch (nSBCode)
{
case SB_LINERIGHT:
if (scrollPos < m_nHScrollMax)
{
int xScroll = GetColumnWidth(idTopLeft.col);
SetScrollPos32(SB_HORZ, scrollPos + xScroll);
if (GetScrollPos32(SB_HORZ) == scrollPos)
break;
rect.left = GetFixedColumnWidth() + xScroll;
ScrollWindow(-xScroll, 0, rect);
rect.left = rect.right - xScroll;
InvalidateRect(rect);
}
break;
case SB_LINELEFT:
if (scrollPos > 0 && idTopLeft.col > GetFixedColumnCount())
{
int xScroll = GetColumnWidth(idTopLeft.col-1);
SetScrollPos32(SB_HORZ, max(0,scrollPos - xScroll));
rect.left = GetFixedColumnWidth();
ScrollWindow(xScroll, 0, rect);
rect.right = rect.left + xScroll;
InvalidateRect(rect);
}
break;
case SB_PAGERIGHT:
if(scrollPos < m_nHScrollMax)
{
rect.left = GetFixedColumnWidth();
int offset = rect.Width();
int pos = min(m_nHScrollMax, scrollPos + offset);
SetScrollPos32(SB_HORZ, pos);
rect.left = GetFixedColumnWidth();
InvalidateRect(rect);
}
break;
case SB_PAGELEFT:
if (scrollPos > 0)
{
rect.left = GetFixedColumnWidth();
int offset = -rect.Width();
int pos = max(0, scrollPos + offset);
SetScrollPos32(SB_HORZ, pos);
rect.left = GetFixedColumnWidth();
InvalidateRect(rect);
}
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
SetScrollPos32(SB_HORZ, GetScrollPos32(SB_HORZ, TRUE));
rect.left = GetFixedColumnWidth();
InvalidateRect(rect);
break;
case SB_LEFT:
if (scrollPos > 0)
{
SetScrollPos32(SB_HORZ, 0);
Invalidate();
}
break;
case SB_RIGHT:
if(scrollPos < m_nHScrollMax)
{
SetScrollPos32(SB_HORZ, m_nHScrollMax);
Invalidate();
}
break;
default:
break;
}
}
void CGridCtrl::OnVScroll(UINT nSBCode, UINT /*nPos*/, CScrollBar* /*pScrollBar*/)
{
if (GetFocus()->GetSafeHwnd() != GetSafeHwnd())
SetFocus();
int scrollPos = GetScrollPos32(SB_VERT);
CCellID idTopLeft = GetTopleftNonFixedCell();
CRect rect;
GetClientRect(rect);
switch (nSBCode)
{
case SB_LINEDOWN:
if (scrollPos < m_nVScrollMax)
{
int yScroll = GetRowHeight(idTopLeft.row);
SetScrollPos32(SB_VERT, scrollPos + yScroll);
if (GetScrollPos32(SB_VERT) == scrollPos)
break;
rect.top = GetFixedRowHeight() + yScroll;
ScrollWindow( 0, -yScroll, rect);
rect.top = rect.bottom - yScroll;
InvalidateRect(rect);
}
break;
case SB_LINEUP:
if (scrollPos > 0 && idTopLeft.row > GetFixedRowCount())
{
int yScroll = GetRowHeight(idTopLeft.row-1);
SetScrollPos32(SB_VERT, max(0, scrollPos - yScroll));
rect.top = GetFixedRowHeight();
ScrollWindow(0, yScroll, rect);
rect.bottom = rect.top + yScroll;
InvalidateRect(rect);
}
break;
case SB_PAGEDOWN:
if(scrollPos < m_nVScrollMax)
{
rect.top = GetFixedRowHeight();
scrollPos = min(m_nVScrollMax, scrollPos + rect.Height());
SetScrollPos32(SB_VERT, scrollPos);
rect.top = GetFixedRowHeight();
InvalidateRect(rect);
}
break;
case SB_PAGEUP:
if(scrollPos > 0)
{
rect.top = GetFixedRowHeight();
int offset = -rect.Height();
int pos = max(0, scrollPos + offset);
SetScrollPos32(SB_VERT, pos);
rect.top = GetFixedRowHeight();
InvalidateRect(rect);
}
break;
case SB_THUMBPOSITION:
SetScrollPos32(SB_VERT, GetScrollPos32(SB_VERT, TRUE));
rect.top = GetFixedRowHeight();
InvalidateRect(rect);
break;
case SB_THUMBTRACK:
{
if(m_bScrollGrid)
{
SetScrollPos32(SB_VERT, GetScrollPos32(SB_VERT, TRUE));
rect.top = GetFixedRowHeight();
InvalidateRect(rect);
}
}
break;
case SB_TOP:
if (scrollPos > 0)
{
SetScrollPos32(SB_VERT, 0);
Invalidate();
}
break;
case SB_BOTTOM:
if(scrollPos < m_nVScrollMax)
{
SetScrollPos32(SB_VERT, m_nVScrollMax);
Invalidate();
}
default:
break;
}
}
void CGridCtrl::OnDraw(CDC* pDC)
{
CRect rect; //获得矩形
int row,col; //设置行列
CRect clipRect; //剪切板矩形
if (pDC->GetClipBox(&clipRect) == ERROR) //获得剪贴板数据,如果错误,就返回
{
return;
}
EraseBkgnd(pDC); //重新绘制背景
int nFixedRowHeight = GetFixedRowHeight(); //获得固定行高度
int nFixedColWidth = GetFixedColumnWidth(); //获得固定列宽度
CCellID idTopLeft = GetTopleftNonFixedCell(); //获得第一个非固定列位置
int minVisibleRow = idTopLeft.row; //最小可见行是左上角的行位置
int minVisibleCol = idTopLeft.col; //最小可见列是左上角的列位置
CRect VisRect; //定义矩形变量-可见矩形
CCellRange VisCellRange = GetVisibleNonFixedCellRange(VisRect); //获得可见非固定列矩形
int maxVisibleRow = VisCellRange.GetMaxRow(); //最大可视行是可视范围最大的一行
int maxVisibleCol = VisCellRange.GetMaxCol(); //最大可视列是可视范围最大的一列
rect.bottom = nFixedRowHeight-1; //矩形的底部是固定行高度减1
for (row = minVisibleRow; row <= maxVisibleRow; row++) //循环最小可视行到最大可视行
{
rect.top = rect.bottom+1; //矩形的顶部等于固定行高度的底部+1
rect.bottom = rect.top + GetRowHeight(row)-1; //矩形的底部等于矩形顶部+矩形高度-1
if(rect.top>clipRect.bottom) //如果矩形顶部>剪切区域矩形的底部
{
break; //就中断退出
}
if(rect.bottom<clipRect.top) //如果矩形的底部<剪切矩形的顶部
{
continue; //就继续执行
}
rect.right = -1; //矩形的右边等于-1
for (col = 0; col < m_nFixedCols; col++) //循环从0到最大固定列
{
rect.left = rect.right+1; //矩形的左边等于矩形的右边+1
rect.right = rect.left + GetColumnWidth(col)-1; //矩形的右边等于矩形的左边+矩形的宽度-1
if (rect.left > clipRect.right) //如果矩形的左边大于剪切矩形的右边
{
break; //就中断退出
}
if (rect.right < clipRect.left) //如果矩形的右边小于剪切矩形的左边
{
continue; //就继续执行
}
DrawFixedCell(pDC, row, col, rect); //绘制固定单元格(pDC,行,列,矩形)
}
}
rect.bottom = -1; //仍旧绘制固定行
for (row = 0; row < m_nFixedRows; row++) //循环行从0到最大固定行
{
rect.top = rect.bottom+1; //矩形的顶部等于矩形的底部+1
rect.bottom = rect.top + GetRowHeight(row)-1; //矩形的底部等于矩形的顶部+行高度-1
if (rect.top > clipRect.bottom) //如果矩形的顶部大于剪切矩形的底部
{
break; //就中断退出
}
if (rect.bottom < clipRect.top) //如果矩形的底部小于剪切矩形的顶部
{
continue; //就继续执行
}
rect.right = nFixedColWidth-1; //矩形的右边等于固定列宽度-1,因为是绘制固定行,所以从固定列宽度之后再开始绘制
for (col = minVisibleCol; col <= maxVisibleCol; col++) //循环可视列=最小列,列<=最大可视列,列++
{
rect.left = rect.right+1; //矩形的左边=矩形的右边+1
rect.right = rect.left + GetColumnWidth(col)-1; //矩形的右边=矩形的左边+列宽度-1
if (rect.left > clipRect.right) //如果矩形的左边>剪切矩形的右边
{
break; //就中断退出
}
if (rect.right < clipRect.left) //如果矩形的右边<剪切矩形的左边
{
continue; //就继续执行
}
DrawFixedCell(pDC, row, col, rect); //绘制固定单元格(pDC,row,col,rect)
}
}
//然后再循环所有的网格行,对于需要绘制对齐标志的,对其进行绘制
CGridColumn* pCol=NULL;
for(col=minVisibleCol;col<=maxVisibleCol;col++)
{
pCol=m_ColData[col];
if(pCol)
{
if(pCol->m_nOrder!=0)
{
if(pCol->m_nOrder==1)
{
DrawOrderSign(col,TRUE,pDC);
}
else
{
DrawOrderSign(col,FALSE,pDC);
}
break;
}
}
}
rect.bottom = nFixedRowHeight-1; //矩形高度=固定行高度-1
for (row = minVisibleRow; row <= maxVisibleRow; row++)
{
rect.top = rect.bottom+1;
rect.bottom = rect.top + GetRowHeight(row)-1;
if (rect.top > clipRect.bottom)
{
break;
}
if (rect.bottom < clipRect.top)
{
continue;
}
rect.right = nFixedColWidth-1;
for (col = minVisibleCol; col <= maxVisibleCol; col++)
{
rect.left = rect.right+1;
rect.right = rect.left + GetColumnWidth(col)-1;
if (rect.left > clipRect.right)
{
break;
}
if(rect.right < clipRect.left)
{
continue;
}
DrawCell(pDC, row, col, rect);
}
}
CPen pen;
TRY
{
pen.CreatePen(PS_SOLID, 0, m_crGridColour);
}
CATCH (CResourceException, e)
{
e->Delete();
return;
}
END_CATCH
pDC->SelectObject(&pen);
CGridCell *pCell;
CRect removelinerect;
if(m_bLineType)
{
for(col = minVisibleCol; col <=maxVisibleCol; col++)
{
for(int i=minVisibleRow; i<=maxVisibleRow;i++)
{
pCell=GetCell(i,col);
GetCellRect(i,col,removelinerect);
if(pCell->m_bLeft)
{
pDC->MoveTo(removelinerect.left-1,removelinerect.top-1);
pDC->LineTo(removelinerect.left-1,removelinerect.bottom);
}
if(pCell->m_bTop)
{
pDC->MoveTo(removelinerect.left-1,removelinerect.top-1);
pDC->LineTo(removelinerect.right,removelinerect.top-1);
}
if(pCell->m_bRight)
{
pDC->MoveTo(removelinerect.right,removelinerect.top-1);
pDC->LineTo(removelinerect.right,removelinerect.bottom);
}
if(pCell->m_bBottom)
{
pDC->MoveTo(removelinerect.left-1,removelinerect.bottom);
pDC->LineTo(removelinerect.right,removelinerect.bottom);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -