emwin设计三层可触屏切换页面并且主页面显示时间代码
时间: 2024-02-01 11:03:57 浏览: 125
【emWin】例程三:显示方向的切换-附件资源
在前面的代码基础上,实现三层可触屏切换页面并且主页面显示时间的代码如下:
```c
#include "GUI.h"
#include "DIALOG.h"
#include "rtc.h"
#include "TOUCH.h"
static WM_HWIN hWin0, hWin1, hWin2; // 三个窗口句柄
static void _cbWin0(WM_MESSAGE * pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT: {
GUI_Clear();
GUI_SetFont(&GUI_Font24B_ASCII);
GUI_DispStringAt("Page 0", 40, 40);
break;
}
default:
WM_DefaultProc(pMsg);
break;
}
}
static void _cbWin1(WM_MESSAGE * pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT: {
GUI_Clear();
GUI_SetFont(&GUI_Font24B_ASCII);
GUI_DispStringAt("Page 1", 40, 40);
break;
}
default:
WM_DefaultProc(pMsg);
break;
}
}
static void _cbWin2(WM_MESSAGE * pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT: {
GUI_Clear();
GUI_SetFont(&GUI_Font24B_ASCII);
GUI_DispStringAt("Page 2", 40, 40);
break;
}
default:
WM_DefaultProc(pMsg);
break;
}
}
void MainTask(void) {
GUI_Init();
TOUCH_Init();
hWin0 = WM_CreateWindow(0, 0, 240, 240, WM_CF_SHOW, _cbWin0, 0);
hWin1 = WM_CreateWindow(0, 0, 240, 240, WM_CF_HIDE, _cbWin1, 0);
hWin2 = WM_CreateWindow(0, 0, 240, 240, WM_CF_HIDE, _cbWin2, 0);
while (1) {
GUI_PID_STATE touch_state;
TOUCH_GetState(&touch_state);
if (touch_state.Pressed) {
if (touch_state.x < 80) {
WM_ShowWindow(hWin0);
WM_HideWindow(hWin1);
WM_HideWindow(hWin2);
} else if (touch_state.x < 160) {
WM_ShowWindow(hWin1);
WM_HideWindow(hWin0);
WM_HideWindow(hWin2);
} else {
WM_ShowWindow(hWin2);
WM_HideWindow(hWin0);
WM_HideWindow(hWin1);
}
}
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(&RTC_TimeStruct);
RTC_GetDate(&RTC_DateStruct);
char time_str[20];
sprintf(time_str, "%02d:%02d:%02d", RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, RTC_TimeStruct.RTC_Seconds);
GUI_Clear();
GUI_SetFont(&GUI_Font24B_ASCII);
GUI_DispStringAt(time_str, 150, 0);
GUI_Exec();
GUI_Delay(100);
}
}
```
这段代码在前面的基础上增加了触屏事件处理。使用 `TOUCH_GetState` 函数获取触屏状态,如果触摸屏被按下,则根据触摸位置切换窗口的显示。在三个窗口的回调函数中不需要处理触屏事件,因为触屏事件已经由主循环处理。
阅读全文