📄 gridctrl.cpp
字号:
BOOL CGridCtrl::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
{
ASSERT(pParentWnd->GetSafeHwnd());
if (!CWnd::Create(GRIDCTRL_CLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
return FALSE;
//Initialise(); - called in PreSubclassWnd
// The number of rows and columns will only be non-zero if the constructor
// was called with non-zero initialising parameters. If this window was created
// using a dialog template then the number of rows and columns will be 0 (which
// means that the code below will not be needed - which is lucky 'cause it ain't
// gonna get called in a dialog-template-type-situation.
TRY
{
m_arRowHeights.SetSize(m_nRows); // initialize row heights
m_arColWidths.SetSize(m_nCols); // initialize column widths
}
CATCH (CMemoryException, e)
{
e->ReportError();
return FALSE;
}
END_CATCH
int i;
for (i = 0; i < m_nRows; i++)
m_arRowHeights[i] = m_cellDefault.GetHeight();
for (i = 0; i < m_nCols; i++)
m_arColWidths[i] = m_cellDefault.GetWidth();
return TRUE;
}
void CGridCtrl::SetupDefaultCells()
{
m_cellDefault.SetGrid(this); // Normal editable cell
m_cellFixedColDef.SetGrid(this); // Cell for fixed columns
m_cellFixedRowDef.SetGrid(this); // Cell for fixed rows
m_cellFixedRowColDef.SetGrid(this); // Cell for area overlapped by fixed columns/rows
m_cellDefault.SetTextClr(m_crWindowText);
m_cellDefault.SetBackClr(m_crWindowColour);
m_cellFixedColDef.SetTextClr(m_crWindowText);
m_cellFixedColDef.SetBackClr(m_cr3DFace);
m_cellFixedRowDef.SetTextClr(m_crWindowText);
m_cellFixedRowDef.SetBackClr(m_cr3DFace);
m_cellFixedRowColDef.SetTextClr(m_crWindowText);
m_cellFixedRowColDef.SetBackClr(m_cr3DFace);
}
void CGridCtrl::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
//HFONT hFont = ::CreateFontIndirect(m_cellDefault.GetFont());
//OnSetFont((LPARAM)hFont, 0);
//DeleteObject(hFont);
Initialise();
}
// Sends a message to the parent in the form of a WM_NOTIFY message with
// a NM_GRIDVIEW structure attached
LRESULT CGridCtrl::SendMessageToParent(int nRow, int nCol, int nMessage) const
{
if (!IsWindow(m_hWnd))
return 0;
NM_GRIDVIEW nmgv;
nmgv.iRow = nRow;
nmgv.iColumn = nCol;
nmgv.hdr.hwndFrom = m_hWnd;
nmgv.hdr.idFrom = GetDlgCtrlID();
nmgv.hdr.code = nMessage;
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, nmgv.hdr.idFrom, (LPARAM)&nmgv);
else
return 0;
}
// Send a request to the parent to return information on a given cell
LRESULT CGridCtrl::SendDisplayRequestToParent(GV_DISPINFO* pDisplayInfo) const
{
if (!IsWindow(m_hWnd))
return 0;
// Fix up the message headers
pDisplayInfo->hdr.hwndFrom = m_hWnd;
pDisplayInfo->hdr.idFrom = GetDlgCtrlID();
pDisplayInfo->hdr.code = GVN_GETDISPINFO;
// Send the message
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, pDisplayInfo->hdr.idFrom, (LPARAM)pDisplayInfo);
else
return 0;
}
// Send a hint to the parent about caching information
LRESULT CGridCtrl::SendCacheHintToParent(const CCellRange& range) const
{
if (!IsWindow(m_hWnd))
return 0;
GV_CACHEHINT CacheHint;
// Fix up the message headers
CacheHint.hdr.hwndFrom = m_hWnd;
CacheHint.hdr.idFrom = GetDlgCtrlID();
CacheHint.hdr.code = GVN_ODCACHEHINT;
CacheHint.range = range;
// Send the message
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, CacheHint.hdr.idFrom, (LPARAM)&CacheHint);
else
return 0;
}
#define LAYER_SIGNATURE (0x5FD4E64)
int CGridCtrl::GetLayer(int** pLayer) // used to save and restore order of columns
{ // gives back the size of the area (do not forget to delete pLayer)
int Length = 2+GetColumnCount()*2;
int *Layer = new int[Length]; // the caller is supposed to delete it
Layer[0]= LAYER_SIGNATURE;
Layer[1]= GetColumnCount();
memcpy(&Layer[2], &m_arColOrder[0], GetColumnCount()*sizeof(int));
memcpy(&Layer[2+GetColumnCount()], &m_arColWidths[0], GetColumnCount()*sizeof(int));
*pLayer = Layer;
return Length;
}
void CGridCtrl::SetLayer(int* pLayer)
{ // coming from a previous GetLayer (ignored if not same number of column, or the same revision number)
if(pLayer[0] != LAYER_SIGNATURE) return;
if(pLayer[1] != GetColumnCount()) return;
/* TRACE(" %d == %d \n",m_arColOrder[0],pLayer[2]);
TRACE(" %d == %d \n",m_arColOrder[1],pLayer[3]);
TRACE(" %d == %d \n",m_arColOrder[2],pLayer[4]);
TRACE(" %d == %d \n",m_arColWidths[0],pLayer[2+3]);
TRACE(" %d == %d \n",m_arColWidths[1],pLayer[3+3]);
TRACE(" %d == %d \n",m_arColWidths[2],pLayer[4+3]);
TRACE(" %d == %d \n",GetColumnCount(),3);
ASSERT(m_arColOrder[0]==pLayer[2]);
ASSERT(m_arColOrder[1]==pLayer[3]);
ASSERT(m_arColOrder[2]==pLayer[4]);
ASSERT(m_arColWidths[0]==pLayer[2+3]);
ASSERT(m_arColWidths[1]==pLayer[3+3]);
ASSERT(m_arColWidths[2]==pLayer[4+3]);
ASSERT(GetColumnCount()==3);
*/ memcpy(&m_arColOrder[0],&pLayer[2], GetColumnCount()*sizeof(int));
memcpy(&m_arColWidths[0],&pLayer[2+GetColumnCount()], GetColumnCount()*sizeof(int));
}
BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
//EFW - Added ON_WM_RBUTTONUP
//{{AFX_MSG_MAP(CGridCtrl)
ON_WM_PAINT()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_SIZE()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_GETDLGCODE()
ON_WM_KEYDOWN()
ON_WM_CHAR()
ON_WM_LBUTTONDBLCLK()
ON_WM_ERASEBKGND()
ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
ON_WM_SYSKEYDOWN()
//}}AFX_MSG_MAP
#ifndef _WIN32_WCE_NO_CURSOR
ON_WM_SETCURSOR()
#endif
#ifndef _WIN32_WCE
ON_WM_RBUTTONUP()
ON_WM_SYSCOLORCHANGE()
ON_WM_CAPTURECHANGED()
#endif
#ifndef GRIDCONTROL_NO_CLIPBOARD
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
#endif
#if (_WIN32_WCE >= 210)
ON_WM_SETTINGCHANGE()
#endif
#if !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
ON_WM_MOUSEWHEEL()
#endif
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
ON_MESSAGE(WM_IME_CHAR, OnImeChar)
ON_NOTIFY(GVN_ENDLABELEDIT, IDC_INPLACE_CONTROL, OnEndInPlaceEdit)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl message handlers
void CGridCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (m_bDoubleBuffer) // Use a memory DC to remove flicker
{
CMemDC MemDC(&dc);
OnDraw(&MemDC);
}
else // Draw raw - this helps in debugging vis problems.
OnDraw(&dc);
}
BOOL CGridCtrl::OnEraseBkgnd(CDC* /*pDC*/)
{
return TRUE; // Don't erase the background.
}
// Custom background erasure. This gets called from within the OnDraw function,
// since we will (most likely) be using a memory DC to stop flicker. If we just
// erase the background normally through OnEraseBkgnd, and didn't fill the memDC's
// selected bitmap with colour, then all sorts of vis problems would occur
void CGridCtrl::EraseBkgnd(CDC* pDC)
{
CRect VisRect, ClipRect, rect;
CBrush FixedRowColBack(GetDefaultCell(TRUE, TRUE)->GetBackClr()),
FixedRowBack(GetDefaultCell(TRUE, FALSE)->GetBackClr()),
FixedColBack(GetDefaultCell(FALSE, TRUE)->GetBackClr()),
TextBack(GetDefaultCell(FALSE, FALSE)->GetBackClr());
CBrush Back(GetGridBkColor());
//CBrush Back(GetTextBkColor());
if (pDC->GetClipBox(ClipRect) == ERROR)
return;
GetVisibleNonFixedCellRange(VisRect);
int nFixedColumnWidth = GetFixedColumnWidth();
int nFixedRowHeight = GetFixedRowHeight();
// Draw Fixed row/column background
if (ClipRect.left < nFixedColumnWidth && ClipRect.top < nFixedRowHeight)
pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
nFixedColumnWidth, nFixedRowHeight),
&FixedRowColBack);
// Draw Fixed columns background
if (ClipRect.left < nFixedColumnWidth && ClipRect.top < VisRect.bottom)
pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
nFixedColumnWidth, VisRect.bottom),
&FixedColBack);
// Draw Fixed rows background
if (ClipRect.top < nFixedRowHeight &&
ClipRect.right > nFixedColumnWidth && ClipRect.left < VisRect.right)
pDC->FillRect(CRect(nFixedColumnWidth-1, ClipRect.top,
VisRect.right, nFixedRowHeight),
&FixedRowBack);
// Draw non-fixed cell background
if (rect.IntersectRect(VisRect, ClipRect))
{
CRect CellRect(__max(nFixedColumnWidth, rect.left),
__max(nFixedRowHeight, rect.top),
rect.right, rect.bottom);
pDC->FillRect(CellRect, &TextBack);
}
// Draw right hand side of window outside grid
if (VisRect.right < ClipRect.right)
pDC->FillRect(CRect(VisRect.right, ClipRect.top,
ClipRect.right, ClipRect.bottom),
&Back);
// Draw bottom of window below grid
if (VisRect.bottom < ClipRect.bottom && ClipRect.left < VisRect.right)
pDC->FillRect(CRect(ClipRect.left, VisRect.bottom,
VisRect.right, ClipRect.bottom),
&Back);
}
void CGridCtrl::OnSize(UINT nType, int cx, int cy)
{
static BOOL bAlreadyInsideThisProcedure = FALSE;
if (bAlreadyInsideThisProcedure)
return;
if (!::IsWindow(m_hWnd))
return;
// This is not the ideal place to register the droptarget
#ifndef GRIDCONTROL_NO_DRAGDROP
m_DropTarget.Register(this);
#endif
// Start re-entry blocking
bAlreadyInsideThisProcedure = TRUE;
EndEditing(); // destroy any InPlaceEdit's
CWnd::OnSize(nType, cx, cy);
ResetScrollBars();
// End re-entry blocking
bAlreadyInsideThisProcedure = FALSE;
}
UINT CGridCtrl::OnGetDlgCode()
{
UINT nCode = DLGC_WANTARROWS | DLGC_WANTCHARS; // DLGC_WANTALLKEYS; //
if (m_bHandleTabKey && !IsCTRLpressed())
nCode |= DLGC_WANTTAB;
return nCode;
}
#ifndef _WIN32_WCE
// If system colours change, then redo colours
void CGridCtrl::OnSysColorChange()
{
CWnd::OnSysColorChange();
if (GetDefaultCell(FALSE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(FALSE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetGridBkColor() == m_crShadow)
SetGridBkColor(::GetSysColor(COLOR_3DSHADOW));
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
}
#endif
#ifndef _WIN32_WCE_NO_CURSOR
// If we are drag-selecting cells, or drag and dropping, stop now
void CGridCtrl::OnCaptureChanged(CWnd *pWnd)
{
if (pWnd->GetSafeHwnd() == GetSafeHwnd())
return;
// kill timer if active
if (m_nTimerID != 0)
{
KillTimer(m_nTimerID);
m_nTimerID = 0;
}
#ifndef GRIDCONTROL_NO_DRAGDROP
// Kill drag and drop if active
if (m_MouseMode == MOUSE_DRAGGING)
m_MouseMode = MOUSE_NOTHING;
#endif
}
#endif
#if (_MFC_VER >= 0x0421) || (_WIN32_WCE >= 210)
// If system settings change, then redo colours
void CGridCtrl::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -