写了一个万年历小程式。
使用VS 2010 MFC 基于对话框的程式。
万年历最主要的是要处理农历部份,农历的数据不能动态计算,必须编定好数据在程式里面。
首先写一个类处理日历中农历的数据部份,再就是输出采用使用画图的方式。
//QQ:2921237067
#pragma once
#include "CalendarCtrl.h"
#include "DrawBitmap.h"
#include "mybotton.h"
#include "afxwin.h"
// Cmy_calendarDlg 对话框
class Cmy_calendarDlg : public CDialogEx
{
// 构造
public:
Cmy_calendarDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_MY_CALENDAR_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
public:
HDC m_hMemDC;//缓存DC句柄
HBITMAP m_hBitmap;//位图句柄
int m_cxBitmap, m_cyBitmap;//位图大小,用于创建位图与缓存DC大小
bool CreateCompatibleInnerDC(HDC hdc);//创建一个缓存DC
CDrawBitmap m_objDrawBackground;//背景图片处理类对象
CCalendarCtrl m_objCalendar;//日历对像处理类对象
WORD m_iYear, m_iMonth, m_iDay;//年,月,日存储变量
//颜色
COLORREF m_dwSelColor;
COLORREF m_dwTitleColor;
COLORREF m_dwGrayTextColor;
COLORREF m_dwWhiteTextColor;
CFont m_oDispFont1;//字体1
CFont m_oDispFont2;//字体2
CRect m_rtYearLeft;//调整年的左三角形按钮显示区域
CRect m_rtYearMiddle;//年的显示区域
CRect m_rtYearRight;//调整年的右三角形按钮显示区域
CRect m_rtMonthLeft;//调整月的左三角形按钮显示区域
CRect m_rtMonthMiddle;//月的显示区域
CRect m_rtMonthRight;//调整月的右三角形按钮显示区域
CRect m_rtTitleText;//标题显示区域
CRect m_rtLunarText;//当前选中日期的农历日期显示区域
bool m_bGrayText;//标识是否灰显日期,灰显日期是上一个月或下一个月的日期
bool m_bYearLeftMove;//鼠标是否在年左三角形按钮显示区域
bool m_bYearRightMove;//鼠标是否在年右三角形按钮显示区域
bool m_bMonthLeftMove;//鼠标是否在月左三角形按钮显示区域
bool m_bMonthRightMove;//鼠标是否在月右三角形按钮显示区域
bool m_bYearLeftPress;//鼠标是否在年左三角形按钮显示区域按下
bool m_bYearRightPress;//鼠标是否在年右三角形按钮显示区域按下
bool m_bMonthLeftPress;//鼠标是否在月左三角形按钮显示区域按下
bool m_bMonthRightPress;//鼠标是否在月右三角形按钮显示区域按下
public:
void InitData();//初始化数据
void InitRect();//初始化各个矩形区大小
void CreateDispFont1();//字体1
void CreateDispFont2();//字体2
WORD HitTest(POINT * pt);//测试点击区域
CString GetLunarString(int nIndex);//获取初一,初二,初三... 信息
void PaintTitle(HDC hdc);//画月顶部的标题
void PaintDataTitle(HDC hdc);//画月星期几的信息
void PaintDate(HDC hdc);//画日历
void PaintDay(HDC hdc, WORD &iYear,WORD &iMonth,WORD &iDay, WORD &iLunarYear,WORD &iLunarMonth, WORD &iLuanrDay, LONG startx, LONG starty);
void PaintDataInfo(HDC hdc);//画年及月信息及调整三角形按钮
void PaintYearLeft(HDC hdc);//画年的左三角形
void PaintYearMiddle(HDC hdc);//画年份
void PaintYearRight(HDC hdc);//画年的右三角形
void PaintMonthLeft(HDC hdc);//画月的左三角形
void PaintMonthMiddle(HDC hdc);//画月份
void PaintMonthRight(HDC hdc);//画月的右三角形
void PaintLunarInfo(HDC hdc);//画月底部的农历信息
bool ButtonMoveCheck(CPoint pt);//检查是否有鼠标有在三角形按钮上
bool ButtonPressCheck(CPoint pt);//检查是否有鼠标按下三角形按钮
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnBnClickedButtonClose();
afx_msg void OnBnClickedButtonMin();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
CMyBotton m_Close;//最大化按钮
CMyBotton m_Min;//最小化按钮
};
//QQ:2921237067
#pragma once
const WORD START_YEAR =1901;
const WORD END_YEAR =2050;
class CCalendarCtrl
{
public:
CCalendarCtrl();
virtual ~CCalendarCtrl();
public:
//判断iYear是不是闰年
static BOOL IsLeapYear(WORD iYear);
//计算iYear,iMonth,iDay对应是星期几 1年1月1日 --- 65535年12月31日
static WORD WeekDay(WORD iYear, WORD iMonth, WORD iDay);
//返回iYear年iMonth月的天数 1年1月 --- 65535年12月
static WORD MonthDays(WORD iYear, WORD iMonth);
//返回阴历iLunarYer年阴历iLunarMonth月的天数,如果iLunarMonth为闰月,
//高字为第二个iLunarMonth月的天数,否则高字为0
// 1901年1月---2050年12月
static LONG LunarMonthDays(WORD iLunarYear, WORD iLunarMonth);
//返回阴历iLunarYear年的总天数
// 1901年1月---2050年12月
static WORD LunarYearDays(WORD iLunarYear);
//返回阴历iLunarYear年的闰月月份,如没有返回0
// 1901年1月---2050年12月
static WORD GetLeapMonth(WORD iLunarYear);
//把iYear年格式化成天干记年法表示的字符串
static void FormatLunarYear(WORD iYear, char *pBuffer);
static void FormatLunarYearEx(WORD iYear, CString &strText);
//把iMonth格式化成中文字符串
static void FormatMonth(WORD iMonth, char *pBuffer, BOOL bLunar = TRUE);
static void FormatMonthEx(WORD iMonth, CString &strText, BOOL bLunar = TRUE);
//把iDay格式化成中文字符串
static void FormatLunarDay(WORD iDay, char *pBuffer);
static void FormatLunarDayEx(WORD iDay, CString &strText);
//计算公历两个日期间相差的天数 1年1月1日 --- 65535年12月31日
static LONG CalcDateDiff(WORD iEndYear, WORD iEndMonth, WORD iEndDay, WORD iStartYear = START_YEAR, WORD iStartMonth =1, WORD iStartDay =1);
//计算从1901年1月1日过iSpanDays天后的阴历日期
static void CalcLunarDate(WORD &iYear, WORD &iMonth, WORD &iDay, LONG iSpanDays);
//计算公历iYear年iMonth月iDay日对应的阴历日期,返回对应的阴历节气 0-24
//1901年1月1日---2050年12月31日
static WORD GetLunarDate(WORD iYear, WORD iMonth, WORD iDay, WORD &iLunarYear, WORD &iLunarMonth, WORD &iLunarDay);
//计算公历iYear年iMonth月iDay日对应的节气 0-24,0表不是节气
static WORD GetLunarHolDay(WORD iYear, WORD iMonth, WORD iDay);
//计算公历iYear年iMonth月iDay日对应的节气 0-24,表不是节气
static BOOL GetLunarHolDayName(WORD iValue, char *pBuffer);
static BOOL GetLunarHolDayNameEx(WORD iValue, CString &strText);
};
下载:http://download.csdn.net/detail/laiyangsheng/9445430