// WMDateCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "WMDateCtrl.h"
#include "WVCellData.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WEEKVIEWER_CLASSNAME _T("DBSWMDateCtrl") // Window class name
/////////////////////////////////////////////////////////////////////////////
// CWMDateCtrl
CWMDateCtrl::CWMDateCtrl()
{
// Default values
clrBackGrnd = RGB(255,255,213);
clrOtherMonthBk = RGB(255, 244, 188);
clrGradBot = RGB(249, 204, 95); // Gradient color - bottom
clrGradTop = RGB(236, 233, 216);
clrGradLine = RGB(187, 85, 3); // Gradient line color
nFontSize = 85;
strFontName = "TAHOMA";
bMilTime = FALSE; // NonMilitary Time
bMonthView = FALSE; // Do not draw Month view
nUniqueItemID = 0; // Unique Item ID Number
dtMonth.SetDate(COleDateTime::GetCurrentTime().GetYear(),
COleDateTime::GetCurrentTime().GetMonth(), 1);
pImageList = NULL;
// Create ALL Cell Objects for Week or Month Views
CWVCellData *pData = NULL;
for (int x = 1; x <= WV_TOTALCELLS; x++)
{
pData = new CWVCellData();
pData->SetCellID(x);
pData->SetMilitaryTime(bMilTime);
pData->SetBkColor(clrBackGrnd);
// Add node to Cell List
m_CellList.AddTail(pData);
}
RegisterWindowClass();
}
CWMDateCtrl::~CWMDateCtrl()
{
POSITION pos = m_CellList.GetHeadPosition();
CObject *pObj = NULL;
while (pos != NULL)
{
pObj = m_CellList.GetNext(pos);
if (pObj != NULL)
delete pObj;
}
m_CellList.RemoveAll();
}
BEGIN_MESSAGE_MAP(CWMDateCtrl, CWnd)
//{{AFX_MSG_MAP(CWMDateCtrl)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_DESTROY()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CWMDateCtrl::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, WEEKVIEWER_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
//wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.hbrBackground = NULL;
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = WEEKVIEWER_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CWMDateCtrl message handlers
void CWMDateCtrl::OnDestroy()
{
CWnd::OnDestroy();
POSITION pos = m_CellList.GetHeadPosition();
CObject *pObj = NULL;
while (pos != NULL)
{
pObj = m_CellList.GetNext(pos);
if (pObj != NULL)
delete pObj;
}
m_CellList.RemoveAll();
}
void CWMDateCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CWnd::PreSubclassWindow();
}
BOOL CWMDateCtrl::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
// Get client area to paint
GetClientRect(&rect);
// Fill with background color
pDC->FillSolidRect(&rect, clrBackGrnd);
//return CWnd::OnEraseBkgnd(pDC);
return TRUE;
}
void CWMDateCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Clear all cell rects
CRect rArea;
CWVCellData *pData = NULL;
for (int x = 1; x <= WV_TOTALCELLS; x++)
{
// Get current CellData
pData = GetCellByID(x);
if (pData)
{
rArea.SetRectEmpty();
pData->SetCellRect(&rArea); // reset cell area
}
}
if (!bMonthView)
{
// Draw Week View
PaintWeekView(&dc);
// Paint Dates on Cell Headers
DrawWeekCellHeaders(&dc);
}
else
{
// Draw Month View
PaintMonthView(&dc);
DrawMonthCellHeaders(&dc);
}
// Do not call CWnd::OnPaint() for painting messages
}
// Draw Headers within each Date Cell
void CWMDateCtrl::DrawWeekCellHeaders(CPaintDC *pDC)
{
CRect rect, rHdr, rArea;
// Calculate height of Cell Header to be the width of the scroll bars
int nHdrHeight = GetSystemMetrics(SM_CXVSCROLL);
CBrush brHdrBrush, brOldBrush;
CFont fFont, *pOldFont;
CString strBuf;
CWVCellData *pCellData = NULL;
COLORREF clrHL = GetSysColor(COLOR_HIGHLIGHT); // Highlight color
COLORREF clrFace = GetSysColor(COLOR_BTNFACE); // Header color
POSITION posItem = NULL;
CWVCellDataItem *pCellItem = NULL;
int nCellHeight = 0;
COleDateTime dtDate = dtCurrentDate;
CString strDay = dtDate.Format("%A, %m/%d/%Y");
// Find Monday
while (dtDate.GetDayOfWeek() != 2)
{
dtDate -= 1;
strDay = dtDate.Format("%A, %m/%d/%Y");
}
// Initialize values
fFont.CreatePointFont(nFontSize, strFontName, pDC);
pOldFont = pDC->SelectObject(&fFont);
// Remove time from date
dtCurrentDate.SetDate(dtCurrentDate.GetYear(), dtCurrentDate.GetMonth(),
dtCurrentDate.GetDay());
// Make drawing text transparent
pDC->SetBkMode(TRANSPARENT);
// Get client area to paint
GetClientRect(&rect);
nCellHeight = rect.bottom / 3;
// Build Week View Headers
for (int x = 1; x <= 7; x++)
{
// Draw Header
pCellData = GetCellByDate(dtDate);
dtDate += 1;
pCellData->GetCellRect(&rHdr);
pDC->FillSolidRect(&rHdr, pCellData->GetBkColor());
rHdr.left += 1;
rHdr.top += 1;
rHdr.bottom = rHdr.top + nHdrHeight;
if (pCellData->GetCellDate() == dtCurrentDate)
{
// Gradient Fill this header
rHdr.left -= 1;
rHdr.right -= 1;
FillGradientRect(pDC, &rHdr);
// Calculate Rectangle - for highlighted cell
CRect rText;
rText.CopyRect(&rHdr);
strBuf = pCellData->GetCellDate().Format("%A, %B %d");
pDC->DrawText(strBuf, &rText, DT_CALCRECT);
if (pCellData->GetHighlight())
{
// Highlight Header
rText.left = rHdr.left + (rHdr.right - rText.right) ;
rText.right = rHdr.right;
rText.bottom = rHdr.bottom;
pDC->FillSolidRect(&rText, clrHL);
pDC->SetTextColor(RGB(255,255,255));
}
}
else
{
// Header not in Gradient fill
if (pCellData->GetHighlight())
{
// Normal highlight of header
pDC->FillSolidRect(&rHdr, clrHL); // Highlight Header
pDC->SetTextColor(RGB(255,255,255));
}
else
{
// Display as a regular non-modified header
pDC->FillSolidRect(&rHdr, clrFace);
}
}
// Draw Date within Header
strBuf = pCellData->GetCellDate().Format("%A, %B %d");
pDC->DrawText(strBuf, &rHdr, DT_SINGLELINE | DT_BOTTOM | DT_RIGHT | DT_END_ELLIPSIS);
pDC->SetTextColor(RGB(0,0,0));
// DrawItems for this cell
pCellData->DrawItems(pDC, bMonthView);
}
pDC->SelectObject(pOldFont);
}
void CWMDateCtrl::SetBkColor(COLORREF clrBk)
{
clrBackGrnd = clrBk;
// Update all cells
POSITION pos = m_CellList.GetHeadPosition();
POSITION posItem = NULL;
CWVCellData *pData = NULL;
CWVCellDataItem *pItem = NULL;
while (pos != NULL)
{
pData = (CWVCellData *)m_CellList.GetNext(pos);
pData->SetBkColor(clrBk);
// Update background color for all the CellData Items
posItem = pData->m_ItemList.GetHeadPosition();
while (posItem != NULL)
{
pItem = (CWVCellDataItem *)pData->m_ItemList.GetNext(posItem);
pItem->SetBkColor(clrBk);
}
}
InvalidateRect(NULL, TRUE);
}
void CWMDateCtrl::FillGradientRect(CDC *pDC, CRect *pRect)
{
// Gradient Fill Header
TRIVERTEX vert[2] ;
GRADIENT_RECT gRect;
vert [0] .x = pRect->left;
vert [0] .y = pRect->top;
vert [0] .Alpha = 0x0000;
vert [1] .x = pRect->right;
vert [1] .y = pRect->bottom;
vert [1] .Alpha = 0x0000;
if
评论7
最新资源