📄 uicalendar.c
字号:
return; // 控件不活动
if(message==PENDOWN)
{
day=_guiCalendar_SearchPos(handle,x,y); // 查找选择的日期
if(day>0)
{
_guiCalendar_Invert(handle); // 取消原选择的日期
pCalendar->day=day;
_guiCalendar_Invert(handle); // 反显新选择的日期
guiPenSound();
}
}
}
// 显示日历控件
STATUS _guiCalendar_Show(HNDL handle)
{
short left,top,right,bottom,x,y,hGap,vGap,width,height,tWidth,tHeight,offset,nx,ny;
WORD year,month,day,week,days,i,nMonth,nDay;
TGuiCalendar *pCalendar;
char buf[3];
guiEnterWCS();
if(handle == NULL) // 句柄错误
{
guiExitWCS();
return STATUS_ERR;
}
if(guiControl_IsVisible(handle) == FALSE) // 控件不可见
{
guiExitWCS();
return STATUS_ERR;
}
pCalendar=(TGuiCalendar *)handle;
left=0;
top=0;
right=pCalendar->base.right-pCalendar->base.left;
bottom=pCalendar->base.bottom-pCalendar->base.top;
guiClearBlock(handle,left,top,right,bottom,GUI_WHITE,0);
width=pCalendar->dayWidth; // 每日有效检测区的宽度和高度
height=pCalendar->dayHeight;
hGap=pCalendar->dayHGap; // 间隔
vGap=pCalendar->dayVGap;
tWidth=hGap+width; // 每日所占区域,包括周围的空白
tHeight=vGap+height;
year=pCalendar->year;
month=pCalendar->month;
day=1;
week=guiCalendar_GetWeek(year,month,day); // 计算1号是星期几
pCalendar->week=week;
days=_guiCalendar_GetDaysofMonth(year,month);// 计算当月天数
x=week*tWidth; // 1号的显示位置
y=0;
nx=(width-15)>>1;
ny=height-7;
guiPushFont(pCalendar->base.font);
buf[2]=0;
guiCalendar_GetNongli(year,month,day,&nMonth,&nDay);// 1号是农历几月几日
for(i=1;i<=days;i++) // 循环显示每一天的信息
{
buf[0]=i/10+'0';
buf[1]=i%10+'0';
if(buf[0]=='0')
buf[0]=' ';
offset=(width-guiGetStringWidth(buf))>>1;
guiShowString(handle,buf,x+offset,y,GUI_BLACK,0); // 显示公历日期
if(nDay>29) // 是否要进位到农历的下一月
guiCalendar_GetNongli(year,month,i,&nMonth,&nDay);
_guiCalendar_ShowNongli(handle,x+nx,y+ny,nMonth,nDay); // 显示农历
nDay++;
x+=tWidth; // 计算下一日的显示位置
if(x>right) // 换行
{
x=0;
y+=tHeight;
if(y>bottom)
y=0;
}
}
guiPopFont();
if(pCalendar->day>days)
pCalendar->day=days;
_guiCalendar_Invert(handle); // 当前日期
guiExitWCS();
return STATUS_OK;
}
// 删除日历控件
STATUS _guiCalendar_Delete(HNDL handle)
{
TGuiCalendar *pCalendar;
guiEnterWCS();
if(handle == NULL) // 句柄错误
{
guiExitWCS();
return STATUS_ERR;
}
pCalendar=(TGuiCalendar *)handle;
if(pCalendar->base.checkFlag != GUI_CONTROL_CHECK_FLAG)
{
guiExitWCS();
return STATUS_ERR; // 控件无效
}
pCalendar->base.checkFlag=0;
kernelFree(pCalendar); // 释放空间
guiExitWCS();
return STATUS_OK;
}
// 创建日历控件
HNDL guiCalendar_Create(short left,short top,short hGap,short vGap,WORD style)
{
TGuiCalendar *pCalendar;
int width,height;
guiEnterWCS();
pCalendar=(TGuiCalendar *)kernelMalloc(sizeof(TGuiCalendar)); // 分配控件存储空间
if(pCalendar == NULL)
{
guiExitWCS();
return 0; // 内存不够
}
pCalendar->base.handle=(HNDL)pCalendar;
pCalendar->base.left=left;
pCalendar->base.top=top;
pCalendar->base.type=CONTROL_CALENDAR;
pCalendar->base.checkFlag=GUI_CONTROL_CHECK_FLAG;
pCalendar->base.style=style;
pCalendar->base.status=0;
pCalendar->base.font=GUI_CALENDAR_FONT;
pCalendar->base.vportHandle=NULL;
pCalendar->base.container=NULL;
pCalendar->base.actionFun=_guiCalendar_Action;
pCalendar->base.showFun=_guiCalendar_Show;
pCalendar->base.delFun=_guiCalendar_Delete;
guiPushFont(GUI_CALENDAR_FONT);
width=guiGetStringWidth("88")+2; // 用两位数字表时日期时的显示宽度
height=guiGetStringHeight()+7;
guiPopFont();
if(width<17) // 日期的宽度不能小于农历的宽度
width=17;
pCalendar->dayWidth=width; // 检测区的高度和宽度
pCalendar->dayHeight=height;
pCalendar->dayHGap=hGap; // 水平和垂直间隔
pCalendar->dayVGap=vGap;
pCalendar->base.right=left+width*7+hGap*6-1; // 7列
if(style & CALENDAR_LINE6)
pCalendar->base.bottom=top+height*6+vGap*5-1; // 6行
else
pCalendar->base.bottom=top+height*5+vGap*4-1; // 5行
guiExitWCS();
return (HNDL)pCalendar;
}
// 展开日历控件
int guiCalendar_Popup(WORD *srcYear,WORD *srcMonth,WORD *srcDay)
{
HNDL hCalendar,hDialog,hYear,hMonth,hToday,hOk;
TGuiCalendar *pCalendar;
TGuiWindow *pWin;
TGuiMessage message;
sysTime now;
WORD year,month,day,week,style;
short left,top,hGap,vGap,titleHeight,yearHeight,yearWidth,monthWidth,dayWidth,dayHeight,buttonWidth;
int i,index;
if(*srcDay == 0 || *srcMonth ==0 || *srcYear == 0) // 开始日期
{
sc_getTime(&now); // 当前日期
day=now.day;
month=now.month;
#ifdef __WIN32__
//year=now.year+1990;
year=now.year; // by zhang xue ping
#else
year=now.year;
#endif
}
else
{
day=*srcDay;
month=*srcMonth;
year=*srcYear;
}
if( year<CALENDAR_YEAR_MIN || year>CALENDAR_YEAR_MAX )
return CALENDAR_ERR; // 年份错误
if( month<1 || month>12 )
return CALENDAR_ERR; // 月份错误
if( day<1 )
return CALENDAR_ERR; // 天数错误
if(day>_guiCalendar_GetDaysofMonth(year,month))
return CALENDAR_ERR; // 天数错误
guiEnterWCS();
// by zhang xue ping
//hDialog=guiDialog_Create(gpTopWindow->handle,0,0,GUI_SCREEN_WIDTH-1,GUI_SCREEN_HEIGHT-1,RES_STR_CALENDAR[gLanguage],0);
hDialog=guiDialog_Create(0,0,0,GUI_SCREEN_WIDTH-1,GUI_SCREEN_HEIGHT-1,RES_STR_CALENDAR[gLanguage],0);
if(hDialog==0)
{
guiExitWCS();
return CALENDAR_ERR; // 建立对话框错误
}
guiPushFont(GUI_DEFAULT_FONT);
titleHeight=guiGetStringHeight()+TITLE_HEIGHT_ADD; // 标题条高度
yearWidth=guiGetStringWidth("8888")+18;
yearHeight=guiGetStringHeight()+4;
monthWidth=guiGetStringWidth(RES_STR_MONTH[gLanguage][11])+12;
buttonWidth=guiGetStringWidth(RES_STR_TODAY[gLanguage])+12;
top=titleHeight+yearHeight+guiGetStringHeight()+9;
guiPopFont();
guiPushFont(GUI_CALENDAR_FONT);
dayWidth=guiGetStringWidth("88")+2;
dayHeight=guiGetStringHeight()+7;
guiPopFont();
if(dayWidth<17)
dayWidth=17;
hGap=(GUI_SCREEN_WIDTH-dayWidth*7)/6;
if(hGap>10)
hGap=10;
left=(GUI_SCREEN_WIDTH-hGap*6-dayWidth*7)>>1;
if( (dayHeight*6+20)>=GUI_SCREEN_HEIGHT-top )
{
style=CALENDAR_LINE5;
vGap=(GUI_SCREEN_HEIGHT-top-dayHeight*5)/4;
}
else
{
style=CALENDAR_LINE6;
vGap=(GUI_SCREEN_HEIGHT-top-dayHeight*6)/5;
}
if(vGap>8)
vGap=8;
hCalendar=guiCalendar_Create(left,top,hGap,vGap,style); // 建立日历控件
if(hCalendar == NULL)
{
guiExitWCS();
guiDialog_Delete(hDialog);
return CALENDAR_ERR; // 创建日历控件失败
}
pCalendar=(TGuiCalendar *)hCalendar;
pCalendar->day=day; // 将开始日期存入日历控件
pCalendar->month=month;
pCalendar->year=year;
// 标题条上的确定按钮
hOk=guiButton_Create(GUI_SCREEN_WIDTH-buttonWidth-2,0,GUI_SCREEN_WIDTH-3,titleHeight-1,RES_STR_OK[gLanguage],BUTTON_TITLE);
// “今天”按钮的宽度
buttonWidth+=12;
// 年、月、今天之间的间隔
hGap=pCalendar->base.right-pCalendar->base.left+1-yearWidth-monthWidth-buttonWidth;
hGap>>=1;
left=pCalendar->base.left; // 建立年选择器
top=titleHeight+2;
hYear=guiSelector_Create(left, top,left+yearWidth-1,top+yearHeight-1,SELECTOR_NUMBER|SELECTOR_ISLOOP);
guiSelector_Setup( hYear,CALENDAR_YEAR_MIN,CALENDAR_YEAR_MAX,year);
left+=yearWidth+hGap; // 建立月选择器
hMonth=guiSelector_Create(left,top,left+monthWidth-1,top+yearHeight-1,SELECTOR_ISLOOP);
for(i=0;i<12;i++)
guiSelector_AddItem(hMonth,RES_STR_MONTH[gLanguage][i]);
guiSelector_Select(hMonth,month-1);
left+=monthWidth+hGap; // 建立“今天”按钮
hToday=guiButton_Create(left,top,left+buttonWidth-1,top+yearHeight-1,RES_STR_TODAY[gLanguage],0);
// 将控件加入对话框
guiControl_Add(hDialog,hOk);
guiControl_Add(hDialog,hToday);
guiControl_Add(hDialog,hYear);
guiControl_Add(hDialog,hMonth);
guiControl_Add(hDialog,hCalendar);
if(guiDialog_Show(hDialog)==STATUS_ERR)
{
guiDialog_Delete(hDialog);
guiExitWCS();
return CALENDAR_ERR; // 显示日历控件失败
}
top+=yearHeight+2;
guiDrawLine(hDialog,0,top,GUI_SCREEN_WIDTH-1,top,GUI_BLACK,GUI_HATCH);
guiPushFont(GUI_DEFAULT_FONT); // 显示星期
top+=2;
left=pCalendar->base.left+((dayWidth-guiGetWordWidth())>>1);
hGap=pCalendar->dayWidth+pCalendar->dayHGap;
for(i=0;i<7;i++)
{
guiShowString(hDialog,RES_STR_WEEK[gLanguage][i],left,top,GUI_BLACK,0);
left+=hGap;
}
guiPopFont();
guiExitWCS();
pWin = (TGuiWindow *)hDialog;
while(1)
{
if( guiDequeue(pWin->messageQueue, &message) == -1)
continue;
switch(message.messageType)
{
case BUTTON_CLICK:
if(message.handle == hOk)
{
*srcYear=pCalendar->year;
*srcMonth=pCalendar->month;
*srcDay=pCalendar->day;
guiDialog_Delete(hDialog);
return CALENDAR_OK; // 确认日期选择
}
else if(message.handle == hToday)
{
sc_getTime(&now); // 当前日期
pCalendar->day=now.day;
pCalendar->month=now.month;
#ifdef __WIN32__
//pCalendar->year=now.year+1990;
pCalendar->year=now.year; // by zhang xue ping
#else
pCalendar->year=now.year;
#endif
guiSelector_Select(hYear,pCalendar->year-CALENDAR_YEAR_MIN);
guiSelector_Select(hMonth,pCalendar->month-1);
_guiCalendar_Show(hCalendar);
}
break;
case SELECTOR_CLICK:
if( message.handle == hYear )
{
index=guiSelector_GetSelectIndex(hYear);
pCalendar->year=index+CALENDAR_YEAR_MIN;
_guiCalendar_Show(hCalendar);
}
if( message.handle == hMonth )
{
index=guiSelector_GetSelectIndex(hMonth);
pCalendar->month=index+1;
_guiCalendar_Show(hCalendar);
}
break;
case WIN_CLOSE:
guiDialog_Delete(hDialog);
return CALENDAR_CANCLE; // 取消选择并返回
default:
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -