34.2 桌面窗口回调函数实例 这里用上期教程所介绍的自定义消息类型做一个实例。代码跟上期教程的三个实例类似。实现源码如下(可以直接将代码复制到模拟器或者开发板上面运行)。
- #include
- #include "GUI.h"
- #include "DIALOG.h"
-
- #include "WM.h"
- #include "BUTTON.h"
- #include "CHECKBOX.h"
- #include "DROPDOWN.h"
- #include "EDIT.h"
- #include "FRAMEWIN.h"
- #include "LISTBOX.h"
- #include "MULTIEDIT.h"
- #include "RADIO.h"
- #include "SLIDER.h"
- #include "TEXT.h"
- #include "PROGBAR.h"
- #include "SCROLLBAR.h"
- #include "LISTVIEW.h"
-
-
- #define WM_UPDATE WM_USER + 1
- /*********************************************************************
- *
- * static data
- *
- **********************************************************************
- */
- GUI_COLOR _acColor[3] = {GUI_BLUE,GUI_RED,GUI_YELLOW};
- static char ucBackColor;
-
-
-
- /*********************************************************************
- *
- * Dialog resource
- *
- * This table conatins the info required to create the dialog.
- * It has been created by ucGUIbuilder.
- */
-
- static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
- { FRAMEWIN_CreateIndirect, "armfly", 0, 0, 0, 800,480,FRAMEWIN_CF_MOVEABLE,0},
- { BUTTON_CreateIndirect, "BUTTON0", GUI_ID_BUTTON0, 82, 122,162,37, 0,0},
- { BUTTON_CreateIndirect, "BUTTON1", GUI_ID_BUTTON1, 357,123,152,35, 0,0}
- };
-
- /*****************************************************************
- ** FunctionName:void PaintDialog(WM_MESSAGE * pMsg)
- ** Function: to initialize the Dialog items
- **
- ** call this function in _cbCallback --> WM_PAINT
- *****************************************************************/
-
- void PaintDialog(WM_MESSAGE * pMsg)
- {
- WM_HWIN hWin = pMsg->hWin;
- // GUI_SetBkColor(_acColor[ucBackColor]);
- // GUI_Clear();
- }
-
- /*****************************************************************
- ** FunctionName:void InitDialog(WM_MESSAGE * pMsg)
- ** Function: to initialize the Dialog items
- **
- ** call this function in _cbCallback --> WM_INIT_DIALOG
- *****************************************************************/
-
- void InitDialog(WM_MESSAGE * pMsg)
- {
- WM_HWIN hWin = pMsg->hWin;
- //
- //FRAMEWIN
- //
- FRAMEWIN_SetFont(hWin,&GUI_Font24B_ASCII);
- FRAMEWIN_SetTextAlign(hWin,GUI_TA_VCENTER|GUI_TA_CENTER);
- FRAMEWIN_AddCloseButton(hWin, FRAMEWIN_BUTTON_RIGHT, 0);
- FRAMEWIN_AddMaxButton(hWin, FRAMEWIN_BUTTON_RIGHT, 1);
- FRAMEWIN_AddMinButton(hWin, FRAMEWIN_BUTTON_RIGHT, 2);
- FRAMEWIN_SetTitleHeight(hWin,30);
-
- /* 默认颜色取*/
- ucBackColor = 0;
- }
-
- /*********************************************************************
- *
- * Dialog callback routine
- */
- static void _cbCallback(WM_MESSAGE * pMsg)
- {
- int NCode, Id;
- WM_HWIN hWin = pMsg->hWin;
- switch (pMsg->MsgId)
- {
- case WM_PAINT:
- PaintDialog(pMsg);
- break;
- case WM_INIT_DIALOG:
- InitDialog(pMsg);
- break;
- case WM_KEY:
- switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key)
- {
- case GUI_KEY_ESCAPE:
- GUI_EndDialog(hWin, 1);
- break;
- case GUI_KEY_ENTER:
- GUI_EndDialog(hWin, 0);
- break;
- }
- break;
- case WM_NOTIFY_PARENT:
- Id = WM_GetId(pMsg->hWinSrc);
- NCode = pMsg->Data.v;
- switch (Id)
- {
- case GUI_ID_OK:
- if(NCode==WM_NOTIFICATION_RELEASED)
- GUI_EndDialog(hWin, 0);
- break;
- case GUI_ID_CANCEL:
- if(NCode==WM_NOTIFICATION_RELEASED)
- GUI_EndDialog(hWin, 0);
- break;
- case GUI_ID_BUTTON0:
- switch(NCode)
- {
- case WM_NOTIFICATION_CLICKED:
- break;
- case WM_NOTIFICATION_RELEASED:
-
- break;
- case WM_NOTIFICATION_MOVED_OUT:
-
- break;
- }
- break;
- case GUI_ID_BUTTON1:
- switch(NCode)
- {
- case WM_NOTIFICATION_CLICKED:
-
- break;
- case WM_NOTIFICATION_RELEASED:
-
- break;
- case WM_NOTIFICATION_MOVED_OUT:
-
- break;
- }
- break;
-
- }
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * Dialog callback routine
- */
- static void _cbBkWindow(WM_MESSAGE * pMsg) (1)
- {
- WM_HWIN hWin = pMsg->hWin;
-
- switch (pMsg->MsgId)
- {
- case WM_PAINT: (2)
- GUI_SetBkColor(_acColor[ucBackColor]);
- GUI_Clear();
- break;
- case WM_UPDATE:(3)
- ucBackColor++;
- if (ucBackColor == 3)
- {
- ucBackColor = 0;
- }
- WM_InvalidateWindow(hWin);
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * MainTask
- *
- **********************************************************************
- */
- void MainTask(void)
- {
- WM_HWIN hDlg;
- /*
- 使能窗口使用内存设备,这样可以有效避免闪烁 放在GUI_Init前面就包括桌面
- 窗口,如果放在后面就不包括桌面窗口。
- */
- WM_SetCreateFlags(WM_CF_MEMDEV); (4)
- GUI_Init();
- /* 设置桌面窗口回调函数 */
- WM_SetCallback(WM_HBKWIN, _cbBkWindow);(5)
- //WM_SetDesktopColor(GUI_BLUE); /* Automacally update desktop window */ (6)
- PROGBAR_SetDefaultSkin(PROGBAR_SKIN_FLEX);
- FRAMEWIN_SetDefaultSkin(FRAMEWIN_SKIN_FLEX);
- PROGBAR_SetDefaultSkin(PROGBAR_SKIN_FLEX);
- BUTTON_SetDefaultSkin(BUTTON_SKIN_FLEX);
- CHECKBOX_SetDefaultSkin(CHECKBOX_SKIN_FLEX);
- DROPDOWN_SetDefaultSkin(DROPDOWN_SKIN_FLEX);
- SCROLLBAR_SetDefaultSkin(SCROLLBAR_SKIN_FLEX);
- SLIDER_SetDefaultSkin(SLIDER_SKIN_FLEX);
- HEADER_SetDefaultSkin(HEADER_SKIN_FLEX);
- RADIO_SetDefaultSkin(RADIO_SKIN_FLEX);
- /* 创建一个对话框 */
- hDlg = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, 0, 0, 0);
- while (1)
- {
- WM_SendMessageNoPara(WM_HBKWIN, WM_UPDATE); (7)
- GUI_Delay(500);
- }
-
- }
复制代码
1. 桌面窗口的回调函数(桌面窗口是emWin最底层的窗口,是初始化后自动创建的)。 2. 重绘消息,用于实现桌面颜色的重绘。 3. 这个是自定义消息,功能和上面34.1小节讲的一样。 4. 使能窗口使用内存设备,这样可以有效的避免闪烁。 5. 通过函数WM_SetCallback来设置桌面窗口的回调函数。 6. 函数WM_SetDesktopColor可以实现桌面窗口颜色的自动重绘。 7. 这里要尤其的注意,系统消息由GUI库发送。请勿从用户应用程序向窗口或小工具发送系统定义的消息。要不会出现异常的。也就是说我们不能发WM_PAINT消息,但是可以发自定义消息,进而来触发重绘。 这个DEMO的功能就是每隔500ms改变一次桌面窗口的颜色,实际显示效果如下:
|