問題:如何利用微軟提供的標(biāo)準(zhǔn)日歷控件來獲得時(shí)間、日期?
答案:微軟標(biāo)準(zhǔn)日歷控件“Microsoft Date and Time Picker Control 6.0(SP4)”可以提供日期、時(shí)間兩種格式,通過此控件獲得日期、時(shí)間、星期。
工具/原料
- 組態(tài)王6.55
- 計(jì)算機(jī)一臺(tái)
方法/步驟
-
新建工程,
定義8個(gè)內(nèi)存整數(shù)變量年、月、日、時(shí)、分、秒、星期、周;
定義2個(gè)內(nèi)存字符串變量:日期、時(shí)間;
-
制作畫面:
新建畫面,名為:日歷控件。
點(diǎn)擊工具箱“插入通用控件”,
選擇“Microsoft Date and Time Picker Control 6.0(SP4)”;
如圖所示:
-
畫出矩形的日歷控件,雙擊控件,命名為:“ADate”。
畫出時(shí)間日歷控件,雙擊控件,命名為:“ATime”;
選中“ATime”控件,右擊選擇“控件屬性”,
進(jìn)行“格式”設(shè)置,4種格式,
選擇“2-dtpTime”,確定。
"ATime"控件由日期變?yōu)闀r(shí)間。
-
控件屬性介紹:
常用控件屬性和事件:
屬性:FLOAT型--Day、Month、Year、DayOfWeek(周)、Hour、Minute、Second、
BOOL型--Enableed;
事件:Change()--在選擇時(shí)間的時(shí)候使用;
CloseUp()--在選擇日期的時(shí)候使用;
Enabled屬性控制此控件是否允許被使用。
如圖所示:
-
控件使用:
在動(dòng)畫連接屬性,選擇“事件”,雙擊“CloseUp”事件的關(guān)聯(lián)函數(shù),彈出“控件事件函數(shù)編輯框”。
-
函數(shù)聲明處為函數(shù)命名,如“CloseUp()”;
在命令語言編輯框中編寫腳本程序,
點(diǎn)擊編輯窗口下方“控件”按鈕,彈出“控件屬性和控件方法”,
在控件名稱中選擇“ADate”日歷控件;
在查看類型中選擇“控件屬性”;
在屬性或方法中選擇“Year”。
-
腳本程序分享如下:(請(qǐng)投票)
日期:
string temp;
\\本站點(diǎn)\年=ADate.Year;
\\本站點(diǎn)\月=ADate.Month;
\\本站點(diǎn)\日=ADate.Day;
\\本站點(diǎn)\周=ADate.DayOfWeek;
temp=StrFromInt(\\本站點(diǎn)\年,10);
if(\\本站點(diǎn)\月<10)
temp=temp+"/0"+StrFromInt(\\本站點(diǎn)\月,10);//一位數(shù)要加零
else
temp=temp+"/"+StrFromInt(\\本站點(diǎn)\月,10);
if(\\本站點(diǎn)\日<10)
temp=temp+"/0"+StrFromInt(\\本站點(diǎn)\日,10);
else
temp=temp+"/"+StrFromInt(\\本站點(diǎn)\日,10);
\\本站點(diǎn)\日期=temp;
if(\\本站點(diǎn)\周==1)
\\本站點(diǎn)\星期="星期日";
if(\\本站點(diǎn)\周==2)
\\本站點(diǎn)\星期="星期一";
if(\\本站點(diǎn)\周==3)
\\本站點(diǎn)\星期="星期二";
if(\\本站點(diǎn)\周==4)
\\本站點(diǎn)\星期="星期三";
if(\\本站點(diǎn)\周==5)
\\本站點(diǎn)\星期="星期四";
if(\\本站點(diǎn)\周==6)
\\本站點(diǎn)\星期="星期五";
if(\\本站點(diǎn)\周==7)
\\本站點(diǎn)\星期="星期六";
時(shí)間:
string temp;
\\本站點(diǎn)\時(shí)=ATime.Hour;
\\本站點(diǎn)\分=ATime.Minute;
\\本站點(diǎn)\秒=ATime.Second;
if(\\本站點(diǎn)\時(shí)<10)
temp="0"+StrFromInt(\\本站點(diǎn)\時(shí),10);//10是十進(jìn)制的樣子;
else
temp=StrFromInt(\\本站點(diǎn)\時(shí),10);
if(\\本站點(diǎn)\分<10)
temp=temp+":0"+StrFromInt(\\本站點(diǎn)\分,10);
else
temp=temp+":"+StrFromInt(\\本站點(diǎn)\分,10);
if(\\本站點(diǎn)\秒<10)
temp=temp+":0"+StrFromInt(\\本站點(diǎn)\秒,10);
else
temp=temp+":"+StrFromInt(\\本站點(diǎn)\秒,10);
\\本站點(diǎn)\時(shí)間=temp;
-
保存畫面,
運(yùn)行系統(tǒng)
如圖所示:
END
注意事項(xiàng)
- 注意動(dòng)畫連接屬性的相關(guān)設(shè)置
- 日期時(shí)間選擇合適的


返回頂部
刷新頁面
下到頁底