53.4 官方DIALOG_MessageBoxUser.c实例讲解
这个例子在模拟器中的位置:
主要功能介绍:
由于函数GUI_MessageBox创建的消息框控件比较小,所以官方提供了一个用户自定义方式创建消息框控件,
通过这个例子用户就可以学会如何重新设置消息框控件中的框架窗口,按钮控件和文本控件。程序代码如下: - #include "DIALOG.h"
- #include "MESSAGEBOX.h"
-
- /*********************************************************************
- *
- * Defines
- *
- **********************************************************************
- */
- //
- // Recommended memory to run the sample with adequate performance
- //
- #define RECOMMENDED_MEMORY (1024L * 10)
-
- /* 字体部分代码省略未贴 */
-
- /*********************************************************************
- *
- * Static code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * _CreateMessageBox
- */
- static WM_HWIN _CreateMessageBox(const char * sMessage, const char * sCaption, int Flags, const GUI_FONT * pFont) {
- WM_HWIN hWin;
- WM_HWIN hItem;
- GUI_RECT Rect;
-
- hWin = MESSAGEBOX_Create(sMessage, sCaption, Flags); //--------------(1)
- //
- // Change font of message box window
- //
- FRAMEWIN_SetFont(hWin, pFont); //--------------(2)
- //
- // Adjust size
- //
- WM_GetWindowRectEx(hWin, &Rect); //--------------(3)
- WM_SetWindowPos(hWin, Rect.x0 - 15,
- Rect.y0 - 15,
- Rect.x1 - Rect.x0 + 1 + 30,
- Rect.y1 - Rect.y0 + 1 + 30);
- //
- // Change font of button widget
- //
- hItem = WM_GetDialogItem(hWin, GUI_ID_OK); //--------------(4)
- BUTTON_SetFont(hItem, pFont);
- //
- // Adjust size of button widget
- //
- WM_GetWindowRectEx(hItem, &Rect); //--------------(5)
- WM_SetWindowPos(hItem, Rect.x0,
- Rect.y0 + 10,
- Rect.x1 - Rect.x0 + 1 + 30,
- Rect.y1 - Rect.y0 + 1 + 5);
- //
- // Change font of text widget
- //
- hItem = WM_GetDialogItem(hWin, GUI_ID_TEXT0); //--------------(6)
- TEXT_SetFont(hItem, pFont);
- //
- // Adjust size text widget
- //
- WM_GetWindowRectEx(hItem, &Rect); //--------------(7)
- WM_SetWindowPos(hItem, Rect.x0,
- Rect.y0,
- Rect.x1 - Rect.x0 + 1 + 30,
- Rect.y1 - Rect.y0 + 1 + 12);
- return hWin;
- }
-
- /*********************************************************************
- *
- * _EnableSkinning
- */
- static void _EnableSkinning(void) {
- FRAMEWIN_SetDefaultSkin(FRAMEWIN_SKIN_FLEX);
- BUTTON_SetDefaultSkin (BUTTON_SKIN_FLEX);
- }
-
- /*********************************************************************
- *
- * Public code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * MainTask
- */
- void MainTask(void) {
- GUI_Init();
- //
- // Check if recommended memory for the sample is available
- //
- if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
- GUI_ErrorOut("Not enough memory available.");
- return;
- }
- _EnableSkinning();
- while (1) {
- GUI_SetFont(&GUI_Font20B_ASCII);
- GUI_DispStringHCenterAt("Application defined MESSAGEBOX", 160, 5);
- GUI_ExecCreatedDialog(_CreateMessageBox("Message", "Caption", 0, &GUI_Font24)); //--------------(8)
- GUI_Clear();
- GUI_Delay(1000);
- }
- }
复制代码
1. 通过函数MESSAGEBOX_Create创建消息框控件,这个函数的使用也比较简单,第一个参数是用来设置文本控件显示的内容,第二个参数是标题栏显示的内容,第三个参数用来设置是否将其设置为模态。2. 由于消息框控件的组成是框架窗口,文本控件和按钮控件组成的对话框,所以函数MESSAGEBOX_Create返回的句柄实际上就是框架窗口的句柄,从而可以通过函数FRAMEWIN_SetFont调用这个句柄来设置框架窗口的标题字体。 3. 通过函数WM_GetWindowRectEx和WM_SetWindowPos来设置消息框控件(其实就是框架窗口)的大小。 4. 通过函数WM_GetDialogItem获取消息框控件上按钮的句柄(此按钮的ID是GUI_ID_OK),然后设置按钮的字体。 5. 通过函数WM_GetWindowRectEx和WM_SetWindowPos来设置按钮控件的大小。 6. 通过函数WM_GetDialogItem获取消息框控件上文本的句柄(此文本的ID是GUI_ID_TEXT0),然后设置文本的字体。 7. 通过函数WM_GetWindowRectEx和WM_SetWindowPos来设置文本控件的大小。 8. 通过函数GUI_ExecCreatedDialog将消息框控件以阻塞式运行。只有点击了消息框控件上面的按钮,此函数才可以返回。
实际显示效果如下: |