Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 10. The dialog box as the main interface

WIN32 compilation: 10. The dialog box as the main interface

Print View , by: iSee ,Total views: 27 ,Word Count: 4299 ,Date: Fri, 8 May 2009 Time: 4:35 PM

Dialog box in the 10th Division as the main application interface


Now we begin to learn some interesting GUI programming, namely: a dialog box as the main application interface. We will be divided into two courses to talk about the process

Theory:

If you have been careful attention to a program before you will find: You can not press TAB key control from a child window control to another sub-window, in order to transfer only if you click with the mouse to click. It is the user unfriendly. Another thing is if you like a lesson in before the main window as the background color from white into gray, sub-window control in order to seamlessly change accordingly, you must fine-classification of all sub-windows. A lot of inconvenience caused by the above-mentioned reason is that child window controls are originally designed for the dialog box as child window controls the background color is gray, and the dialog box background color is gray, so that they would have on each other, and there is no need the treatment of other programmers. We had intensive discussions in the dialog box before the dialog box we need to know what is meant. A dialog box is actually a lot of sub-window of a window controls, WINDOWS in the dialog box there is a internal management procedures to deal with its pressing TAB key as the input focus from a subset of window space to another sub-section window controls, equivalent to pressing the ENTER key with input focus in the current sub-window by clicking on the mouse control of these chores, etc., so that programmers can concentrate on affairs of their logic. The main dialog box for input and output interface, it does not have to know their work within the principle, and as long as know how to interact with them on it. This is also the object-oriented design of the so-called information hiding. As long as the black box in the realization of perfect enough, we can safely use, of course, we must emphasize that the "black box" must be perfect. Within the WIN32 API to achieve that is a "black box." Oh, if we take the title of some of the discussion, let us return to the topic, the dialog box is designed to reduce the workload of programmers generally if you put in its own window controls a subset of the window you have to deal with One of the key logical and detailed classification of the process of its window. If you put it in the dialog box, those chores will be to handle the dialog box, you just know how to get user input data and how to Add data to the sub-window can be put in control. In the process of dialog and menu to be defined as a resource, you can write a script file in the dialog box template, which contains the dialog box and the characteristics of sub-window, and then use the resources editor. It should be noted that all of the resources must be placed on the same script file. Although you can use a text editor to edit the script file, but as the child window controls to adjust the location coordinates to be related to some of the best value or some visual editor, so much more convenient. The general development of the compiler package editor will take the resources, you can use them to generate a template and then added to and deleted a number of child window controls. There are two main types of dialog box: modal dialog and non-model dialog box. Modeless dialog box allows you to switch the input focus to (the same application) to another window, and there is no need to close the dialog box. For example, MS WORD in the FIND dialog box. Models and two types of dialog box: application modal dialog box and the system dialog box. Applications in this dialog box does not allow you to switch between applications input focus, but you can switch to other applications to, and the system dialog box in the dialog box must be to respond to you can not switch to any application. To create a Modeless Dialog API function calls CreateDialogParam, and the dialog box to create a model of the API function call DialogBoxParam. Model in which applications and system model dialog box is the difference between the style of different parameters, in order to create a system model must be the parameter dialog box "or" on DS_SYSMODAL flag. In the dialog box to control communications and sub-window function is called SendDlgItemMesage. The function syntax is as follows:

SendDlgItemMessage proto hwndDlg: DWORD, \
idControl: DWORD, \
uMsg: DWORD, \
wParam: DWORD, \
lParam: DWORD
Function of the PAI for use in sending to the child window controls are very useful. For example: If you want the string in the edit control can be done:
call SendDlgItemMessage, hDlg, ID_EDITBOX, WM_GETTEXT, 256, ADDR text_buffer
To send specific inquiries about the news should be of the WIN32 API reference manual. WINDOWS also provides quick access to several of the function control data. For example: GetDlgItemText, CheckDlgButton and so on. In this way, you can not query for each message to the wParam and lParam parameters of the relevant information. You should be as much as possible the use of these API functions, which makes your code easier to maintain in future. Dialog management function will be some message sent to a specific callback function: dialog box, the process function, which the format is:
DlgProc proto hDlg: DWORD, \
iMsg: DWORD, \
wParam: DWORD, \
lParam: DWORD
The format of this function is very similar to the process of the window function, in addition to TURE and the return value is FALSE, instead of HRESULT, exists in the WINDOWS Manager dialog box inside the dialog box is the real function of the process window. It will give us some message-passing process of the window function. So when we deal with the window function process the information on the return of TTRUE, otherwise eax return on FALSE. This also means that the course of our windows do not function in the processing of the received message does not call the DefWindowProc function, because it is not a real function of the process window. The dialog box has two uses: one is as a main window to use is as an input and output devices. This lesson we will use the model first. "The dialog window used to make decisions," There are two means: 1. RegisterClassEx function you can call the dialog box template as a window class registration. This behavior of the dialog box similar to an ordinary window: it through the registration window in the window designated to handle all the process information, through such methods to the advantage of using the dialog box shows that you do not need to create sub-window control, WINDOWS itself will help you create a good addition, we will help you handle all the logic of the button, and you can also specify the window class structure of your cursor and icon; 2. Your application to create the dialog window has no parent window, this method, there is no need to take some of the code dealing with the news cycle, because all the news directly to the process function dialog box, so that you can not register a window category. My first lesson goalkeeper and then use the first method of using the second method.

Examples:


dialog.asm


.386
. model flat, stdcall
option casemap: none
WinMain proto: DWORD,: DWORD,: DWORD,: DWORD
include \ masm32 \ include \ windows.inc
include \ masm32 \ include \ user32.inc
include \ masm32 \ include \ kernel32.inc
includelib \ masm32 \ lib \ user32.lib
includelib \ masm32 \ lib \ kernel32.lib

. data
ClassName db "DLGCLASS", 0
MenuName db "MyMenu", 0
DlgName db "MyDialog", 0
AppName db "Our First Dialog Box", 0
TestString db "Wow! I''''m in an edit box now", 0

. data?
hInstance HINSTANCE?
CommandLine LPSTR?
buffer db 512 dup (?)

. const
IDC_EDIT equ 3000
IDC_BUTTON equ 3001
IDC_EXIT equ 3002
IDM_GETTEXT equ 32000
IDM_CLEAR equ 32001
IDM_EXIT equ 32002

. code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke GetCommandLine
mov CommandLine, eax
invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
invoke ExitProcess, eax

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD
LOCAL wc: WNDCLASSEX
LOCAL msg: MSG
LOCAL hDlg: HWND
mov wc.cbSize, SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, DLGWINDOWEXTRA
push hInst
pop wc.hInstance
mov wc.hbrBackground, COLOR_BTNFACE +1
mov wc.lpszMenuName, OFFSET MenuName
mov wc.lpszClassName, OFFSET ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov wc.hCursor, eax
invoke RegisterClassEx, addr wc
invoke CreateDialogParam, hInstance, ADDR DlgName, NULL, NULL, NULL
mov hDlg, eax
invoke ShowWindow, hDlg, SW_SHOWNORMAL
invoke UpdateWindow, hDlg
invoke GetDlgItem, hDlg, IDC_EDIT
invoke SetFocus, eax
. WHILE TRUE
invoke GetMessage, ADDR msg, NULL, 0,0
. BREAK. IF (! Eax)
invoke IsDialogMessage, hDlg, ADDR msg
. IF eax == FALSE
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
. ENDIF
. ENDW
mov eax, msg.wParam
ret
WinMain endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM
. IF uMsg == WM_DESTROY
invoke PostQuitMessage, NULL
. ELSEIF uMsg == WM_COMMAND
mov eax, wParam
. IF lParam == 0
. IF ax == IDM_GETTEXT
invoke GetDlgItemText, hWnd, IDC_EDIT, ADDR buffer, 512
invoke MessageBox, NULL, ADDR buffer, ADDR AppName, MB_OK
. ELSEIF ax == IDM_CLEAR
invoke SetDlgItemText, hWnd, IDC_EDIT, NULL
. ELSE
invoke DestroyWindow, hWnd
. ENDIF
. ELSE
mov edx, wParam
shr edx, 16
. IF dx == BN_CLICKED
. IF ax == IDC_BUTTON
invoke SetDlgItemText, hWnd, IDC_EDIT, ADDR TestString
. ELSEIF ax == IDC_EXIT
invoke SendMessage, hWnd, WM_COMMAND, IDM_EXIT, 0
. ENDIF
. ENDIF
. ENDIF
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start


Dialog.rc


# include "resource.h"

# define IDC_EDIT 3000
# define IDC_BUTTON 3001
# define IDC_EXIT 3002

# define IDM_GETTEXT 32000
# define IDM_CLEAR 32001
# define IDM_EXIT 32003

MyDialog DIALOG 10, 10, 205, 60
STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "Our First Dialog Box"
CLASS "DLGCLASS"
BEGIN
EDITTEXT IDC_EDIT, 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT
DEFPUSHBUTTON "Say Hello", IDC_BUTTON, 141,10,52,13
PUSHBUTTON "E & xit", IDC_EXIT, 141,26,52,13, WS_GROUP
END

MyMenu MENU
BEGIN
POPUP "Test Controls"
BEGIN
MENUITEM "Get Text", IDM_GETTEXT
MENUITEM "Clear Text", IDM_CLEAR
MENUITEM "",, 0x0800 / * MFT_SEPARATOR * /
MENUITEM "E & xit", IDM_EXIT
END
END

Analysis:

We first analyze the first example:
The example shows how to register a dialog template as a window class, and then create a window class derived from the window. Because there is no need for you to create their own sub-window control, so to simplify the work of many.
Us to analyze the dialog box template.

MyDialog DIALOG 10, 10, 205, 60

First, the name of the dialog box, and then the keyword "DAILOG". The next four figures, the first two are the coordinates of the dialog box, dialog box, the latter two is wide and high (Note: their units are the units dialog box, and not necessarily pixel).

STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK

Above definition of the dialog box style.

CAPTION "Our First Dialog Box"

This is the title displayed in the dialog box title bar.

CLASS "DLGCLASS"

This line is critical. It is precisely because of the keyword CLASS, we can use it to the statement of a dialog box to use as a window. With the back of the keyword is "window class" name.

BEGIN
EDITTEXT IDC_EDIT, 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT
DEFPUSHBUTTON "Say Hello", IDC_BUTTON, 141,10,52,13
PUSHBUTTON "E & xit", IDC_EXIT, 141,26,52,13
END

The above definition of a subset of the dialog window controls, they are a statement in one of two keywords between BEGIN and END.

    control-type "text", controlID, x, y, width, height [, styles]
The type of control is the definition of a good resource editor of the constant, you can find information about the manual.
Now we take a look at the source code compilation. Look at this part:
    mov wc.cbWndExtra, DLGWINDOWEXTRA
    mov wc.lpszClassName, OFFSET ClassName
CbWndExtra normally be set to NULL, but we want to register a dialog template as a window class, we must set the value of the member into DLGWINDOWEXTRA. Attention to class and template name must be with the CLASS keyword in the back of the same name. The remaining members of the general statement of variables and the same window class. Fill out the window class structure RegisterClassEx variable function call to register. All this seemed an ordinary and registered window class is the same.
    invoke CreateDialogParam, hInstance, ADDR DlgName, NULL, NULL, NULL
After registration, we will create the dialog box. In this example, we call a function CreateDialogParam Modeless Dialog. The function of a total of five parameters, of which the first two parameters are necessary: example of the dialog box template to handle and the name of the pointer. Note the second parameter is the point to the template name instead of the name of the pointer type. At this time, WINDOWS will have a dialog and sub-control window. At the same time, your application will receive the first transmission from a WINDOWS message WM_CREATE.
    invoke GetDlgItem, hDlg, IDC_EDIT
    invoke SetFocus, eax
In the dialog box after the focus of our input and output based on the edit control. If the paragraph in WM_CREATE message processing code if the focus settings, GetDlgItem function will fail, because at this time window has not yet generated space, in order to dialog and all controls are a subset of the window after the call the function we made it to the function arrangements UpdatWindow after, GetDlgItem function of the exposure to return to the control handle.

invoke IsDialogMessage, hDlg, ADDR msg
. IF eax == FALSE
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
. ENDIF

Procedures to enter the news cycle now, in our translation and distribution of news before the function built-in dialog box dialog box allows the management of procedures to deal with the keyboard logic Jump. If the function returns TRUE, that message is passed to the dialog box has been handled by the function. Attention and different from the previous lesson, when we want to control when text messaging instead of calling GetWindowText function GetDlgItemText function, the former to accept a control parameter is the ID number, not a window handle, which makes the dialog box calls the function more convenient.



Good use of the second approach we are now a main window as a dialog box to use. In the next example, we will have a model of an application dialog box, you will find there is no news cycle or the process window, because they did not need to!
dialog.asm (part 2)


.386
. model flat, stdcall
option casemap: none

DlgProc proto: DWORD,: DWORD,: DWORD,: DWORD

include \ masm32 \ include \ windows.inc
include \ masm32 \ include \ user32.inc
include \ masm32 \ include \ kernel32.inc
includelib \ masm32 \ lib \ user32.lib
includelib \ masm32 \ lib \ kernel32.lib

. data
DlgName db "MyDialog", 0
AppName db "Our Second Dialog Box", 0
TestString db "Wow! I''''m in an edit box now", 0

. data?
hInstance HINSTANCE?
CommandLine LPSTR?
buffer db 512 dup (?)

. const
IDC_EDIT equ 3000
IDC_BUTTON equ 3001
IDC_EXIT equ 3002
IDM_GETTEXT equ 32000
IDM_CLEAR equ 32001
IDM_EXIT equ 32002

. code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke DialogBoxParam, hInstance, ADDR DlgName, NULL, addr DlgProc, NULL
invoke ExitProcess, eax

DlgProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM
. IF uMsg == WM_INITDIALOG
invoke GetDlgItem, hWnd, IDC_EDIT
invoke SetFocus, eax
. ELSEIF uMsg == WM_CLOSE
invoke SendMessage, hWnd, WM_COMMAND, IDM_EXIT, 0
. ELSEIF uMsg == WM_COMMAND
mov eax, wParam
. IF lParam == 0
. IF ax == IDM_GETTEXT
invoke GetDlgItemText, hWnd, IDC_EDIT, ADDR buffer, 512
invoke MessageBox, NULL, ADDR buffer, ADDR AppName, MB_OK
. ELSEIF ax == IDM_CLEAR
invoke SetDlgItemText, hWnd, IDC_EDIT, NULL
. ELSEIF ax == IDM_EXIT
invoke EndDialog, hWnd, NULL
. ENDIF
. ELSE
mov edx, wParam
shr edx, 16
. if dx == BN_CLICKED
. IF ax == IDC_BUTTON
invoke SetDlgItemText, hWnd, IDC_EDIT, ADDR TestString
. ELSEIF ax == IDC_EXIT
invoke SendMessage, hWnd, WM_COMMAND, IDM_EXIT, 0
. ENDIF
. ENDIF
. ENDIF
. ELSE
mov eax, FALSE
ret
. ENDIF
mov eax, TRUE
ret
DlgProc endp
end start


dialog.rc (part 2)


# include "resource.h"

# define IDC_EDIT 3000
# define IDC_BUTTON 3001
# define IDC_EXIT 3002

# define IDR_MENU1 3003

# define IDM_GETTEXT 32000
# define IDM_CLEAR 32001
# define IDM_EXIT 32003

MyDialog DIALOG 10, 10, 205, 60
STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "Our Second Dialog Box"
MENU IDR_MENU1
BEGIN
EDITTEXT IDC_EDIT, 15,17,111,13, ES_AUTOHSCROLL | ES_LEFT
DEFPUSHBUTTON "Say Hello", IDC_BUTTON, 141,10,52,13
PUSHBUTTON "E & xit", IDC_EXIT, 141,26,52,13
END

IDR_MENU1 MENU
BEGIN
POPUP "Test Controls"
BEGIN
MENUITEM "Get Text", IDM_GETTEXT
MENUITEM "Clear Text", IDM_CLEAR
MENUITEM "",, 0x0800 / * MFT_SEPARATOR * /
MENUITEM "E & xit", IDM_EXIT
END
END



As follows:

DlgProc proto: DWORD,: DWORD,: DWORD,: DWORD

We have defined the function prototype DlgProc, so ADDR operator can be used to obtain its address (remember, it can dynamically at run time to obtain a valid address identifier):

invoke DialogBoxParam, hInstance, ADDR DlgName, NULL, addr DlgProc, NULL

A few lines above the function call DialogBoxPAram, this function has five parameters are: instance handle, the name of the dialog box template, the parent window handle, function address the process of dialog, and dialog-related data. The model generates a dialog box function. If you do not show to close the function will not return.

. IF uMsg == WM_INITDIALOG
invoke GetDlgItem, hWnd, IDC_EDIT
invoke SetFocus, eax
. ELSEIF uMsg == WM_CLOSE
invoke SendMessage, hWnd, WM_COMMAND, IDM_EXIT, 0

In addition to not deal with outside the WM_CREATE message processing dialog window function and the general process is similar to the window. Function of the process to receive the first news is that WM_INITDIALOG. The initialization code is usually on here. Note If you must deal with the news eax return TRUE. Management function built-in dialog box will not WM_DESTROY message is sent to the dialog message handling functions, so if we want to deal with the dialog box closed, put it on WM_CLOSE messages processed. In our example, we send a message WM_COMMAND, and placed in the wParam parameter IDM_EXIT, this WM_CLOSE message and deal with the same effect, in dealing with IDM_EXIT function, we call EndDialog. If we want to destroy a dialog box, we must call EndDialog function, which does not immediately destroy a window, but to set up a flag, and then dialog manager will deal with the next action to destroy the dialog box. Well, now we take a look at the resource file, the most significant change is the menu at the specified string is we did not specify the name of the menu instead of a constant IDR_MENU1. DialogBoxParam calls generated in the dialog box must be articulated a menu to do so, pay attention to the template in the dialog box, in which the identifier must MENU keyword before, these two examples are significant differences between the latter there is no icon This can be in dealing with the news WM_INITDIALOG send WM_SETICON news, and then in the message handling code can make appropriate arrangements.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.