STM32 - EMWIN dialog box (12)

EMWI

foreword

Widgets can be created and used independently because they are essentially windows, however, a dialog is often required, which is a window that contains one or more widgets. A dialog is usually a window that, when presented, asks the user to enter information, it may contain multiple widgets that ask the user to provide information based on various choices, or in the form of a message box that provides information only (such as providing information to the user) Caution or Warning) and an OK button.

First, the basic principle of the dialog box

Input focus
The window manager remembers the window or window object that the user finally selected using the touch screen, mouse, keyboard, or otherwise. The window receives the keyboard input message, i.e. has the input focus. The main reason for tracking input focus is to determine where keyboard commands are sent. A window with input focus receives events generated by the keyboard. If you want to move the input focus within a dialog to the next focused dialog item, you can use the GUI_KEY_TAB key. If you want to move backwards, you can use GUI_KEY_BACKTAB.
Blocking and non-blocking dialogs
Dialog windows can be divided into blocking and non-blocking. Blocking dialog blocks the thread of execution. By default, it has input focus, and the user must close it before the thread can continue to execute. . A blocking dialog does not simultaneously disable other dialogs that are displayed, in other words, a blocking dialog is not a modal dialog. If the dialog is blocking, it means that the function used (GUI_ExecDialogBox() or GUI_ExecCreatedDialog()) will return a value only after the dialog is closed. A non-blocking dialog does not block the calling thread - allowing the task to continue running while it is visible. The function returns the value immediately after the dialog is created.
It is important to note that blocking functions should never be called from within a callback function, as doing so may cause the application to malfunction!
Dialog Procedure Functions
A dialog is a window that receives messages like any other window in the system. Most messages are automatically handled by the dialog's window callback program, while other messages are passed to the callback program specified when the dialog box was created, which is called a dialog procedure function.
dialog message
Two additional messages sent to dialog procedure functions are: WM_INIT_DIALOG and WM_NOTIFY_PARENT. Immediately before the dialog is displayed, the WM_INIT_DIALOG message is sent to the dialog procedure function, which is typically used by dialog procedure functions to initialize widgets and perform any other initialization tasks that affect the appearance of the dialog. WM_NOTIFY_PARENT messages are sent to the dialog via the dialog's child windows, notifying the parent window of any events to ensure synchronization. Events sent through a child window depend on its type and are logged separately for each type of gadget.

2. Create a dialog

Creating a dialog requires two basic elements: a resource table and a dialog procedure; the former defines the widgets to be included, and the latter defines the widget's initial value and its behavior. Once you have these two elements, you can create a dialog with a single function call (GUI_CreateDialogBox() or GUI_ExecDialogBox()).

1. Dialog resource table

Dialogs can be created based on blocking (using GUI_ExecDialogBox() ) or non-blocking (using GUI_CreateDialogBox() ). A resource table must be defined first to specify all the gadgets to be included in the dialog box. Generally, when we use the GUIBulider to design the interface and select the dialog box, the resource table will be automatically generated in the final generated C file.
insert image description here
This is what we did yesterday Create a resource table that generates .c files

2. Dialog callback function

As mentioned earlier, dialogs are also windows. Creating a dialog also requires a callback function. The callback function is used to initialize other gadgets in the dialog box and define the behavior of these gadgets. The callback function is as follows:

//对话框回调函数
static void _cbDialog(WM_MESSAGE * pMsg) 
{
    
    
switch (pMsg->MsgId) 
{
    
    
case WM_INIT_DIALOG:
//其他消息… defauyt:
WM_DefaultProc(pMsg);
} }

After the dialog is created, all widgets in the resource table will be visible. Although these gadgets are visible in the screenshot above, they appear as "empty". This is because the dialog procedure function does not yet contain code to initialize the individual elements. The initial values ​​of widgets, the behavior caused by them, and the interactions between them all need to be defined during the dialog.

3. Dialog initialization

insert image description here
Using tools to create will also automatically generate initial values, which can also be initialized with code

static void _cbCallback(WM_MESSAGE * pMsg) 
{
    
    
 WM_HWIN hEdit0;
 WM_HWIN hEdit1;
WM_HWIN hEdit2;
WM_HWIN hEdit3;
WM_HWIN hListBox;
 WM_HWIN hDropd0;
 WM_HWIN hDropd1;
 WM_HWIN hWin= pMsg->hWin;
 switch (pMsg->MsgId) 
{
    
    
 case WM_INIT_DIALOG: //WM_INIT_DIALOG 消息,初始化所有小控件
 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);
 hDropd0 = WM_GetDialogItem(hWin, GUI_ID_DROPDOWN0);
 hDropd1 = WM_GetDialogItem(hWin, GUI_ID_DROPDOWN1);
 //初始化所有小控件
 EDIT_SetText(hEdit0, "EDIT widget 0");
 EDIT_SetText(hEdit1, "EDIT widget 1");
 EDIT_SetTextAlign(hEdit1, GUI_TA_LEFT);
 EDIT_SetHexMode(hEdit2, 0x1234, 0, 0xffffff);
 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);
 SCROLLBAR_CreateAttached(hListBox, SCROLLBAR_CF_VERTICAL);
 DROPDOWN_AddString(hDropd0, "Item 0");
 DROPDOWN_AddString(hDropd0, "Item 1");
 DROPDOWN_AddString(hDropd0, "Item 2");
 DROPDOWN_AddString(hDropd1, "Item 0");
 DROPDOWN_AddString(hDropd1, "Item 1");
 DROPDOWN_AddString(hDropd1, "Item 2");
 DROPDOWN_AddString(hDropd1, "Item 3");
 DROPDOWN_AddString(hDropd1, "Item 4");
 break;
 default:
 WM_DefaultProc(pMsg);
 } 
 }

3. Define the function of the gadget

Once the dialog is initialized, all that remains is to add code to the dialog procedure function, which defines the widget as it is, so that it is fully operational.
insert image description here
This is the dialog box created yesterday, and it has reserved a place for us to fill in

static void _cbCallback(WM_MESSAGE * pMsg) 
{
    
    
 WM_HWIN hEdit0;
 WM_HWIN hEdit1;
 WM_HWIN hEdit2;
 WM_HWIN hEdit3;
 WM_HWIN hListBox;
 WM_HWIN hDropd0;
 WM_HWIN hDropd1;
 WM_HWIN hWin; 
 hWin = pMsg->hWin;
 switch (pMsg->MsgId) 
{
    
    
 case WM_INIT_DIALOG: //WM_INT_DIALOG 消息,用于初始化小工具
 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);
 hDropd0 = WM_GetDialogItem(hWin, GUI_ID_DROPDOWN0);
 hDropd1 = WM_GetDialogItem(hWin, GUI_ID_DROPDOWN1);
 //初始化小工具
 EDIT_SetText(hEdit0, "EDIT widget 0");
 EDIT_SetText(hEdit1, "EDIT widget 1");
 EDIT_SetTextAlign(hEdit1, GUI_TA_LEFT);
 EDIT_SetHexMode(hEdit2, 0x1234, 0, 0xffffff);
 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);
 SCROLLBAR_CreateAttached(hListBox, SCROLLBAR_CF_VERTICAL);
 DROPDOWN_AddString(hDropd0, "Item 0");
 DROPDOWN_AddString(hDropd0, "Item 1");
 DROPDOWN_AddString(hDropd0, "Item 2");
 DROPDOWN_AddString(hDropd1, "Item 0");
 DROPDOWN_AddString(hDropd1, "Item 1");
 DROPDOWN_AddString(hDropd1, "Item 2");
 DROPDOWN_AddString(hDropd1, "Item 3");
 DROPDOWN_AddString(hDropd1, "Item 4");
 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: (1)
 Id= WM_GetId(pMsg->hWinSrc); //获取小控件 ID 号 (2)
 NCode = pMsg->Data.v; //消息类型 (3)
 switch (NCode) 
{
    
    
 case WM_NOTIFICATION_RELEASED: //按键被释放 (4)
 if (Id == GUI_ID_OK) //OK 按键 (5)
{
    
     
 GUI_EndDialog(hWin, 0);
 }
 if (Id == GUI_ID_CANCEL) //CANCEL 按键 (6)
{
    
     
 GUI_EndDialog(hWin, 1);
 }
 break;
 }
 break;
 default:
 WM_DefaultProc(pMsg);
 } }

(1), WM_NOTIFY_PARTEN message, this message will be sent to the parent control when the child control changes.
(2), get the ID number of the widget
(3), get the message type sent by the widget.
(4) The message type is WM_NOTIFICATION_RELEASED, that is, this message will be sent when the button is released.
(5), the control ID is GUI_ID_OK, that is, the OK button is released
(6), the space ID is GUI_ID_CANCEL, that is, the CANCEL button is released.

4. Dialog API function

GUI_CreateDialogBox() 创建非阻塞式对话框。
GUI_ExecCreatedDialog() 执行已创建的对话框。
GUI_ExecDialogBox() 创建并执行对话框。
GUI_EndDialog() 结束对话框。

1. GUI_CreateDialogBox()

insert image description here

2. GUI_ExecCreatedDialog()

insert image description here

3.GUI_ExecDialogBox()

Description
Create and execute dialog
insert image description here

4.GUI_EndDialog()

insert image description here

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/124033038