📄 mwclock.c
字号:
memset(szTimer,0,sizeof(szTimer));
// 开始消息循环
while(1)
{
GrGetNextEventTimeout(&event, 100L);
switch(event.type)
{
case GR_EVENT_TYPE_EXPOSURE:
GrText(wSetButton,DCButton,4,13,"设置...",-1,GR_TFASCII);
DrawButton(wSetButton,60,20,0);
break;
case GR_EVENT_TYPE_CLOSE_REQ:
// 关闭按钮
if (event.general.wid == w)
{
GrDestroyWindow(wSetButton);
GrDestroyWindow(w);
GrDestroyGC(DCTimer);
GrDestroyGC(DCButton);
GrDestroyFont(ChineseFont);
GrClose();
exit(0);
}
break;
case GR_EVENT_TYPE_BUTTON_DOWN:
// 按下按钮
if (event.general.wid == wSetButton)
{
DrawButton(wSetButton,60,20,1);
do_setup_time(w);
DrawButton(wSetButton,60,20,0);
}
break;
case GR_EVENT_TYPE_TIMEOUT:
// 我们在超时的时间里显示时间
nSeconds = ((rBCDSEC & 0x70) >> 4)*10 + (rBCDSEC & 0xf);
nMinutes = ((rBCDMIN & 0x70) >> 4)*10 + (rBCDMIN & 0xf);
nHours = ((rBCDHOUR & 0x70) >> 4)*10 + (rBCDHOUR & 0xf);
nDay = ((rBCDDAY & 0x70) >> 4)*10 + (rBCDDAY & 0xf);
nMonth = ((rBCDMON & 0x70) >> 4)*10 + (rBCDMON & 0xf);
nYear = rBCDYEAR;
GrSetGCForeground(DCTimer,GrGetSysColor(GR_COLOR_BTNFACE));
GrText(w,DCTimer,3,15,szTimer,-1,GR_TFASCII);
sprintf(szTimer,"20%02d年%02d月%02d日 %02d:%02d:%02d",nYear,nMonth,nDay,nHours,nMinutes,nSeconds);
GrSetGCForeground(DCTimer,BLACK);
GrText(w,DCTimer,3,15,szTimer,-1,GR_TFASCII);
//GrFlush();
break;
default:
break;
}
}
}
void do_setup_time(GR_WINDOW_ID wRootWindow)
{
GR_WINDOW_ID wSetupWindow;
GR_WINDOW_ID wOKButton;
GR_WINDOW_ID wCancelButton;
GR_EVENT event;
GR_GC_ID DCLabels;
int nYear=0;
int nMonth=0;
int nDay=0;
int nHours=0;
int nMinutes=0;
int nSeconds=0;
SPINBUTTON SpinButtons[6*2];
int i=0;
int nValue=1;
int nTopPosition=12;
// 设置事件窗体
wSetupWindow = GrNewWindowEx(
GR_WM_PROPS_MAXIMIZE|GR_WM_PROPS_NOAUTOMOVE, "Clock Setup", GR_ROOT_WINDOW_ID,
50, 50, 230, 120, GrGetSysColor(GR_COLOR_BTNFACE));
// OK按钮
wOKButton = GrNewWindowEx(
GR_WM_PROPS_BORDER|GR_WM_PROPS_APPFRAME,
"OK", wSetupWindow,
160, 10, 60, 20, GrGetSysColor(GR_COLOR_BTNFACE));
// Cancel按钮
wCancelButton = GrNewWindowEx(
GR_WM_PROPS_BORDER|GR_WM_PROPS_APPFRAME,
"Cancel", wSetupWindow,
160, 30, 60, 20, GrGetSysColor(GR_COLOR_BTNFACE));
// 事件选择
GrSelectEvents(wSetupWindow, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_TIMEOUT);
GrSelectEvents(wOKButton, GR_EVENT_MASK_BUTTON_DOWN);
GrSelectEvents(wCancelButton, GR_EVENT_MASK_BUTTON_DOWN);
// 下面生成那些 + - 按钮
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
if (nValue == 1)
{
// + 按钮
SpinButtons[i].wWindow = GrNewWindowEx(GR_WM_PROPS_BORDER|GR_WM_PROPS_APPFRAME, "Spin", wSetupWindow,
100, nTopPosition, 14, 14, GrGetSysColor(GR_COLOR_BTNFACE));
}
else
{
// - 按钮
SpinButtons[i].wWindow = GrNewWindowEx(GR_WM_PROPS_BORDER|GR_WM_PROPS_APPFRAME, "Spin", wSetupWindow,
120, nTopPosition, 14, 14, GrGetSysColor(GR_COLOR_BTNFACE));
nTopPosition += 15;
}
// 选择事件
GrSelectEvents(SpinButtons[i].wWindow, GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP);
// 显示按钮
GrMapWindow(SpinButtons[i].wWindow);
// 设置增加的数值
SpinButtons[i].nValue = nValue;
// 最大和最小值
SpinButtons[i].nMin = 0;
SpinButtons[i].nMax = 59;
// 因为一个是+按钮,一个是-按钮,所以增加的数量一会儿是1,一会儿是-1,这里用一个取反操作实现
nValue = -nValue;
}
// 显示其他窗口
GrMapWindow(wSetupWindow);
GrMapWindow(wOKButton);
GrMapWindow(wCancelButton);
// 设置绘图句柄和颜色,字体
DCLabels = GrNewGC();
GrSetGCForeground(DCLabels,BLACK);
GrSetGCBackground(DCLabels,GrGetSysColor(GR_COLOR_BTNFACE));
GrSetGCFont(DCLabels,ChineseFont);
// 获取RTC寄存器里的信息
nSeconds = ((rBCDSEC & 0x70) >> 4)*10 + (rBCDSEC & 0xf);
nMinutes = ((rBCDMIN & 0x70) >> 4)*10 + (rBCDMIN & 0xf);
nHours = ((rBCDHOUR & 0x70) >> 4)*10 + (rBCDHOUR & 0xf);
nDay = ((rBCDDAY & 0x70) >> 4)*10 + (rBCDDAY & 0xf);
nMonth = ((rBCDMON & 0x70) >> 4)*10 + (rBCDMON & 0xf);
nYear = rBCDYEAR;
// 设置操作的目标变量和最大最小数值
SpinButtons[0].pValue = &nYear; SpinButtons[0].nMax = 99;
SpinButtons[1].pValue = &nYear; SpinButtons[1].nMax = 99;
SpinButtons[2].pValue = &nMonth; SpinButtons[2].nMax = SpinButtons[3].nMax = 12;
SpinButtons[3].pValue = &nMonth; SpinButtons[2].nMin = SpinButtons[3].nMin = 1;
SpinButtons[4].pValue = &nDay; SpinButtons[4].nMin = SpinButtons[5].nMin = 1;
SpinButtons[5].pValue = &nDay; SpinButtons[4].nMax = SpinButtons[5].nMax = 31;
SpinButtons[6].pValue = &nHours; SpinButtons[6].nMax = SpinButtons[7].nMax = 23;
SpinButtons[7].pValue = &nHours;
SpinButtons[8].pValue = &nMinutes;
SpinButtons[9].pValue = &nMinutes;
SpinButtons[10].pValue = &nSeconds;
SpinButtons[11].pValue = &nSeconds;
// 消息循环
while(1)
{
GrGetNextEventTimeout(&event, 100L);
switch(event.type)
{
case GR_EVENT_TYPE_EXPOSURE:
if (event.general.wid == wSetupWindow)
{
// 显示文字
GrText(wSetupWindow,DCLabels,4,15+10,"年:",-1,GR_TFASCII);
GrText(wSetupWindow,DCLabels,4,30+10,"月:",-1,GR_TFASCII);
GrText(wSetupWindow,DCLabels,4,45+10,"日:",-1,GR_TFASCII);
GrText(wSetupWindow,DCLabels,4,60+10,"小时:",-1,GR_TFASCII);
GrText(wSetupWindow,DCLabels,4,75+10,"分钟:",-1,GR_TFASCII);
GrText(wSetupWindow,DCLabels,4,90+10,"秒:",-1,GR_TFASCII);
// 按钮上的文字
GrText(wOKButton,DCLabels,16,13,"确定",-1,GR_TFASCII);
GrText(wCancelButton,DCLabels,16,13,"取消",-1,GR_TFASCII);
// 绘制按钮边框
DrawButton(wOKButton,60,20,0);
DrawButton(wCancelButton,60,20,0);
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
if (SpinButtons[i].nValue > 0)
{
GrText(SpinButtons[i].wWindow,DCLabels,3,11,"+",-1,GR_TFASCII);
}
else
{
GrText(SpinButtons[i].wWindow,DCLabels,3,11,"-",-1,GR_TFASCII);
}
DrawButton(SpinButtons[i].wWindow,14,14,0);
}
}
break;
case GR_EVENT_TYPE_BUTTON_DOWN:
if (event.general.wid == wOKButton)
{
DrawButton(wOKButton,60,20,1);
// 保存信息
rBCDSEC = NumberToBCD(nSeconds);
rBCDMIN = NumberToBCD(nMinutes);
rBCDHOUR = NumberToBCD(nHours);
rBCDDAY = NumberToBCD(nDay);
rBCDMON = NumberToBCD(nMonth);
rBCDYEAR = nYear;
// 销毁+ -按钮
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
GrDestroyWindow(SpinButtons[i].wWindow);
}
// 按钮和主窗体
GrDestroyWindow(wOKButton);
GrDestroyWindow(wCancelButton);
GrDestroyWindow(wSetupWindow);
GrDestroyGC(DCLabels);
return;
}
else if (event.general.wid == wCancelButton)
{
DrawButton(wCancelButton,60,20,1);
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
GrDestroyWindow(SpinButtons[i].wWindow);
}
GrDestroyWindow(wOKButton);
GrDestroyWindow(wCancelButton);
GrDestroyWindow(wSetupWindow);
GrDestroyGC(DCLabels);
return;
}
else
{
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
if (SpinButtons[i].wWindow == event.general.wid)
{
// TODO: 这里没有判断具体的日期有效性,比如2月30日这种情况。
if (((SpinButtons[i].nValue > 0) && (*SpinButtons[i].pValue < SpinButtons[i].nMax))
|| ((SpinButtons[i].nValue < 0) && (*SpinButtons[i].pValue > SpinButtons[i].nMin)))
{
// 设置新的数值
*SpinButtons[i].pValue += SpinButtons[i].nValue;
}
DrawButton(SpinButtons[i].wWindow,14,14,1);
break;
}
}
}
break;
case GR_EVENT_TYPE_BUTTON_UP:
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
if (SpinButtons[i].wWindow == event.general.wid)
{
DrawButton(SpinButtons[i].wWindow,14,14,0);
break;
}
}
break;
case GR_EVENT_TYPE_CLOSE_REQ:
for (i=0;i<sizeof(SpinButtons)/sizeof(SpinButtons[0]);i++)
{
GrDestroyWindow(SpinButtons[i].wWindow);
}
GrDestroyWindow(wOKButton);
GrDestroyWindow(wCancelButton);
GrDestroyWindow(wSetupWindow);
GrDestroyGC(DCLabels);
return;
break;
case GR_EVENT_TYPE_TIMEOUT:
// 在TIMEOUT事件里显示
PrintTimer(nYear,60,15+10,DCLabels,wSetupWindow);
PrintTimer(nMonth,60,30+10,DCLabels,wSetupWindow);
PrintTimer(nDay,60,45+10,DCLabels,wSetupWindow);
PrintTimer(nHours,60,60+10,DCLabels,wSetupWindow);
PrintTimer(nMinutes,60,75+10,DCLabels,wSetupWindow);
PrintTimer(nSeconds,60,90+10,DCLabels,wSetupWindow);
break;
}
}
}
void PrintTimer(int nValue,int X,int Y,GR_GC_ID hDC,GR_WINDOW_ID wWindow)
{
char szTimer[10];
sprintf(szTimer," "); // 先显示空字符,用来擦除上次显示的内容,不然在上次显示2位这次显示1位的时候会出问题
GrText(wWindow,hDC,X,Y,szTimer,-1,GR_TFASCII);
sprintf(szTimer,"%d",nValue); // 实际的数值
GrText(wWindow,hDC,X,Y,szTimer,-1,GR_TFASCII);
}
void DrawButton(GR_WINDOW_ID wButton,int Width,int Height,int bDown)
{
GR_GC_ID DCButton;
DCButton = GrNewGC();
if (!bDown)
{
GrSetGCForeground(DCButton,GrGetSysColor(GR_COLOR_BTNHIGHLIGHT));
GrLine(wButton,DCButton,0,0,0,Height);
GrLine(wButton,DCButton,0,0,Width,0);
GrSetGCForeground(DCButton,GrGetSysColor(GR_COLOR_BTNSHADOW));
GrLine(wButton,DCButton,0,Height-1,Width,Height-1);
GrLine(wButton,DCButton,Width-1,0,Width-1,Height-1);
}
else
{
GrSetGCForeground(DCButton,GrGetSysColor(GR_COLOR_BTNSHADOW));
GrLine(wButton,DCButton,0,0,0,Height);
GrLine(wButton,DCButton,0,0,Width,0);
GrSetGCForeground(DCButton,GrGetSysColor(GR_COLOR_BTNHIGHLIGHT));
GrLine(wButton,DCButton,0,Height-1,Width,Height-1);
GrLine(wButton,DCButton,Width-1,0,Width-1,Height-1);
}
GrDestroyGC(DCButton);
}
int NumberToBCD(int nValue)
{
int nHigh = nValue / 10;
int nLow = nValue - nHigh * 10;
return (nHigh << 4) | nLow;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -