⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendar_edit.cpp

📁 一个完整的桌面日历程序
💻 CPP
字号:
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// -																																					-
// - File:			calendar_edit.cpp.																													-
// -																																					-
// - Contents: 		Implementation of class CCalEdit.																									-
// -																																					-
// - Purpose:  		A rich edit based class specially made for calendar usage.																			-
// -																																					-
// - Remarks:    	-																																	-
// -																																					-
// - Originator: 	Michael Mogensen, MM-IT Consult 2003.																								-
// -																																					-
// - Compiler:		MS Visual C++ ver6.0.																											    -
// -																																					-
// - Period:		29.04.03 - 00.00.00.																											    -
// -																																					-
// - Version:		1.00. 																																-
// -																																					-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous.																																		-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Header(s).																																			-
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma once
#include "stdafx.h"
#include "calendar_edit.h" // Self.

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous.																																		-
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#define new DEBUG_NEW

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - CCalEdit.																																			-
// ------------------------------------------------------------------------------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CCalEdit, CRichEditCtrl)
	// System.
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_MOUSEWHEEL()
	ON_WM_NCPAINT()
	// 
END_MESSAGE_MAP()

int CCalEdit::OnCreate(LPCREATESTRUCT lpcs)
// The framework calls this member function when an application requests that the Windows window be created by calling the Create or CreateEx member function.
{
	if(CRichEditCtrl::OnCreate(lpcs) == -1)
		return -1;
	Init();
	return 0;
}

void CCalEdit::OnDestroy()
// The framework calls this member function to inform the CWnd object that it is being destroyed.
{
	UnInit();
	CRichEditCtrl::OnDestroy();
}

BOOL CCalEdit::OnMouseWheel(UINT uFlag, short sDelta, CPoint ipMouse)
// The framework calls this member function as a user rotates the mouse wheel and encounters the wheel's next notch.
{
	// For Windows NT 4.0 and up.
	BOOL bResult = CRichEditCtrl::OnMouseWheel(uFlag, sDelta, ipMouse);
	LineScroll(sDelta < 0 ? 1 : -1);
	return bResult;
}

void CCalEdit::OnNcPaint()
// The framework calls this member function when the nonclient area needs to be painted.
{
	// Paint NC-part of window.
	CRichEditCtrl::OnNcPaint();
	CRgn RgnWindow,
		 RgnClient;
	const CRectEx irWindow(GetWindowRectEx(this, true));
	if(!RgnWindow.CreateRectRgn(irWindow.left,
								irWindow.top,
								irWindow.right,
								irWindow.bottom))
		return;
	if(!RgnClient.CreateRectRgn(irWindow.left + 3,
								irWindow.top + 3,
								irWindow.right - 3,
								irWindow.bottom - 3))
		return;
	const int iResult = RgnWindow.CombineRgn(&RgnWindow, &RgnClient, RGN_XOR);
	if(iResult != ERROR &&
	   iResult != NULLREGION)
	{
		CDC *pdc = GetWindowDC();
		CBrush BkGnd;
		//BkGnd.CreateSolidBrush(pdc->GetBkColor());
		BkGnd.CreateSysColorBrush(COLOR_WINDOW);
		//BkGnd.CreateSolidBrush(GetBackgroundColor());
		pdc->FillRgn(&RgnWindow, &BkGnd);
		ReleaseDC(pdc);
	}
}

BOOL CCalEdit::PreTranslateMessage(MSG *pMSG)
// Used by class CWinApp to translate window messages before they are dispatched to theTranslateMessage andDispatchMessage Windows functions.
{
	BOOL bResult = CRichEditCtrl::PreTranslateMessage(pMSG);
	return bResult;
}

// 4. Slaves. (alphabetical).
/*
COLORREF CCalEdit::GetBackgroundColor()
// Return color ref. of background of window.
{
	try
	{
		CBrush *pBrush = CBrush::FromHandle((HBRUSH)::GetClassLong(GetSafeHwnd(), GCL_HBRBACKGROUND));
		LOGBRUSH lBrush;
		pBrush->GetLogBrush(&lBrush);
		return lBrush.lbColor;
	}
	catch(CResourceException *pre)
	{
		pre->ReportError();
		pre->Delete();
		return 0L;
	}
	catch(...)
	{
		return 0L;
	}
}
*/

const void CCalEdit::Clear()
// Clear contents.
{
	SetSel(0, -1);
	CRichEditCtrl::Clear();
	SetDefaultCharFormat();
}

const CString CCalEdit::GetText()
// Return contents.
{
	HideSelection(TRUE, FALSE);
	SetSel(0, -1);
	const CString cstrContents(GetSelText());
	SetSel(0, 0);
	HideSelection(FALSE, FALSE);
	return cstrContents;
}

const void CCalEdit::Init()
// Init.
{
	SetDefaultCharFormat();
	// Make window white as snow.
	SetBackgroundColor(TRUE, RGB_White);
	// Enable change and update events.
	DWORD dwEventMask = 0;
	dwEventMask |= ENM_CHANGE;
	//dwEventMask |= ENM_SETFOCUS;
	dwEventMask |= ENM_UPDATE;
	SendMessage(EM_SETEVENTMASK, 0, dwEventMask);
}

const bool CCalEdit::Load(const CString &cstrFile)
// Load and get contents. Return T on loaded and F if not.
{
	if(!file_or_directory_exist(cstrFile))
		return false;
	CFile File((LPCTSTR)cstrFile, CFile::modeRead);
	EDITSTREAM Stream;
	Stream.dwCookie = (DWORD)&File;
	Stream.pfnCallback = StreamCallback_In;
	StreamIn(SF_TEXT, Stream);
	return true;
}

const bool CCalEdit::Save(const CString &cstrFile)
// Save and set contents. Return T on saved and F if not.
{
	CFile File((LPCTSTR)cstrFile, CFile::modeCreate | CFile::modeWrite);
	EDITSTREAM Stream;
	Stream.dwCookie = (DWORD)&File;
	Stream.pfnCallback = StreamCallback_Out;
	StreamOut(SF_TEXT, Stream);
	return true;
}

/*
void CCalEdit::SetBackgroundColor(BOOL bIsSysColor, COLORREF crBkGnd)
// Set color ref. of background of window.
{
	CDC *pdc = GetWindowDC();
	pdc->SetBkColor(crBkGnd);
	ReleaseDC(pdc);
	CRichEditCtrl::SetBackgroundColor(bIsSysColor, crBkGnd);
}
*/

const void CCalEdit::SetText(const CString &cstrText)
// Set contents.
{
	Clear();
	SetWindowText(cstrText);
}

const void CCalEdit::SetDefaultCharFormat(const bool bJustRetainIt)
// Used to ensure that we always use the same char. format.
{
	if(bJustRetainIt)
	{
		long lChar_Start = 0, 
			 lChar_End = 0;
		CHARFORMAT cf_def;
		cf_def.cbSize = sizeof(CHARFORMAT);
		GetDefaultCharFormat(cf_def);
		GetSel(lChar_Start, lChar_End);
		HideSelection(TRUE, FALSE);
		SetSel(0, -1);
		SetSelectionCharFormat(cf_def);
		SetSel(lChar_End, lChar_End);
		HideSelection(FALSE, FALSE);
	}
	else
	{
		// Default font is native message bar font.
		NONCLIENTMETRICS ncm = { 0 };
		ncm.cbSize = sizeof(NONCLIENTMETRICS);
		if(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0))
		{
			CHARFORMAT cf;
			cf.cbSize = sizeof(CHARFORMAT);
			GetDefaultCharFormat(cf);
			cf.dwMask = (CFM_BOLD |
						 CFM_CHARSET |
						 CFM_COLOR |
						 CFM_FACE |
						 CFM_ITALIC |
						 CFM_OFFSET |
						 CFM_PROTECTED |
						 CFM_SIZE |
						 CFM_STRIKEOUT |
						 CFM_UNDERLINE);
			cf.dwEffects &= ~(CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_AUTOCOLOR);
			cf.yHeight = ncm.lfMessageFont.lfHeight;
			cf.yOffset = 0;
			cf.crTextColor = RGB_Black;
			cf.bCharSet = DEFAULT_CHARSET;
			cf.bPitchAndFamily = DEFAULT_PITCH;
			::lstrcpy(cf.szFaceName, ncm.lfMessageFont.lfFaceName);
			CRichEditCtrl::SetDefaultCharFormat(cf);
		}
	}
}

DWORD CALLBACK CCalEdit::StreamCallback_In(DWORD dwCookie, LPBYTE lpBuffer, LONG lBytes, LONG *plBytesRead)
// Do actual read op. FROM stream.
{
	CFile *pFile = (CFile*)dwCookie;
	*plBytesRead = pFile->Read(lpBuffer, lBytes);
	return 0;
}

DWORD CALLBACK CCalEdit::StreamCallback_Out(DWORD dwCookie, LPBYTE lpBuffer, LONG lBytes, LONG *plBytesWritten)
// Do actual write op. TO stream.
{
	CFile *pFile = (CFile*)dwCookie;
	pFile->Write(lpBuffer, lBytes);
	*plBytesWritten = lBytes;
	return 0;
}

const void CCalEdit::UnInit()
// UnInit.
{
}

// ------------------------------------------------------------------------------------------------------------------------------------------------------
// -																																				  	-
// ------------------------------------------------------------------------------------------------------------------------------------------------------

// 0. Data. (alphabetical).
// 1. Object. (alphabetical).
// 2. Event's. (alphabetical).
// 3.0. Menu choice. (menu order).
// 3.1. Menu choice, enablers. (menu order).
// 4. Slaves. (alphabetical).
// 5. Other. (alphabetical).

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -