?? calendars.c
字號(hào):
sprintf(text, "%2d", *iDay);
// if day == current day
/*
if(iDay++ == m_iDay)
{
SetRect(&m_sSelRect, startx-1, starty, startx +COL_WIDTH-3,starty + ROW_HEIGHT);
dc.FillRect(m_sSelRect, &CBrush(m_dwSelColor));
dc.SetTextColor(m_dwSelForeColor);
dc.SetBkColor(m_dwSelColor);
dc.TextOut(startx, starty +2, text);
dc.SetBkColor(m_dwBackColor);
dc.SetTextColor(m_dwForeColor);
dc.BitBlt(startx+17, starty +2, 8,16,&imgdc,0,0,MERGEPAINT);
}
else */
{
outtextxy(startx, starty+2, text);
if(*iLunarMonth==12 && *iLunarDay==1)
{
line(startx+25, starty+4, startx+37, starty+4);
line(startx+27, starty+9, startx+35, starty+9);
line(startx+25, starty+12, startx+37, starty+12);
line(startx+31, starty+1, startx+31, starty+8);
}
else
FNTDoChineseChar(OsdHandle, FONT_12X12, startx+25, starty+1, cText);
FNTDoChineseChar(OsdHandle, FONT_12X12, startx+25, starty+15, &cText[2]);
}
*iDay+=1;
if(*iLunarYear <START_YEAR && *iLunarDay == 29)
*iLunarDay = 1, *iLunarMonth+=1;
else if(*iLunarDay<29)
*iLunarDay+=1;
else
{
UINT32 days = CAL_LunarMonthDays(*iLunarYear, *iLunarMonth);
if(!HIWORD(days) && *iLunarDay == LOWORD(days)
|| bSecondLeapMonth && *iLunarDay == HIWORD(days))
*iLunarMonth =(UINT16) ((*iLunarMonth)%12+1);
if(*iLunarDay < LOBYTE(days))
*iLunarDay+=1;
else
*iLunarDay =1;
}
}
void CAL_PaintDate(T_OSGRect rect)
{
INT32 i, j, startx;
UINT8 text[4];
UINT16 day =1;
UINT16 iLunarYear, iLunarMonth, iLunarDay;
UINT16 iHolDay;
T_OSGRect rect1;
UINT8 bSecondLeapMonth = CAL_CalcLunarDate(&iLunarYear, &iLunarMonth, &iLunarDay,
CAL_CalcDateDiff(m_iYear, m_iMonth, 1));
//GetLunarDate(m_iYear, m_iMonth, 1, iLunarYear, iLunarMonth, iLunarDay);
rect.y +=TITLE_HEIGHT+6;
rect1.x = rect.x+2*COL_WIDTH+10;
rect1.y = rect.y+6*ROW_HEIGHT;
setbkcolor(NB_COLOR);
setcolor(NT_COLOR);
setfont(FONT_NORMAL);
outtextxy(rect1.x, rect1.y+2, "節(jié)氣");
rect1.x +=COL_WIDTH;
for(i=0; i<6; i++, rect.y += ROW_HEIGHT)
{
startx=rect.x+10;
for(j=0; j<7; j++, startx += COL_WIDTH)
{
if(j < CAL_WeekDay(m_iYear, m_iMonth, 1) && i==0)
continue;
if(day > CAL_MonthDays(m_iYear, m_iMonth))
break;
iHolDay = CAL_GetLunarHolDay(m_iYear, m_iMonth, day);
if(iHolDay)
{
setcolor(NT_COLOR);
sprintf(text, "%2d", day);
outtextxy(rect1.x,rect1.y+2, text);
CAL_PaintChineseInfo((UINT16)(rect1.x+30),(UINT16)(rect1.y+2), iHolDay);
rect1.x+=(2*COL_WIDTH);
}
if(j==0||j==6||CAL_GetHoilday(m_iMonth, day, iLunarMonth, iLunarDay))
setcolor(35);
else
setcolor(NT_COLOR);
CAL_PaintOneDay(&day, &iLunarYear, &iLunarMonth,
&iLunarDay, startx, rect.y, bSecondLeapMonth);
}
}
}
UINT16 CAL_GetLunarDate (UINT16 iYear, UINT16 iMonth, UINT16 iDay,
UINT16* iLunarYear, UINT16* iLunarMonth, UINT16* iLunarDay)
{
CAL_CalcLunarDate(iLunarYear, iLunarMonth, iLunarDay,
CAL_CalcDateDiff(iYear, iMonth, iDay));
return CAL_GetLunarHolDay(iYear, iMonth, iDay);
}
UINT32 CAL_CalcDateDiff(UINT16 iEndYear, UINT16 iEndMonth, UINT16 iEndDay)
{
UINT16 iStartYear = START_YEAR;
UINT16 iStartMonth =1;
UINT16 iStartDay =1;
UINT16 monthday[]={0, 31, 59 ,90, 120, 151, 181, 212, 243, 273, 304, 334};
//計(jì)算兩個(gè)年份1月1日之間相差的天數(shù)
UINT32 iDiffDays =(iEndYear - iStartYear)*365;
iDiffDays += (iEndYear-1)/4 - (iStartYear-1)/4;
iDiffDays -= ((iEndYear-1)/100 - (iStartYear-1)/100);
iDiffDays += (iEndYear-1)/400 - (iStartYear-1)/400;
//加上iEndYear年1月1日到iEndMonth月iEndDay日之間的天數(shù)
iDiffDays += monthday[iEndMonth-1] +
(CAL_IsLeapYear(iEndYear)&&iEndMonth>2? 1: 0);
iDiffDays += iEndDay;
//減去iStartYear年1月1日到iStartMonth月iStartDay日之間的天數(shù)
iDiffDays -= (monthday[iStartMonth-1] +
(CAL_IsLeapYear(iStartYear)&&iStartMonth>2 ? 1: 0));
iDiffDays -= iStartDay;
return iDiffDays;
}
UINT8 CAL_CalcLunarDate(UINT16* iYear, UINT16* iMonth ,UINT16* iDay, UINT32 iSpanDays)
{
//陽歷1901年2月19日為陰歷1901年正月初一
//陽歷1901年1月1日到2月19日共有49天
UINT32 tmp;
UINT8 rcode =0;
if(iSpanDays <49)
{
*iYear = (UINT16)(START_YEAR-1);
if(iSpanDays <19)
{
*iMonth = 11;
*iDay = (UINT16)(11+iSpanDays);
}
else
{
*iMonth = 12;
*iDay = (UINT16)(iSpanDays -18);
}
return rcode;
}
//下面從陰歷1901年正月初一算起
iSpanDays -=49;
*iYear = START_YEAR;
*iMonth = 1;
*iDay = 1;
//計(jì)算年
tmp = CAL_LunarYearDays(*iYear);
while(iSpanDays >= tmp)
{
iSpanDays -= tmp;
*iYear+=1;
tmp = CAL_LunarYearDays(*iYear);
}
//計(jì)算月
tmp = LOWORD(CAL_LunarMonthDays(*iYear, *iMonth));
while(iSpanDays >= tmp)
{
iSpanDays -= tmp;
if(*iMonth == CAL_GetLeapMonth(*iYear))
{
tmp = HIWORD(CAL_LunarMonthDays(*iYear, *iMonth));
if(iSpanDays < tmp)
{
rcode = 1;
break;
}
iSpanDays -= tmp;
}
*iMonth+=1;
tmp = LOWORD(CAL_LunarMonthDays(*iYear, *iMonth));
}
//計(jì)算日
*iDay += (UINT16)(iSpanDays);
return rcode;
}
/*
** 根據(jù)節(jié)氣數(shù)據(jù)存儲(chǔ)格式,
** 計(jì)算陽歷iYear年iMonth月iDay日對(duì)應(yīng)的節(jié)氣.
*/
UINT16 CAL_GetLunarHolDay(UINT16 iYear, UINT16 iMonth, UINT16 iDay)
{
UINT8 flag = gLunarHolDay[(iYear - START_YEAR)*12+iMonth -1];
UINT16 day;
if(iDay <15)
day= (UINT16)(15 - ((flag>>4)&0x0f));
else
day = (UINT16)((flag&0xF)+15);
if(iDay == day)
return (UINT16)((iMonth-1) *2 + (iDay>15? 1: 0) +1);
else
return 0;
}
/*
** 返回當(dāng)前的日期
*/
void CAL_GetDate(UINT16* iYear, UINT16* iMonth, UINT16* iDay)
{
*iYear = m_iYear;
*iMonth = m_iMonth;
*iDay = m_iDay;
}
BOOL CAL_SetDate(UINT16 iYear, UINT16 iMonth, UINT16 iDay)
{
if(iYear < START_YEAR || iYear > END_YEAR || iMonth <1 || iMonth >12)
return FALSE;
if(iDay <1 || iDay > CAL_MonthDays(iYear, iMonth))
return FALSE;
m_iYear = iYear;
m_iMonth = iMonth;
m_iDay = iDay;
return TRUE;
}
UINT16 CAL_WeekDay(UINT16 iYear, UINT16 iMonth, UINT16 iDay)
{
//數(shù)組元素monthday[i]表示第i個(gè)月以前的總天數(shù)除以7的余數(shù)
UINT16 monthday[]={0,3,3,6,1,4,6,2,5,0,3,5};
UINT16 iDays = (UINT16)((iYear-1)%7 + (iYear-1)/4 - (iYear-1)/100 +(iYear-1)/400);
iDays += (monthday[iMonth-1] +iDay) ;
//如果iYear是閏年
if(CAL_IsLeapYear(iYear) && iMonth>2)
iDays++;
//返回:0,1,2,3,4,5,6表日、一、二、三、四、五、六
return (UINT16)(iDays%7);
}
UINT16 CAL_MonthDays(UINT16 iYear, UINT16 iMonth)
{
switch(iMonth)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
return 31;
break;
case 4:case 6:case 9:case 11:
return 30;
break;
case 2:
//如果是閏年
if(CAL_IsLeapYear(iYear))
return 29;
else
return 28;
break;
}
return 0;
}
UINT16 CAL_GetLeapMonth(UINT16 iLunarYear)
{
UINT8 flag = gLunarMonth[(UINT8)((iLunarYear - START_YEAR)/2)];
return (UINT16) ((iLunarYear - START_YEAR)%2 ? flag&0x0f : flag>>4);
}
UINT32 CAL_LunarMonthDays(UINT16 iLunarYear, UINT16 iLunarMonth)
{
UINT16 height =0 ,low =29;
INT32 iBit = 16 - iLunarMonth;
if(iLunarYear < START_YEAR)
return 30L;
if(iLunarMonth > CAL_GetLeapMonth(iLunarYear) && CAL_GetLeapMonth(iLunarYear))
iBit --;
if(gLunarMonthDay[iLunarYear - START_YEAR] & (1<<iBit))
low ++;
if(iLunarMonth == CAL_GetLeapMonth(iLunarYear))
if(gLunarMonthDay[iLunarYear - START_YEAR] & (1<< (iBit -1)))
height =30;
else
height =29;
return ((height<<16)|(low));
}
UINT16 CAL_LunarYearDays(UINT16 iLunarYear)
{
UINT8 i;
UINT32 tmp;
/*
UINT16 days=348 ; //12*29
INT32 month = 12 ;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -