|
楼主 |
发表于 2017-3-11 15:49:16
|
显示全部楼层
54.4 官方WIDGET_MultieditTryIt.c实例讲解
这个例子在模拟器中的位置:
主要功能介绍:
如果打算深入学习多行文本控件,这个例子非常具有代表性,基本演示了所有多行文本控件的相关API函数。另外,由于这个例子比较长,我们直接将注释写到程序代码里面。
这个例子的整体框架比较清晰,就是创建了一个对话框,在上面创建了按钮控件,滑块控件,复选框等,这些控件用于设置多行文本控件的显示效果,从而可以让用户很方便的查看多行文本控件不同配置下的不同显示效果。程序代码如下: - #include <stddef.h>
- #include "GUI.h"
- #include "DIALOG.h"
-
- /*********************************************************************
- *
- * Defines
- *
- **********************************************************************
- */
- //
- // Recommended memory to run the sample with adequate performance
- //
- #define RECOMMENDED_MEMORY (1024L * 20)
-
- /*********************************************************************
- *
- * Static data
- *
- **********************************************************************
- */
- static int _Password = 0;
- static int _RdOnly = 0;
- static int _RTF8 = 0;
-
- /*********************************************************************
- *
- * Dialog resource
- *
- * 对话框资源列表
- *
- */
- static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
- { FRAMEWIN_CreateIndirect, "MultiEdit sample", 0, 3, 3, 314, 234, FRAMEWIN_CF_MOVEABLE },
- /* 多行文本的创建 */
- { MULTIEDIT_CreateIndirect, "Suspend", GUI_ID_MULTIEDIT0, 5, 5, 152, 120, 0, 0 },
- //
- // Read only check box
- //
- { CHECKBOX_CreateIndirect, 0, GUI_ID_CHECK0, 5, 110, 0, 0 },
- { TEXT_CreateIndirect, "R/O", 0, 25, 110, 50, 15, TEXT_CF_LEFT },
- //
- // UTF8 check box
- //
- { CHECKBOX_CreateIndirect, 0, GUI_ID_CHECK1, 50, 110, 0, 0 },
- { TEXT_CreateIndirect, "UTF8", 0, 70, 110, 50, 15, TEXT_CF_LEFT },
- //
- // Password mode check box
- //
- { CHECKBOX_CreateIndirect, 0, GUI_ID_CHECK2, 102, 110, 0, 0 },
- { TEXT_CreateIndirect, "Passw.", 0, 122, 110, 50, 15, TEXT_CF_LEFT },
- //
- // Radio button for scroll mode
- //
- { TEXT_CreateIndirect, "H", 0, 241, 3, 50, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "V", 0, 257, 3, 50, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Scroll", 0, 272, 3, 50, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Auto", 0, 272, 17, 50, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Off", 0, 272, 32, 50, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "On", 0, 272, 47, 50, 15, TEXT_CF_LEFT },
- { RADIO_CreateIndirect, NULL, GUI_ID_RADIO0, 236, 16, 0, 0, 0, 0x0f03 },
- { RADIO_CreateIndirect, NULL, GUI_ID_RADIO1, 252, 16, 0, 0, 0, 0x0f03 },
- //
- // Radio button for effect type
- //
- { TEXT_CreateIndirect, "Effect", 0, 169, 3, 60, 15, TEXT_CF_LEFT },
- { RADIO_CreateIndirect, NULL, GUI_ID_RADIO3, 165, 16, 51, 0, 0, 0x0f03 },
- //
- // Radio button for wrap mode
- //
- { TEXT_CreateIndirect, "Wrap", 0, 159, 155, 60, 15, TEXT_CF_LEFT },
- { RADIO_CreateIndirect, NULL, GUI_ID_RADIO2, 155, 168, 47, 0, 0, 0x0d03 },
- //
- // Prompt
- //
- { TEXT_CreateIndirect, "Prompt:", 0, 167, 65, 50, 15, TEXT_CF_LEFT },
- { EDIT_CreateIndirect, "Prompt:", GUI_ID_EDIT0, 212, 65, 45, 15, TEXT_CF_LEFT },
- { BUTTON_CreateIndirect, "Set", GUI_ID_BUTTON1, 262, 65, 35, 15 },
- //
- // Cursor offset
- //
- { TEXT_CreateIndirect, "CurOff:", 0, 167, 83, 50, 15, TEXT_CF_LEFT },
- { EDIT_CreateIndirect, 0, GUI_ID_EDIT1, 212, 83, 45, 15, TEXT_CF_LEFT, 3 },
- { BUTTON_CreateIndirect, "Set", GUI_ID_BUTTON2, 262, 83, 35, 15 },
- //
- // Window size
- //
- { TEXT_CreateIndirect, "Window size", 0, 167, 102, 60, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Y: ", 0, 167, 114, 60, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "X: ", 0, 167, 133, 60, 15, TEXT_CF_LEFT },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER0, 182, 114, 115, 20, 0},
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER1, 182, 133, 115, 20, 0},
- //
- // HBorder
- //
- { TEXT_CreateIndirect, "HBorder", 0, 207, 151, 60, 15, TEXT_CF_LEFT },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER2, 207, 163, 90, 20, 0},
- //
- // MaxNumChars
- //
- { TEXT_CreateIndirect, "NumChars", 0, 207, 181, 60, 15, TEXT_CF_LEFT },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER3, 207, 193, 90, 20, 0},
- //
- // Set font
- //
- { BUTTON_CreateIndirect, "Monosp. Font", GUI_ID_BUTTON6, 5, 130, 78, 20 },
- { BUTTON_CreateIndirect, "Prop. Font", GUI_ID_BUTTON7, 88, 130, 68, 20 },
- //
- // Set text
- { BUTTON_CreateIndirect, "Short\\nText", GUI_ID_BUTTON3, 5, 155, 47, 30 },
- { BUTTON_CreateIndirect, "Long\\nText", GUI_ID_BUTTON4, 57, 155, 47, 30 },
- { BUTTON_CreateIndirect, "Add\\nText", GUI_ID_BUTTON5, 107, 155, 47, 30 },
- //
- // Add. buttons
- //
- { BUTTON_CreateIndirect, "OK", GUI_ID_OK, 5, 190, 47, 20 },
- { BUTTON_CreateIndirect, "Cancel", GUI_ID_CANCEL, 57, 190, 47, 20 },
- };
-
- /*********************************************************************
- *
- * Static code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * _cbBkWindow
- * 桌面窗口的回调函数
- */
- static void _cbBkWindow(WM_MESSAGE* pMsg) {
- switch (pMsg->MsgId) {
- case WM_PAINT:
- GUI_SetBkColor(GUI_RED);
- GUI_Clear();
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(&GUI_Font24_ASCII);
- GUI_DispStringHCenterAt("WIDGET_MultiEdit", 160, 5);
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * _cbCallback
- * 对话框回调函数
- */
- static void _cbCallback(WM_MESSAGE * pMsg) {
- int NCode;
- int Id;
- int Sel;
- char ac[80];
- WM_HWIN hDlg;
- WM_HWIN hMulti;
- WM_HWIN hItem;
-
- hDlg = pMsg->hWin;
- hMulti = WM_GetDialogItem(hDlg, GUI_ID_MULTIEDIT0);
- switch (pMsg->MsgId) {
- /* 对话框中控件的初始化 */
- case WM_INIT_DIALOG:
- hItem = WM_GetDialogItem(hDlg, GUI_ID_SLIDER0);
- SLIDER_SetRange(hItem, 1, 100);
- SLIDER_SetValue(hItem, 100);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_SLIDER1);
- SLIDER_SetRange(hItem, 1, 152);
- SLIDER_SetValue(hItem, 152);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_SLIDER2);
- SLIDER_SetRange(hItem, 0, 5);
- SLIDER_SetValue(hItem, 2);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_SLIDER3);
- SLIDER_SetRange(hItem, 0, 100);
- SLIDER_SetValue(hItem, 0);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
- EDIT_SetDecMode(hItem, 0, 0, 1000, 0, 0);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_RADIO2);
- RADIO_SetText(hItem, "None", 0);
- RADIO_SetText(hItem, "Char", 1);
- RADIO_SetText(hItem, "Word", 2);
- hItem = WM_GetDialogItem(hDlg, GUI_ID_RADIO3);
- RADIO_SetText(hItem, "None", 0);
- RADIO_SetText(hItem, "Simple", 1);
- RADIO_SetText(hItem, "3D", 2);
- RADIO_SetValue(hItem, 2);
- RADIO_SetTextColor(hItem, GUI_BLUE);
- /* 使能多行文本光标的闪烁,300ms闪烁一次 */
- MULTIEDIT_EnableBlink(hMulti, 300, 1);
- /* 设置多行文本控件的字体 */
- MULTIEDIT_SetFont(hMulti, &GUI_Font8x16);
- /* 设置多行文本控件的背景色 */
- MULTIEDIT_SetBkColor(hMulti, 0, GUI_YELLOW);
- /* 设置多行文本控件中文本的显示颜色 */
- MULTIEDIT_SetTextColor(hMulti, 0, GUI_BLUE);
- /* 设置多行文本控件的垂直滚动条自动显示 */
- MULTIEDIT_SetAutoScrollV(hMulti, 1);
- /* 设置多行文本控件的插入模式 */
- MULTIEDIT_SetInsertMode(hMulti, 1);
- /* 设置多行文本控件的提示字符 */
- MULTIEDIT_SetPrompt(hMulti, "C:\\\\>");
- /* emWin手册中没有对这个函数进行说明,这里是设置文本显示距多行文本控件左侧边界的距离 */
- MULTIEDIT_SetHBorder(hMulti, 2);
- /* 设置多行文本显示的字符 */
- MULTIEDIT_SetText(hMulti, "Multiedit - long line 12345678901234567890\\nThis demonstrates the use of a
- MULTIEDIT widget");
- /* 这个函数是将外部键盘或者类似外部键盘的输入设备的值添加到多行文本控件,这里可以理解为通过模拟键盘进行
- 添加。
- */
- MULTIEDIT_AddKey(hMulti, 'A');
- MULTIEDIT_AddKey(hMulti, 'B');
- MULTIEDIT_AddKey(hMulti, 'C');
- MULTIEDIT_AddKey(hMulti, GUI_KEY_ENTER);
- /* 使能多行文本控件的水平滚动条 */
- MULTIEDIT_SetAutoScrollH(hMulti, 1);
- /* 设置按钮控件禁止聚焦 */
- hItem = WM_GetDialogItem(hDlg, GUI_ID_BUTTON5);
- BUTTON_SetFocussable(hItem, 0);
- break;
- case WM_KEY:
- switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key) {
- case GUI_KEY_ESCAPE:
- GUI_EndDialog(hDlg, 1);
- break;
- case GUI_KEY_ENTER:
- GUI_EndDialog(hDlg, 0);
- break;
- }
- break;
- case WM_NOTIFY_PARENT:
- Id = WM_GetId(pMsg->hWinSrc);
- NCode = pMsg->Data.v;
- hItem = WM_GetDialogItem(hDlg, Id);
- switch (NCode) {
- /* 控件的数值发生改变的消息处理 */
- case WM_NOTIFICATION_VALUE_CHANGED:
- switch (Id) {
- /* 设置多行文本控件的Y轴大小 */
- case GUI_ID_SLIDER0:
- Sel = SLIDER_GetValue(hItem);
- WM_SetYSize(hMulti, Sel);
- break;
- /* 设置多行文本控件的 X轴大小 */
- case GUI_ID_SLIDER1:
- Sel = SLIDER_GetValue(hItem);
- WM_SetXSize(hMulti, Sel);
- break;
- /* 设置多行文本控件的文本显示距左侧边界的距离 */
- case GUI_ID_SLIDER2:
- Sel = SLIDER_GetValue(hItem);
- MULTIEDIT_SetHBorder(hMulti, Sel);
- break;
- /* 设置多行文本控件最大支持的输入字符数 */
- case GUI_ID_SLIDER3:
- Sel = SLIDER_GetValue(hItem);
- MULTIEDIT_SetMaxNumChars(hMulti, Sel);
- break;
- case GUI_ID_RADIO0:
- //
- // 设置多换文本控件的水平滚动条
- //
- Sel = RADIO_GetValue(hItem);
- switch (Sel) {
- case 0:
- MULTIEDIT_SetAutoScrollH(hMulti, 1); // 设置自动显示
- break;
- case 1:
- MULTIEDIT_SetAutoScrollH(hMulti, 0); // 设置关闭
- WM_SetScrollbarH(hMulti, 0);
- break;
- case 2:
- MULTIEDIT_SetAutoScrollH(hMulti, 0); // 设置强制显示,也就不管需要不需要,一直显示
- WM_SetScrollbarH(hMulti, 1);
- break;
- }
- break;
- case GUI_ID_RADIO1:
- //
- // 设置垂直滚动条
- //
- Sel = RADIO_GetValue(hItem);
- switch (Sel) {
- case 0:
- MULTIEDIT_SetAutoScrollV(hMulti, 1); // 设置自动显示
- break;
- case 1:
- MULTIEDIT_SetAutoScrollV(hMulti, 0); // 设置关闭
- WM_SetScrollbarV(hMulti, 0);
- break;
- case 2:
- MULTIEDIT_SetAutoScrollV(hMulti, 0); // 设置强制显示,也就不管需要不需要,一直显示
- WM_SetScrollbarV(hMulti, 1);
- break;
- }
- break;
- case GUI_ID_RADIO2:
- //
- // 设置换行模式
- //
- Sel = RADIO_GetValue(hItem);
- switch (Sel) {
- case 0:
- MULTIEDIT_SetWrapNone(hMulti); //禁止换行
- break;
- case 1:
- MULTIEDIT_SetWrapChar(hMulti); //启用字符换行
- break;
- case 2:
- MULTIEDIT_SetWrapWord(hMulti); //启用字换行
- break;
- }
- break;
- case GUI_ID_RADIO3:
- //
- // 设置多行文本控件的显示效果
- //
- Sel = RADIO_GetValue(hItem);
- switch (Sel) {
- case 0:
- WIDGET_SetEffect(hMulti, &WIDGET_Effect_None); //禁止显示效果
- break;
- case 1:
- WIDGET_SetEffect(hMulti, &WIDGET_Effect_Simple); //使能简易显示效果
- break;
- case 2:
- WIDGET_SetEffect(hMulti, &WIDGET_Effect_3D); //使能3D显示效果
- break;
- }
- break;
- }
- break;
- /* 控件释放消息处理 */
- case WM_NOTIFICATION_RELEASED:
- WM_GetDialogItem(hDlg, Id);
- switch (Id) {
- case GUI_ID_OK:
- GUI_EndDialog(hDlg, 0);
- break;
- case GUI_ID_CANCEL:
- GUI_EndDialog(hDlg, 1);
- break;
- /* 多行文本控件是否使能只读的反转设置 */
- case GUI_ID_CHECK0:
- _RdOnly ^= 1;
- MULTIEDIT_SetReadOnly(hMulti, _RdOnly);
- break;
- /* 多行文本控件是否使能UTF-8编码的反转设置 */
- case GUI_ID_CHECK1:
- _RTF8 ^= 1;
- if (_RTF8) {
- GUI_UC_SetEncodeUTF8();
- } else {
- GUI_UC_SetEncodeNone();
- }
- WM_InvalidateWindow(hMulti);
- break;
- /* 多行文本控件是否使能密码显示效果的反转设置 */
- case GUI_ID_CHECK2:
- _Password ^= 1;
- MULTIEDIT_SetPasswordMode(hMulti, _Password);
- break;
- /* 多行文本控件的提示字符设置 */
- case GUI_ID_BUTTON1:
- hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT0);
- EDIT_GetText(hItem, &ac[0], sizeof(ac));
- MULTIEDIT_SetPrompt(hMulti, ac);
- break;
- case GUI_ID_BUTTON2:
- hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
- MULTIEDIT_SetCursorOffset(hMulti, EDIT_GetValue(hItem));
- WM_SetFocus(hMulti);
- break;
- /* 多行文本控件显示字符 */
- case GUI_ID_BUTTON3:
- MULTIEDIT_SetText(hMulti, "Short text");
- break;
- case GUI_ID_BUTTON4:
- MULTIEDIT_SetText(hMulti, "Long text\\n123456789012345678901234567890\\nLast line\\n");
- break;
- /* 多行文本控件添加字符 */
- case GUI_ID_BUTTON5:
- MULTIEDIT_AddText(hMulti, "Added");
- break;
- /* 多行文本文件字体设置 */
- case GUI_ID_BUTTON6:
- MULTIEDIT_SetFont(hMulti, &GUI_Font8x16);
- break;
- case GUI_ID_BUTTON7:
- MULTIEDIT_SetFont(hMulti, &GUI_Font16_1);
- break;
- }
- break;
- }
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * 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;
- }
- WM_SetCallback(WM_HBKWIN, _cbBkWindow);
- WM_SetCreateFlags(WM_CF_MEMDEV); // Use memory devices on all windows to avoid flicker
- while (1) {
- GUI_ExecDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbCallback, 0, 0, 0);
- GUI_Delay(1000);
- }
- }
复制代码
实际显示效果如下: |
|