C++实现带中国农历的万年历

忙里偷闲,这几天写了个万年历,是win32控制台的,效果还不错,先放上效果图:

C++实现带中国农历的万年历_第1张图片

 

C++实现带中国农历的万年历_第2张图片

C++实现带中国农历的万年历_第3张图片

做得丑了些,多年以前,看过多数万年历软件的代码,感觉需要将一些有共性的东西组织起来,免得重复造轮子,于是,本人打算将这部分内容封装成库,libLunar就这样诞生了。它是一个Windows下,C++接口的动态链接库,提供了丰富的方法来实现日期转换。经过多年的努力,目前已经比较完美。

头文件中定义的结构体如下:

typedef enum eConstellationName
{
	Co_InitValue = 0,//初始化值(无实际意义),请注意和数组下标匹配
	Co_Capricorn,//摩羯座
	Co_Aquarius,//水瓶座
	Co_Pisces,//双鱼座
	Co_Aries,//白羊座
	Co_Taurus,//金牛座
	Co_Gemini,//双子座
	Co_Cancer,//巨蟹座
	Co_Leo,//狮子座
	Co_Virgo,//处女座
	Co_Libra,//天秤座
	Co_Scorpio,//天蝎座
	Co_Sagittarius,//射手座
	Co_End	//结束(无效类型,特殊用处)
}ConstellationName;//星座

typedef enum eLunarPhaseName
{
	Lp_InitValue = 0,//初始化值(无实际意义),请注意和数组下标匹配
	Lp_NewMoon,//新月
	Lp_FirstQuarter,//上弦
	Lp_FullMoon,//满月
	Lp_LastQuarter,//下弦
	Lp_End	//结束(无效类型,特殊用处)
}LunarPhaseName;//月相

typedef enum eSolarTermName
{
	St_InitValue = 0,//初始化值(无实际意义),请注意和数组下标匹配
	St_LiChun,//立春
	St_YuShui,//雨水
	St_JingZhe,//惊蛰
	St_ChunFen,//春分
	St_QingMing,//清明
	St_GuYu,//谷雨
	St_LiXia,//立夏
	St_XiaoMan,//小满
	St_MangZhong,//芒种
	St_XiaZhi,//夏至
	St_XiaoShu,//小暑
	St_DaShu,//大暑
	St_LiQiu,//立秋
	St_ChuShu,//处暑
	St_BaiLu,//白露
	St_QiuFen,//秋分
	St_HanLu,//寒露
	St_ShuangJiang,//霜降
	St_LiDong,//立冬
	St_XiaoXue,//小雪
	St_DaXue,//大雪
	St_DongZhi,//冬至
	St_XiaoHan,//小寒
	St_DaHan,//大寒
	St_End	//结束(无效类型,特殊用处)
}SolarTermName;//节气

typedef enum ePhenologyName
{
	Ph_InitValue = 0,
	Ph_DongFengJieDong,//东风解冻
	Ph_ZheChongShiZhen,//蛰虫始振
	Ph_YuZhiFuBing,//鱼陟负冰
	Ph_TaJiYu,//獭祭鱼
	Ph_HouYanBei,//候雁北
	Ph_CaoMuMengDang,//草木萌动
	Ph_TaoShiHua,//桃始华
	Ph_CangGengMing,//仓庚鸣
	Ph_YingHuaWeiJiu,//鹰化为鸠
	Ph_XuanNiaoZhi,//玄鸟至
	Ph_LeiNaiFaSheng,//雷乃发声
	Ph_ShiDian,//始电
	Ph_TongShiHua,//桐始华
	Ph_TianShuHuaRu,//田鼠化鴽
	Ph_HongShiJian,//虹始见
	Ph_PingShiSheng,//萍始生
	Ph_MingJiuFuYu,//鸣鸠拂羽
	Ph_DaiShengJiangSang,//戴胜降桑
	Ph_LouGuoMing,//蝼蝈鸣
	Ph_QiuYinChu,//蚯蚓出
	Ph_WangGuaSheng,//王瓜生
	Ph_KuCaiXiu,//苦菜秀
	Ph_MiCaoSi,//靡草死
	Ph_MaiQiuZhi,//麦秋至
	Ph_TangLangSheng,//螳螂生
	Ph_JuanShiMing,//鹃始鸣
	Ph_FanSheWuSheng,//反舌无声
	Ph_LuJiaoJie,//鹿角解
	Ph_TiaoShiMing,//蜩始鸣
	Ph_BanXiaSheng,//半夏生
	Ph_WenFengZhi,//温风至
	Ph_XiFengJuBi,//蟋蜂居壁
	Ph_YingShiZhi,//鹰始挚
	Ph_FuCaoWeiYing,//腐草为萤
	Ph_TuRunRuShu,//土润溽暑
	Ph_DaYuShiXing,//大雨时行
	Ph_LiangFengZhi,//凉风至
	Ph_BaiLuJiang,//白露降
	Ph_HanChanMing,//寒蝉鸣
	Ph_YingNaiJiNiao,//鹰乃祭鸟
	Ph_TianDiShiSu,//天地始肃
	Ph_HeNaiDeng,//禾乃登
	Ph_HongYanLai,//鸿雁来
	Ph_XuanNiaoGui,//玄鸟归
	Ph_QunNiaoYangXiu,//群鸟养羞
	Ph_LeiShiShouSheng,//雷始收声
	Ph_ZheChongPiHu,//蛰虫坯户
	Ph_ShuiShiHe,//水始涸
	Ph_HongYanLaiBin,//鸿雁来宾
	Ph_QueRuDaShuiWeiGe,//雀入大水为蛤
	Ph_JuYouHuangHua,//菊有黄华
	Ph_ChaiNaiJiShou,//豺乃祭兽
	Ph_CaoMuHuangLuo,//草木黄落
	Ph_ZheChongXianFu,//蛰虫咸俯
	Ph_ShuiShiBing,//水始冰
	Ph_DiShiDong,//地始冻
	Ph_ZhiRuDaShuiWeiShen,//雉入大水为蜃
	Ph_HongCangBuJian,//虹藏不见
	Ph_TianQiShangSheng,//天气上升
	Ph_BiSeChengDong,//闭塞成冬
	Ph_HeDanBuMing,//鹖鴠不鸣
	Ph_HuShiJIao,//虎始交
	Ph_LiTingChu,//荔挺出
	Ph_QiuYinJie,//蚯蚓结
	Ph_JunJiaoJie,//麋角解
	Ph_ShuiQuanDong,//水泉动
	Ph_YanBeiXiang,//雁北乡
	Ph_QueShiChao,//鹊始巢
	Ph_ZhiGou,//雉雊
	Ph_JiRu,//鸡乳
	Ph_ZhengNiaoLiJi,//征鸟厉疾
	Ph_ShuiZeFuJian,//水泽腹坚
	Ph_End	//结束
}PhenologyName;//物候

typedef enum eMonthName
{
	Mn_InitValue = 0,//初始值
	Mn_January,//1月
	Mn_February,//2月
	Mn_March,//3月
	Mn_April,//4月
	Mn_May,//5月
	Mn_June,//6月
	Mn_July,//7月
	Mn_August,//8月
	Mn_September,//9月
	Mn_October,//10月
	Mn_November,//11月
	Mn_December,//12月
	Mn_End	//结束
}MonthName;

typedef enum eDayOfWeek
{
	Dw_Sunday = 0,//星期日
	Dw_Monday,//星期一
	Dw_Tuesday,//星期二
	Dw_Wednesday,//星期三
	Dw_Thursday,//星期四
	Dw_FriDay,//星期五
	Dw_Saturday//星期六
}DayOfWeek;

typedef enum eMonthType
{
	Mt_Normal = 0,//正常
	Mt_Later,//后
	Mt_Alias,//异名
	Mt_Extra,//附加
	Mt_Second,//第二个
	Mt_Last//上一个
}MonthType;//月份类型

typedef struct tagYEAR
{
	union
	{
		UINT uData;
		struct
		{
			UINT wYear:31;//公历年份编号
			UINT bBeforeChrist:1;//是否为公元前
		};
	};
}YEAR, *PYEAR, LPYEAR;//年份

typedef struct tagMONTH
{
	union
	{
		UINT uData;
		struct
		{
			WORD wMonth;//月名
			BYTE byMonthType;//月类型(MonthType)
			bool bLeapMonth;//是否为闰月(农历月)
		};
	};
}MONTH, *PMONTH, LPMONTH;//月份

typedef struct tagDATE
{
	YEAR uYear;//公历年份
	MONTH uMonth;//公历月
	WORD wDay;//公历日
	WORD wDayOfWeek;//星期
	tagDATE() : wDay(0), wDayOfWeek(Dw_Sunday){}

	virtual tagDATE & operator =(const tagDATE & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		return *this;
	}
}DATE, *PDATE, LPDATE;//公历日期(注意:不作为对外使用类型)

typedef struct tagTBElement
{
	WORD wTrunk;//天干
	WORD wBranch;//地支
	tagTBElement() : wTrunk(0), wBranch(0){}

	tagTBElement & operator =(const tagTBElement & rhs)
	{
		this->wTrunk = rhs.wTrunk;
		this->wBranch = rhs.wBranch;
		return *this;
	}
}TBElement;//干支元素

typedef struct tagDATEINFO : DATE
{
	TBElement tbYear;//农历纪年的天干地支,地支表示生肖(与正月初一同步,这是标准农历)
	WORD wDaysOfMonth;//月的总天数
	SHORT sDayOrdinal;//年内序数(以uYear元旦为参考)
	bool bLunarDate;//是否为农历日期
	tagDATEINFO() : wDaysOfMonth(0), sDayOrdinal(0), bLunarDate(false){}

	tagDATEINFO & operator =(const tagDATEINFO & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		this->tbYear = rhs.tbYear;
		this->wDaysOfMonth = rhs.wDaysOfMonth;
		this->sDayOrdinal = rhs.sDayOrdinal;
		this->bLunarDate = rhs.bLunarDate;
		return *this;
	}
}DATEINFO, *PDATEINFO, LPDATEINFO;//日期信息

typedef struct tagTIME
{
	WORD wHour;//时
	WORD wMinute;//分
	WORD wSecond;//秒
	WORD wMillisecond;//毫秒
	tagTIME() : wHour(0) ,wMinute(0), wSecond(0), wMillisecond(0){}

	tagTIME & operator =(const tagTIME & rhs)
	{
		this->wHour = rhs.wHour;
		this->wMinute = rhs.wMinute;
		this->wSecond = rhs.wSecond;
		this->wMillisecond = rhs.wMillisecond;
		return *this;
	}
}TIME, *PTIME, LPTIME;//时间

typedef struct tagDATETIME : DATE, TIME
{
	tagDATETIME & operator =(const tagDATETIME & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		this->wHour = rhs.wHour;
		this->wMinute = rhs.wMinute;
		this->wSecond = rhs.wSecond;
		this->wMillisecond = rhs.wMillisecond;
		return *this;
	}
}DATETIME, *PDATETIME, LPDATETIME;//公历日期时间

typedef struct tagLUNARMONTH
{
	YEAR uYear;//公历年份
	MONTH uMonth;//公历月
	WORD wDaysOfMonth;//月的总天数
	SHORT sNewMoonOrdinal;//新月的年内序数
	tagLUNARMONTH() : wDaysOfMonth(0), sNewMoonOrdinal(0){}

	tagLUNARMONTH & operator =(const tagLUNARMONTH & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDaysOfMonth = rhs.wDaysOfMonth;
		this->sNewMoonOrdinal = rhs.sNewMoonOrdinal;
		return *this;
	}
}LUNARMONTH, *PLUNARMONTH, LPLUNARMONTH;

typedef struct tagSOLARTERM : DATE
{
	SolarTermName STName;//节气编号
	SHORT sSTOrdinal;//节气的年内序数
	tagSOLARTERM() : STName(St_InitValue), sSTOrdinal(0){}

	tagSOLARTERM & operator =(const tagSOLARTERM & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		this->sSTOrdinal = rhs.sSTOrdinal;
		this->STName = rhs.STName;
		return *this;
	}
}SOLARTERM, *PSOLARTERM, LPSOLARTERM;//节气

typedef struct tagEXTREMESEASON
{
	YEAR uYear;//公历年份
	SHORT sYijiuOrdinal;//数九中的“一九”对应在uYear中的年内序数(二九至九九在此基础上推算可得到,故省略)
	SHORT sChufuOrdinal;//初伏的年内序数
	SHORT sMofuOrdinal;//末伏的年内序数(中伏在初伏后10天,故省略)
	tagEXTREMESEASON() : sYijiuOrdinal(0), sChufuOrdinal(0), sMofuOrdinal(0){}

	tagEXTREMESEASON & operator =(const tagEXTREMESEASON & rhs)
	{
		this->uYear = rhs.uYear;
		this->sYijiuOrdinal = rhs.sYijiuOrdinal;
		this->sChufuOrdinal = rhs.sChufuOrdinal;
		this->sMofuOrdinal = rhs.sMofuOrdinal;
		return *this;
	}
}EXTREMESEASON, *PEXTREMESEASON, LPEXTREMESEASON;//数九、伏日

typedef struct tagMEIYU
{
	YEAR uYear;//公历年份
	SHORT sRumeiOrdinal;//入梅的年内序数
	SHORT sChumeiOrdinal;//出梅的年内序数
	tagMEIYU() : sRumeiOrdinal(0), sChumeiOrdinal(0){}

	tagMEIYU & operator =(const tagMEIYU & rhs)
	{
		this->uYear = rhs.uYear;
		this->sRumeiOrdinal = rhs.sRumeiOrdinal;
		this->sChumeiOrdinal = rhs.sChumeiOrdinal;
		return *this;
	}
}MEIYU, *PMEIYU, LPMEIYU;//梅雨

typedef struct tagSHERI
{
	YEAR uYear;//公历年份
	SHORT sChunSheOrdinal;//春社日的年内序数
	SHORT sQiuSheOrdinal;//秋社日的年内序数
	tagSHERI() : sChunSheOrdinal(0), sQiuSheOrdinal(0){}

	tagSHERI & operator =(const tagSHERI & rhs)
	{
		this->uYear = rhs.uYear;
		this->sChunSheOrdinal = rhs.sChunSheOrdinal;
		this->sQiuSheOrdinal = rhs.sQiuSheOrdinal;
		return *this;
	}
}SHERI, *PSHERI, LPSHERI;//社日

typedef struct tagPHENOLOGY : DATETIME
{
	PhenologyName PHName;//物候名称
	SHORT sPheOrdial;//物候的年内序数
	tagPHENOLOGY() : PHName(Ph_InitValue), sPheOrdial(0){}

	tagPHENOLOGY & operator =(const tagPHENOLOGY & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		this->wHour = rhs.wHour;
		this->wMinute = rhs.wMinute;
		this->wSecond = rhs.wSecond;
		this->wMillisecond = rhs.wMillisecond;
		this->sPheOrdial = rhs.sPheOrdial;
		this->PHName = rhs.PHName;
		return *this;
	}
}PHENOLOGY, *PPHENOLOGY, LPPHENOLOGY; //物候

typedef struct tagLUNARPHASE : DATETIME
{
	LunarPhaseName LPName;//月相名称
	SHORT sLPOrdinal;//年内序数
	tagLUNARPHASE() : LPName(Lp_InitValue), sLPOrdinal(0){}

	tagLUNARPHASE & operator =(const tagLUNARPHASE & rhs)
	{
		this->uYear = rhs.uYear;
		this->uMonth = rhs.uMonth;
		this->wDay = rhs.wDay;
		this->wDayOfWeek = rhs.wDayOfWeek;
		this->wHour = rhs.wHour;
		this->wMinute = rhs.wMinute;
		this->wSecond = rhs.wSecond;
		this->wMillisecond = rhs.wMillisecond;
		this->sLPOrdinal = rhs.sLPOrdinal;
		this->LPName = rhs.LPName;
		return *this;
	}
}LUNARPHASE, *PLUNARPHASE, LPLUNARPHASE;//月相

typedef struct tagSIZHU
{
	TBElement tbYear;//年柱
	TBElement tbMonth;//月柱
	TBElement tbDay;//日柱
	TBElement tbHour;//时柱

	tagSIZHU & operator =(const tagSIZHU & rhs)
	{
		this->tbYear = rhs.tbYear;
		this->tbMonth = rhs.tbMonth;
		this->tbDay = rhs.tbDay;
		this->tbHour = rhs.tbHour;
		return *this;
	}
}SIZHU, *PSIZHU, LPSIZHU;//四柱

类的声明:

class CLunar
{
public:
	virtual bool GetDayOrdinal(const DATEINFO & diDate, const YEAR & uYear, SHORT & sDays) = 0;
	virtual bool GetDateFromOrdinal(const YEAR & uYear, SHORT sDays, DATEINFO & diDate) = 0;
	virtual bool GetConstellation(const DATEINFO & diDate, ConstellationName & CoIndex) = 0;
	virtual bool GetSizhu(const DATETIME & diDate, SIZHU & Sizhu) = 0;
	virtual bool DateConvert(const DATEINFO & diSrc, DATEINFO & diDest) = 0;
	virtual bool IsLeapYear(const YEAR & uYear, bool & bLeapYear) = 0;
	virtual bool GetExtremeSeason(const YEAR & uYear, EXTREMESEASON & esInfo) = 0;
	virtual bool GetMeiyu(const YEAR & uYear, MEIYU & myInfo) = 0;
	virtual bool GetSheri(const YEAR & uYear, SHERI & srInfo) = 0;
	virtual bool GetEasterSunday(const YEAR & uYear, DATEINFO & diEastSunday) = 0;
	virtual bool GetDays(const DATEINFO & diDate1, const DATEINFO & diDate2, long & lDays) = 0;
	virtual bool GetDays(const DATETIME & dtTime1, const DATETIME & dtTime2, double & dDays) = 0;
	virtual bool GetDate(const DATEINFO & diSrc, long lDays, DATEINFO & diDst) = 0;
	virtual bool GetDate(const DATETIME & dtSrc, double dDays, DATETIME & dtDst) = 0;
	virtual bool InitDateInfo(DATEINFO & diDst, bool bToday = true, const YEAR & uYear = 1, const MONTH & wMonth = Mn_January, WORD wDay = 1) = 0;
	virtual bool InitDateInfo(const DATETIME & dtSrc, DATEINFO & diDst) = 0;
	virtual bool InitDateTime(DATETIME & dtDst, bool bCurrentTime = true, const YEAR & uYear = 1, const MONTH & wMonth = Mn_January, WORD wDay = 1, WORD wHour = 0, WORD wMinute = 0, WORD wSecond = 0, WORD wMilliSecond = 0) = 0;
	virtual bool InitDateTime(const DATEINFO & diSrc, const TIME & tSrc, DATETIME & dtDst) = 0;
	virtual bool InitDateTime(const DATEINFO & diSrc, DATETIME & dtDst) = 0;
	virtual bool GetDayOfWeek(const YEAR & uYear, const MONTH & uMonth, WORD wDay, DayOfWeek & dwDst) = 0;
	virtual bool GetDayOfWeek(DATETIME & dtInfo) = 0;
	virtual bool GetDayOfWeek(DATEINFO & diDate) = 0;
	virtual bool GetFirstMonthInfo(const YEAR & uYear, LUNARMONTH & lmInfo) = 0;
	virtual bool GetNextMonthInfo(LUNARMONTH & lmInfo) = 0;
	virtual bool GetPreviousMonthInfo(LUNARMONTH & lmInfo) = 0;
	virtual bool GetMonthInfoByIndex(const YEAR & uYear, WORD wIndex, LUNARMONTH & lmInfo) = 0;
	virtual bool GetFirstSolarTerm(const YEAR & uYear, SOLARTERM & stInfo) = 0;
	virtual bool GetNextSolarTerm(SOLARTERM & stInfo) = 0;
	virtual bool GetPreviousSolarTerm(SOLARTERM & stInfo) = 0;
	virtual bool GetSolarTermByName(const YEAR & uYear, SolarTermName stName, SOLARTERM & stInfo) = 0;
	virtual bool GetFirstPhase(const YEAR & uYear, const MONTH & uMonth, LUNARPHASE & lpInfo) = 0;
	virtual bool GetNextPhase(LUNARPHASE & lpInfo) = 0;
	virtual bool GetPreviousPhase(LUNARPHASE & lpInfo) = 0;
	virtual bool GetFirstPhenology(const YEAR & uYear, PHENOLOGY & phInfo) = 0;
	virtual bool GetNextPhenology(PHENOLOGY & phInfo) = 0;
	virtual bool GetPreviousPhenology(PHENOLOGY & phInfo) = 0;
	virtual bool GetPhenologyByName(const YEAR & uYear, PhenologyName phIndex, PHENOLOGY & phInfo) = 0;
	virtual bool GetEmperorReign(const DATEINFO & diDate, char * pBuffer, DWORD dwBufSize) = 0;
	virtual MONTH GetLeapMonth(const YEAR & uYear) = 0;
	virtual WORD GetDaysOfMonth(const YEAR & uYear, const MONTH & uMonth) = 0;
	virtual WORD GetDaysOfYear(const YEAR & uYear) = 0;
	virtual WORD GetDaysOfLunarMonth(const YEAR & uYear, const MONTH & uLunarMonth) = 0;
	virtual WORD GetDaysOfLunarYear(const YEAR & uYear) = 0;
protected:
	CLunar(void);
	virtual ~CLunar(void);
};

 

DLL的接口定义如下:

提供的方法有以下36个:

1. GetDayOrdinal,计算某日期在该年的序数;

2. GetDateFromOrdinal,从年内序数求出日期;

3. GetConstellation,计算星座;

4. GetSiZhu,获取某一时刻的四柱(八字);

5. DateConvert,农历与公历互转;

6. IsLeapYear,判断是否为闰年(公历);

7. GetExtremeSeason,获取数九,三伏;

8. GetMeiyu,获取入梅、出梅;

9. GetSheri,  获取春秋社日的年内序数;

10. GetEasterSunday,  获取复活节日期;

11. GetDays,  获取两个日期之间的天数差;

12. GetDate,获取距离某个日期指定天数的日期或时间;

13. InitDateInfo,初始化日期结构体;

14. InitDateTime,初始化日期时间结构体;

15. GetDayOfWeek,获取某一天的星期;

16. GetFirstMonthInfo,获取农历某年年首月份的信息;

17. GetNextMonthInfo,获取下个农历月的信息;

18. GetPreviousMonthInfo,获取上个农历月的信息;

19. GetMonthInfoByIndex,获取农历年当中指定索引的月份信息;

20. GetFirstSolarTerm,获取一年当中首个节气信息;

21. GetNextSolarTerm,获取下一个节气信息;

22. GetPreviousSolarTerm,获取上一个节气信息;

23. GetSolarTermByIndex,获取一年当中指定节气的信息;

24. GetFirstPhase,查询公历月中的第一个月相信息;

25. GetNextPhase,查询下一个月相信息;

26. GetPreviousPhase,查询上一个月相信息;

27. GetFirstPhenology,获取公历某一年当中的首个物候信息;

28. GetNextPhenology,获取下一个物候信息;

29. GetPreviousPhenology,获取上一个物候信息;

30. GetPhenologyByName,获取公历年当中指定的物候信息;

31.GetEmperorReign,获取年号;

32.GetDaysOfMonth,获取公历月份的总天数;

33.GetDaysOfYear,获取公历年份的总天数;

34.GetLeapMonth,获得农历闰月;

35.GetDaysOfLunarMonth,获取农历月份的总天数;

36.GetDaysOfLunarYear,获取农历年份的总天数。

 

使用方法(仅限于在C++中使用):

1. 动态链接调用

#include "libLunar.h"

//以下为代码片段
CL_CLunarInterface * pLunarIf = NULL;
HINSTANCE hLunarDll = LoadLibrary(_T("liblunar.dll"));
pLunarIfMgr lunar_interface = (pLunarIfMgr)GetProcAddress(hLunarDll, "GetLunarInterface");
pLunarIf = lunar_interface();
if (pLunarIf == NULL)
{
	return;
}
CL_CLunar * pLunar=NULL;
pLunarIf->GetClassPtr(Ct_Lunar,(void **)&pLunar);
if (!pLunar)
{
	return;
}
pLunar->GetDayOrdinal(, , );
pLunar->DateConvert(, );
//TODO:其它代码
if (pLunarIf)
{
	if (pLunar)
	{
		pLunarIf->ReleaseClassPtr((void **)&pLunar);//此代码建议加上
	}
	pLunarIf->ReleaseInterface();
	pLunarIf = NULL;
}
if (hLunarDll != NULL)
{
	FreeLibrary(hLunarDll);//释放DLL
	hLunarDll = NULL;
}

2. 静态链接调用

#define USING_LUNAR_STATIC
#include "libLunar.h"

#pragma comment(lib, "liblunar.lib")

//以下为代码片段
CL_CLunarInterface * pLunarIf = GetLunarInterface();
if (pLunarIf == NULL)
{
	return;
}
pLunar->GetDayOrdinal(, , );
pLunar->DateConvert(, );
//TODO:其它代码

要实现万年历,函数调用流程如下:

1.先根据要显示的年和月,算出本月1日的星期。

2.调用GetDaysOfMonth函数来得到当前月的总天数,到这一步,一个显示公历的简单日历可以说是完成了。

3.调用GDateToLDate函数,得到公历月首的农历信息,通过一一对应关系,逐一按天来排农历,当农历到月末时,重新计算下一个月的农历信息,直到将公历月份填充完毕。

4.需要添加节气、数九、三伏、梅雨等信息的,可以通过与SOLARTERM结构体有关的函数得到二十四节气信息,通过GetExtremeSeason得到数九、三伏,以及入梅、出梅信息。

通过以上一系列函数调用,一个像模像样的万年历就制作成功了,界面可以根据自己的设计,做漂亮些(可以使用界面库),以下提供相应的资料包。

经过了多年的修正与优化,libLunar库已经比较完善,但后续还是会对其进行更新。

百度盘下载(2019/02/22) 密码:6und

你可能感兴趣的:(农历相关)