📄 gridctrl.cpp
字号:
// GridCtrl.cpp : implementation file
//
// MFC Grid Control
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MemDC.h"
#include "GridCtrl.h"
// OLE stuff for clipboard operations
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////////
#define SELECTED_CELL_FONT_WEIGHT 600 // wieght of text for selected items
///////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CGridCtrl, CWnd)
////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
void AFXAPI DDX_GridControl(CDataExchange* pDX, int nIDC, CGridCtrl& rControl)
{
if (rControl.GetSafeHwnd() == NULL) // not subclassed yet
{
ASSERT(!pDX->m_bSaveAndValidate);
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (!rControl.SubclassWindow(hWndCtrl))
{
ASSERT(FALSE);
AfxThrowNotSupportedException();
}
#ifndef _AFX_NO_OCC_SUPPORT
else
{
if (pDX->m_pDlgWnd->GetSafeHwnd() != ::GetParent(rControl.GetSafeHwnd()))
rControl.AttachControlSite(pDX->m_pDlgWnd);
}
#endif //!_AFX_NO_OCC_SUPPORT
}
}
//////////////////////////////////////////////////////////////////
// Get the number of lines to scroll with each mouse wheel notch
// Why doesn't windows give us this function???
/////////////////////////////////////////////////////////////////
UINT GetMouseScrollLines()
{
int nScrollLines = 3;
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"),
0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
TCHAR szData[128];
DWORD dwKeyDataType;
DWORD dwDataBufSize = sizeof(szData);
if (RegQueryValueEx(hKey, _T("WheelScrollLines"), NULL, &dwKeyDataType,
(LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
{
nScrollLines = _tcstoul(szData, NULL, 10);
}
RegCloseKey(hKey);
}
return nScrollLines;
}
/////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
CGridCtrl::CGridCtrl(int nRows, int nCols, int nFixedRows, int nFixedCols)
{
RegisterWindowClass();
m_pParentWnd=NULL;
m_bMustUninitOLE = FALSE;
#ifdef GRIDCONTROL_USE_OLE_DRAGDROP
_AFX_THREAD_STATE* pState = AfxGetThreadState();
if (!pState->m_bNeedTerm)
{
SCODE sc = ::OleInitialize(NULL);
if (FAILED(sc))
AfxMessageBox(_T("OLE initialization failed. Make sure that the OLE libraries are the correct version"));
else
m_bMustUninitOLE = TRUE;
}
#endif
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
m_crGridColour = RGB(0,0,0);
m_nRows = 0;
m_nCols = 0;
m_nFixedRows = 0;
m_nFixedCols = 0;
m_nDefCellHeight = 10;
m_nDefCellHeight = 30;
m_nVScrollMax = 0;
m_nHScrollMax = 0;
m_nMargin = 0;
m_nRowsPerWheelNotch = GetMouseScrollLines();
m_MouseMode = MOUSE_NOTHING;
m_nGridLines = GVL_BOTH;
m_bEditable = TRUE;
m_bListMode = FALSE;
m_bAllowDraw = TRUE;
m_bEnableSelection = TRUE;
m_bAllowRowResize = TRUE;
m_bAllowColumnResize = TRUE;
m_bSortOnClick = TRUE;
m_bHandleTabKey = TRUE;
m_bDoubleBuffer = TRUE;
m_bTitleTips = TRUE;
m_bAscending = TRUE;
m_SortColumn = -1;
m_nTimerID = 0;
m_nTimerInterval = 25;
m_nResizeCaptureRange = 3;
m_pImageList = NULL;
m_bAllowDragAndDrop = FALSE;
m_nHeaderHeight=60;
m_nFootHeight=10;
m_nLeft=40;
m_nRight=10;
m_nTop=10;
m_nBottom=10;
m_nHeaderGap=0;//头与表首行间的间距
SetRowCount(nRows);
SetColumnCount(nCols);
SetFixedRowCount(nFixedRows);
SetFixedColumnCount(nFixedCols);
SetTextColor(m_crWindowText);
SetTextBkColor(m_crWindowColour);
SetBkColor(m_crShadow);
SetFixedTextColor(m_crWindowText);
SetFixedBkColor(m_cr3DFace);
m_SelectedCellMap.RemoveAll();
m_PrevSelectedCellMap.RemoveAll();
}
////////////////////////////////////////
//
///////////////////////////////////////
CGridCtrl::~CGridCtrl()
{
if (m_pToolTip)
{
delete (m_pToolTip);
m_pToolTip = NULL;
}
DeleteAllItems();
m_pParentWnd=NULL;
DestroyWindow();
m_Font.DeleteObject();
if (m_bMustUninitOLE)
::OleUninitialize();
}
///////////////////////////////////////////
//
////////////////////////////////////////////
BOOL CGridCtrl::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetResourceHandle();
if (!(::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls)))
{
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = NULL;
CBrush Brush(RGB(255,255,255));
wndcls.hbrBackground = (HBRUSH) (Brush.operator HBRUSH());//COLOR_3DFACE + 1
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = GRIDCTRL_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
BOOL CGridCtrl::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
{
ASSERT(pParentWnd->GetSafeHwnd());
m_pParentWnd=(CView *)pParentWnd;
ASSERT(m_pParentWnd!=NULL);
if (!CWnd::Create(GRIDCTRL_CLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
return FALSE;
#ifdef GRIDCONTROL_USE_OLE_DRAGDROP
m_DropTarget.Register(this);
#endif
#ifdef GRIDCONTROL_USE_TITLETIPS
if (m_bTitleTips)
m_TitleTip.Create(this);
#endif
try
{
m_arRowHeights.SetSize(m_nRows);
m_arColWidths.SetSize(m_nCols);
}
catch (CMemoryException *e)
{
e->ReportError();
e->Delete();
return FALSE;
}
for (int i = 0; i < m_nRows; i++)
m_arRowHeights[i] = m_nDefCellHeight;
for (i = 0; i < m_nCols; i++)
m_arColWidths[i] = m_nDefCellWidth;
ResetScrollBars();
return TRUE;
}
//////////////////////////////////////
//
/////////////////////////////////////
void CGridCtrl::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
CDC *pDC=GetDC();
m_Logfont.lfHeight=MulDiv (9, pDC->GetDeviceCaps (LOGPIXELSY), 72); //-12;
m_Logfont.lfWidth=0;
m_Logfont.lfEscapement=0;
m_Logfont.lfOrientation=0;
m_Logfont.lfWeight=FW_NORMAL;
m_Logfont.lfItalic=0;
m_Logfont.lfUnderline=0;
m_Logfont.lfStrikeOut=0;
m_Logfont.lfCharSet=134;
m_Logfont.lfOutPrecision=3;
m_Logfont.lfClipPrecision=2;
m_Logfont.lfQuality=0;
m_Logfont.lfPitchAndFamily=1;
TCHAR FontName[]="宋体";
strcpy(m_Logfont.lfFaceName,FontName);
CFont Font;
Font.CreateFontIndirect(&m_Logfont);
OnSetFont((LPARAM)(Font.operator HFONT()), 0);
ReleaseDC(pDC);
ResetScrollBars();
}
///////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
BOOL CGridCtrl::SubclassWindow(HWND hWnd)
{
if (!CWnd::SubclassWindow(hWnd))
return FALSE;
CreateTooltips();
return TRUE;
}
////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////
BOOL CGridCtrl::SendMessageToParent(int nRow, int nCol, int nMessage,CString str)
{
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;
nmgv.sTxt=str;
nmgv.bValid=TRUE;
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
pOwner->SendMessage(WM_NOTIFY, nmgv.hdr.idFrom, (LPARAM)&nmgv);
return nmgv.bValid;
}
////////////////////////////////////////
//
///////////////////////////////////////
BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
//{{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_WM_SETCURSOR()
ON_WM_SYSCOLORCHANGE()
ON_WM_CAPTURECHANGED()
//}}AFX_MSG_MAP
#if _MFC_VER >= 0x0421
ON_WM_MOUSEWHEEL()
ON_WM_SETTINGCHANGE()
#endif
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT,0,0xFFFF,OnToolTipNotify)
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int CGridCtrl::GetRowCount() const
{
return m_nRows;
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int CGridCtrl::GetColumnCount() const
{
return m_nCols;
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int CGridCtrl::GetFixedRowCount() const
{
return m_nFixedRows;
}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int CGridCtrl::GetFixedColumnCount() const
{
return m_nFixedCols;
}
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
void CGridCtrl::SetTextColor(COLORREF clr)
{
m_crTextColour = clr;
}
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
COLORREF CGridCtrl::GetTextColor() const
{
return m_crTextColour;
}
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
void CGridCtrl::SetTextBkColor(COLORREF clr)
{
m_crTextBkColour = clr;
}
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
COLORREF CGridCtrl::GetTextBkColor() const
{
return m_crTextBkColour;
}
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
void CGridCtrl::SetBkColor(COLORREF clr)
{
m_crBkColour = clr;
}
///////////////////////////////////////////////////////////////////////
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -