42.4 创建对话框
创建对话框需要两个基本要素:资源列表和对话框回调函数,资源列表用来定义所要创建的控件,回调函数用来处理各种消息类型。一旦具备这两个要素,则只需调用函数GUI_CreateDialogBox(非阻塞是对话框)或GUI_ExecDialogBox(阻塞式对话框)就能创建对话框。 这里只是给大家讲解一下各个部分的原理,具体的创建方法可以看前面第6章:GUIBuilder5.32的使用方法和第7章uCGUIBulder4.1的使用方法,这两个小软件都是以对话框为模板创建控件的,后面章节讲解各个控件时还会继续为大家加强对话框方面的认识。
42.4.1 资源列表
对话框可以基于阻塞(使用GUI_ExecDialogBox())或非阻塞(使用GUI_CreateDialogBox())方式创建。首先,必须定义一个资源类别,以指定在对话框中要创建的控件,下面是一个创建好的资源列表实例: - static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] =
- {
- { FRAMEWIN_CreateIndirect, "Dialog", 0, 10, 10, 180, 230, FRAMEWIN_CF_MOVEABLE, 0 },
- { BUTTON_CreateIndirect, "OK", GUI_ID_OK, 100, 5, 60, 20 },
- { BUTTON_CreateIndirect, "Cancel", GUI_ID_CANCEL, 100, 30, 60, 20 },
- { TEXT_CreateIndirect, "LText", 0, 10, 55, 48, 15, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "RText", 0, 10, 80, 48, 15, TEXT_CF_RIGHT },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT0, 60, 55, 100, 15, 0, 50 },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT1, 60, 80, 100, 15, 0, 50 },
- { TEXT_CreateIndirect, "Hex", 0, 10, 100, 48, 15, TEXT_CF_RIGHT },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT2, 60, 100, 100, 15, 0, 6 },
- { TEXT_CreateIndirect, "Bin", 0, 10, 120, 48, 15, TEXT_CF_RIGHT },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT3, 60, 120, 100, 15 },
- { LISTBOX_CreateIndirect, NULL, GUI_ID_LISTBOX0,10, 10, 48, 40 },
- { CHECKBOX_CreateIndirect, NULL, GUI_ID_CHECK0, 10, 140, 0, 0 },
- { CHECKBOX_CreateIndirect, NULL, GUI_ID_CHECK1, 30, 140, 0, 0 },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER0, 60, 140, 100, 20 },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER1, 10, 170, 150, 30 }
- };
复制代码对话框中要创建的任何控件都必须使用<WIDGET>_CreateIndirect()函数来间接创建,这个在前面第41章41.7.1小节进行了专门讲解,这里不再赘述。
42.4.2 对话框回调函数
上述42.4.1小节的资源列表再配合下面的回调函数就可以创建对话框了: - /*********************************************************************
- *
- * 对话框回调函数
- */
- static void _cbCallback(WM_MESSAGE * pMsg) {
- switch (pMsg->MsgId) {
- default:
- WM_DefaultProc(pMsg);
- }
- }
复制代码对话框的创建函数采用阻塞式API函数GUI_ExecDialogBox: GUI_ExecDialogBox(_aDialogCreate,GUI_COUNTOF(_aDialogCreate),&_cbCallback, 0, 0, 0); 所生成的对话框显示效果如下(实际外观则取决于用户配置和系统默认设置): 创建对话框后,所有资源列表中的控件都将可见。但他们仅仅只是显示出来,没有任何实际功能,因为回调函数里面什么都没有做,只有默认的系统处理函数WM_DefaultProc。
42.4.3 配置初对话框回调消息WM_INIT_DIALOG
下面将上述42.4.2小节回调函数中加入对话框初始化消息WM_INIT_DIALOG,代码如下: - /*********************************************************************
- *
- * 对话框回调函数
- */
- static void _cbCallback(WM_MESSAGE * pMsg)
- {
- int NCode, Id;
- WM_HWIN hEdit0, hEdit1, hEdit2, hEdit3, hListBox;
- WM_HWIN hWin = pMsg->hWin;
-
- switch (pMsg->MsgId)
- {
- case WM_INIT_DIALOG:
-
- /* Get window handles for all widgets */
- hEdit0 = WM_GetDialogItem(hWin, GUI_ID_EDIT0);
- hEdit1 = WM_GetDialogItem(hWin, GUI_ID_EDIT1);
- hEdit2 = WM_GetDialogItem(hWin, GUI_ID_EDIT2);
- hEdit3 = WM_GetDialogItem(hWin, GUI_ID_EDIT3);
- hListBox = WM_GetDialogItem(hWin, GUI_ID_LISTBOX0);
-
- /* Initialize all widgets */
- EDIT_SetText(hEdit0, "EDIT widget 0");
- EDIT_SetText(hEdit1, "EDIT widget 1");
- EDIT_SetTextAlign(hEdit1, GUI_TA_LEFT);
- EDIT_SetHexMode(hEdit2, 0x1234, 0, 0xffff);
- EDIT_SetBinMode(hEdit3, 0x1234, 0, 0xffff);
- LISTBOX_SetText(hListBox, _apListBox);
-
- WM_DisableWindow (WM_GetDialogItem(hWin, GUI_ID_CHECK1));
-
- CHECKBOX_Check( WM_GetDialogItem(hWin, GUI_ID_CHECK0));
- CHECKBOX_Check( WM_GetDialogItem(hWin, GUI_ID_CHECK1));
-
- SLIDER_SetWidth( WM_GetDialogItem(hWin, GUI_ID_SLIDER0), 5);
- SLIDER_SetValue( WM_GetDialogItem(hWin, GUI_ID_SLIDER1), 50);
- break;
-
- default:
- WM_DefaultProc(pMsg);
- )
- }
- }
复制代码 加入了对话框初始化消息后,所有控件都包含了它们各自的初始值,显示效果如下:
42.4.4 定义对话框行为
对话框上的控件有了各自的初始化数值后,就可以为各个控件添加具体的功能了,也就是在回调函数里面添加相应控件的回调消息,代码如下所示,这里有个感性认识即可: - /*********************************************************************
- *
- * 对话框回调函数
- */
-
- static void _cbCallback(WM_MESSAGE * pMsg)
- {
- int NCode, Id;
- WM_HWIN hEdit0, hEdit1, hEdit2, hEdit3, hListBox;
- WM_HWIN hWin = pMsg->hWin;
-
- switch (pMsg->MsgId)
- {
- case WM_INIT_DIALOG:
-
- /* Get window handles for all widgets */
- hEdit0 = WM_GetDialogItem(hWin, GUI_ID_EDIT0);
- hEdit1 = WM_GetDialogItem(hWin, GUI_ID_EDIT1);
- hEdit2 = WM_GetDialogItem(hWin, GUI_ID_EDIT2);
- hEdit3 = WM_GetDialogItem(hWin, GUI_ID_EDIT3);
- hListBox = WM_GetDialogItem(hWin, GUI_ID_LISTBOX0);
-
- /* Initialize all widgets */
- EDIT_SetText(hEdit0, "EDIT widget 0");
- EDIT_SetText(hEdit1, "EDIT widget 1");
- EDIT_SetTextAlign(hEdit1, GUI_TA_LEFT);
- EDIT_SetHexMode(hEdit2, 0x1234, 0, 0xffff);
- EDIT_SetBinMode(hEdit3, 0x1234, 0, 0xffff);
-
- LISTBOX_SetText(hListBox, _apListBox);
-
- WM_DisableWindow (WM_GetDialogItem(hWin, GUI_ID_CHECK1));
-
- CHECKBOX_Check( WM_GetDialogItem(hWin, GUI_ID_CHECK0));
- CHECKBOX_Check( WM_GetDialogItem(hWin, GUI_ID_CHECK1));
-
- SLIDER_SetWidth( WM_GetDialogItem(hWin, GUI_ID_SLIDER0), 5);
- SLIDER_SetValue( WM_GetDialogItem(hWin, GUI_ID_SLIDER1), 50);
- break;
-
- case WM_KEY:
- switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key)
- {
- case GUI_ID_ESCAPE:
- GUI_EndDialog(hWin, 1);
- break;
-
- case GUI_ID_ENTER:
- GUI_EndDialog(hWin, 0);
- break;
- }
- break;
-
- case WM_NOTIFY_PARENT:
- Id = WM_GetId(pMsg->hWinSrc); /* Id of widget */
- NCode = pMsg->Data.v; /* Notification code */
- switch (NCode)
- {
- /* React only if released */
- case WM_NOTIFICATION_RELEASED:
- if (Id == GUI_ID_OK)
- {
- /* OK Button */
- GUI_EndDialog(hWin, 0);
- }
- if (Id == GUI_ID_CANCEL)
- {
- /* Cancel Button */
- GUI_EndDialog(hWin, 1);
- }
- break;
-
- /* Selection changed */
- case WM_NOTIFICATION_SEL_CHANGED:
- FRAMEWIN_SetText(hWin, "Dialog - sel changed");
- break;
-
- default:
- FRAMEWIN_SetText(hWin, "Dialog - notification received");
- }
- break;
-
- default:
- WM_DefaultProc(pMsg);
- }
- }
复制代码 |