📄 timegridctrl.cpp
字号:
// GridCtrl.cpp : implementation file
//
// MFC Grid Control
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TimeGridCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CTimeGridCtrl, CWnd)
////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
/*void AFXAPI DDX_GridControl(CDataExchange* pDX, int nIDC, CTimeGridCtrl& 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
}
}*/
/////////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
CTimeGridCtrl::CTimeGridCtrl()
{
RegisterWindowClass();
m_pToolTip = NULL;
m_pParentWnd=NULL;
m_nRows = 25;
m_nCols = 61;
m_nFixedRows = 1;
m_nFixedCols = 1;
m_nDefCellHeight = 16;
m_nDefCellWidth = 6;
m_nFixedColHeight = 2*m_nDefCellHeight;
m_nFixedCellWidth =7*m_nDefCellWidth;
SetCols();
m_bCreated=FALSE;
}
////////////////////////////////////////
//
///////////////////////////////////////
CTimeGridCtrl::~CTimeGridCtrl()
{
if (m_pToolTip)
{
delete (m_pToolTip);
m_pToolTip = NULL;
}
DeleteAllItems();
m_pParentWnd=NULL;
DestroyWindow();
}
//////////////////////////////////////
//
/////////////////////////////////////
void CTimeGridCtrl::CreateTooltips()
{
if (m_pToolTip == NULL)
{
CRect rect;
GetClientRect(rect);
m_pToolTip = new CToolTipCtrl;
if(!m_pToolTip)
{
MessageBox(_T("Unable to allocate memory for ToolTips!"));
}
else
{
if( !m_pToolTip->Create(this, TTS_ALWAYSTIP) )
{
MessageBox(_T("Unable to Create ToolTips for Grid!"));
}
else
{
m_pToolTip->AddTool(this, LPSTR_TEXTCALLBACK, rect, 1);
m_pToolTip->Activate(TRUE);
m_pToolTip->SendMessage(TTM_SETDELAYTIME,TTDT_AUTOPOP,30000);
EnableToolTips(TRUE);
}
}
}
}
//////////////////////////////////////
//
/////////////////////////////////////
BOOL CTimeGridCtrl::DeleteAllItems()
{
for (int row = 0; row < m_nRows; row++)
{
GRID_ROW* pRow = m_RowData[row];
if (!pRow) continue;
for (int col = 0; col < m_nCols; col++)
{
CGridCell* pCell = pRow->GetAt(col);
if (pCell)
{
delete pCell; // better to call m_SelectedCells.RemoveAll()
} // instead. This is safer for changes though.
}
delete pRow;
}
m_RowData.RemoveAll();
m_nRows = m_nFixedRows = m_nCols = m_nFixedCols = 0;
return TRUE;
}
///////////////////////////////////////////
//
////////////////////////////////////////////
BOOL CTimeGridCtrl::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 CTimeGridCtrl::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;
return TRUE;
}
///////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
BOOL CTimeGridCtrl::SubclassWindow(HWND hWnd)
{
if (!CWnd::SubclassWindow(hWnd))
return FALSE;
CreateTooltips();
return TRUE;
}
////////////////////////////////////////
//
///////////////////////////////////////
BEGIN_MESSAGE_MAP(CTimeGridCtrl, CWnd)
//{{AFX_MSG_MAP(CTimeGridCtrl)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT,0,0xFFFF,OnToolTipNotify)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////
void CTimeGridCtrl::OnPaint()
{
CPaintDC dc(this);
if(!m_bCreated)
{
CreateTooltips();
m_bCreated=TRUE;
}
OnDraw(&dc);
}
/////////////////////////////////////////////
//
/////////////////////////////////////////////
BOOL CTimeGridCtrl::OnEraseBkgnd(CDC* /*pDC*/)
{
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTimeGridCtrl implementation functions
////////////////////////////////////////////////////////////////////////////
void CTimeGridCtrl::OnDraw(CDC* pDC)
{
CRect rect;
int row,col;
//固定行
rect.top = 0;
rect.bottom = rect.top + m_nFixedColHeight-1;
rect.left = 0;
rect.right = rect.left + m_nFixedCellWidth-1;
//DrawFixedCell(pDC, 0,0, rect);
rect.bottom = m_nFixedColHeight-1;
for (row = 1; row <m_nRows; row++)
{
rect.top = rect.bottom+1;
rect.bottom = rect.top + m_nDefCellHeight-1;
rect.right = -1;
rect.left = rect.right+1;
rect.right = rect.left + m_nFixedCellWidth-1;
DrawHourLabel(pDC, row,rect);
}
// draw fixed row cells 0..m_nFixedRows, m_nFixedCols..n
rect.bottom = -1;
//
rect.top = rect.bottom+1;
rect.bottom = rect.top + m_nFixedColHeight-1;
rect.left = 0;
rect.right=rect.left+m_nFixedCellWidth-1;
for (col =1; col <m_nCols; col++)
{
rect.left = rect.right+1;
rect.right = rect.left + m_nDefCellWidth-1;
DrawMinuateLabel(pDC,col, rect);
}
// draw rest of non-fixed cells
rect.bottom = m_nFixedColHeight-1;
rect.left=m_nFixedCellWidth-1;
for (row =1; row < m_nRows; row++)
{
rect.top = rect.bottom+1;
rect.bottom = rect.top + m_nDefCellHeight-1;
rect.left=m_nFixedCellWidth-1;
rect.right = rect.left;
for (col =1; col <m_nCols; col++)
{
rect.left = rect.right+1;
rect.right = rect.left + m_nDefCellWidth-1;
DrawCell(pDC, row, col, rect);
}
}
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CTimeGridCtrl::RedrawCell(int nRow, int nCol)
{
CRect rect;
if (!GetCellRect(nRow, nCol, rect))
return;
InvalidateRect(rect, TRUE); // Could not get a DC - invalidate it anyway
return;
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
CGridCell* CTimeGridCtrl::GetCellFromPt(CPoint point,int &nRow, int &nCol)
{
if (point.x <=m_nFixedCellWidth || point.y<=m_nFixedColHeight)
return NULL;
int ypos = m_nFixedColHeight;
nRow=-1,nCol=-1;
for (int row =1; row <=m_nRows+1; row++)
{
ypos += m_nDefCellHeight;
if (ypos > point.y)
{
nRow=row;
break;
}
}
int xpos = m_nFixedCellWidth;
for (int col =1; col <=m_nCols+1; col++)
{
xpos += m_nDefCellWidth;
if (xpos > point.x)
{
nCol=col;
break;
}
}
if(nRow!=-1&&nCol!=-1)
return GetCell(nRow,nCol);
return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -