ICZelion TUT10

zhaozj2021-02-11  207

Tutorial 10: Dialog Box As Main Window

Now Comes The Really Interesting Part About Gui, The Dialog Box. In this Tutorial (and the next), We Well Learn How To Use a Dialog Box As Our Main Window.

Download The First Example Here, The Second Example HERE.

Theory: If you play with the examples in the previous tutorial long enough, you 'll find out that you can not change input focus from one child window control to another with Tab key The only way you can do that is by clicking the control you. want it to gain input focus. This situation is rather cumbersome. Another thing you might notice is that I changed the background color of the parent window to gray instead of normal white as in previous examples. This is done so that the color of the child window controls can blend seamlessly with the color of the client area of ​​the parent window. There is a way to get around this problem but it's not easy. You have to subclass all child window controls in your parent window.

The reason why such inconvenience exists is that child window controls are originally designed to work with a dialog box, not a normal window. The default color of child window controls such as a button is gray because the client area of ​​a dialog box is normally gray So The Blend Into Each Other WITHOUT Any Sweat on The Programmer's Part.

Before we get deep into the detail, we should know first what a dialog box is. A dialog box is nothing more than a normal window which is designed to work with child window controls. Windows also provides internal "dialog box manager" which is responsible for most of the keyboard logic such as shifting input focus when the user presses Tab, pressing the default pushbutton if Enter key is pressed, etc so programmers can deal with higher level tasks. Dialog boxes are primarily used as input / output devices. As such a dialog box can be considered as an input / output "black box" meaning that you do not have to know how a dialog box works internally in order to be able to use it, you only have to know how to interact with it. that's a principle of object oriented programming (OOP) called information hiding. If the black box is * perfectly * designed, the user can make use of it without any knowledge on how it operates. The catch is that the black box must be perfect, That's hard to achieve in th e real world. Win32 API is also designed as a black box too.Well, it seems we stray from our path. Let's get back to our subject. Dialog boxes are designed to reduce workload of a programmer. Normally if you put child window controls on a normal window, you have to subclass them and write keyboard logic yourself. But if you put them on a dialog box, it will handle the logic for you. you only have to know how to get the user input from the dialog box or How to send commands to it.

A dialog box is defined as a resource much the same way as a menu. You write a dialog box template describing the characteristics of the dialog box and its controls and then compile the resource script with a resource editor.

Note that all resources are put together in the same resource script file. You can use any text editor to write a dialog box template but I do not recommend it. You should use a resource editor to do the job visually since arranging child window controls on a dialog box is hard to do manually. Several excellent resource editors are available. Most of the major compiler suites include their own resource editors. You can use them to create a resource script for your program and then cut out irrelevant lines such as those related to MFC.There are two main types of dialog box:. modal and modeless A modeless dialog box lets you change input focus to other window The example is the Find dialog of MS Word There are two subtypes of modal dialog box:.. application MODAL AND System Modal. An Application Modal Dialog Box Doesn't Let You Change Input Focus To Other Window In The Same Application But You Can Change The Input Focus to The Window of

Other Application. A System Modal Dialog Box Doesn't Allow You To Change Input Focus To Any Other Window Until You Respond To It First.

A modeless dialog box is created by calling CreateDialogParam API function. A modal dialog box is created by calling DialogBoxParam. The only distinction between an application modal dialog box and a system modal one is the DS_SYSMODAL style. If you include DS_SYSMODAL style in a dialog box Template, That Dialog Box Will Be a System Modal ONE.

You Can Communicate with any child window by using senddlgitemmessage function. Its syntax is like this:

Senddlgitemmessage Proto Hwnddlg: DWORD, /

IDControl: DWORD, /

UMSG: DWORD, /

WPARAM: DWORD, /

LParam: DWORD

This API call is immensely useful for interacting with a child window control For example, if you want to get the text from an edit control, you can do this:. Call SendDlgItemMessage, hDlg, ID_EDITBOX, WM_GETTEXT, 256, ADDR text_buffer

.

Windows also provides several control-specific API functions to get and set data quickly, for example, GetDlgItemText, CheckDlgButton etc. These control-specific functions are provided for programmer's convenience so he does not have to look up the meanings of wParam and lParam for each message. Normally, you should use control-specific API calls when they're available since they make source code maintenance easier. Resort to SendDlgItemMessage only if no control-specific API calls are available.

The Windows Dialog Box Manager Sends Some Messages To a Specialized Callback Function Called A Dialog Box Procedure Which Has The Following Format:

DLGPROC Proto HDLG: DWORD, /

IMSG: DWORD, /

WPARAM: DWORD, /

LParam: DWORD

THE DIALOG BOX Procedure Is Very Similar To A Window Procedure Except for the Type of Return Value Which IS True / False Instead of LRESULT. The Inside Windows

. IS the true window procedure for the dialog box It calls our dialog box procedure with some messages that it received So the general rule of thumb is that:. If our dialog box procedure processes a message, it

MUST return TRUE in eax and if it does not process the message, it must return FALSE in eax. Note that a dialog box procedure does not pass the messages it does not process to the DefWindowProc call since it's not a real window procedure.

There are two distinct uses of a dialog box. You can use it as the main window of your application or use it as an input device. We 'll examine the first approach in this tutorial. "Using a dialog box as main window" can BE Interpreted in Two DiffERENT SENSES.

You can use the dialog box template as a class template which you register with RegisterClassEx call In this case, the dialog box behaves like a "normal" window:. It receives messages via a window procedure referred to by lpfnWndProc member of the window class, not via a dialog box procedure. The benefit of this approach is that you do not have to create child window controls yourself, Windows creates them for you when the dialog box is created. Also Windows handles the keyboard logic for you such as Tab order etc. Plus you can specify the cursor and icon of your window in the window class structure. your program just creates the dialog box without creating any parent window. This approach makes a message loop unnecessary since the messages are sent directly to the dialog box procedure . You don't even have to register a window class! This tutorial is going to be a long one. I'll present the first approach backed by the second.

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 / WANEL32.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) IN Voke isdialogmessage, hdlg, addr msg .if eax == false Invoke TranslateMessage, Addr Msg Invoke DispatchMessage, Addr Msg .ndif.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 EA X, EAX RET WNDPROC ENDP End StartDialog.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 ENDMyMenu MENU BEGIN POPUP "Test Controls" BEGIN MENUITEM "Get Text", IDM_GETTEXT MENUITEM " Clear text ", idm_clear meneuitem", 0x0800 / * mft_separator * / menuitem "e & xit", IDM_EXIT End end

Analysis: Let's analyze this first example.

This Example Shows How To Register A Dialog Template AS WINDOW Class and Create A "Window" from That Class. It Simplifies your program Since You don't have to create the child window control.

Let's first analyze the dialog template.

MYDIALOG DIALOG 10, 10, 205, 60

Declare the name of a dialog, in this case, "MyDialog" followed by the keyword "DIALOG" The following four numbers are:. X, y, width, and height of the dialog box in dialog box units (not the same as pixels ).

Style 0x0004 | DS_Center | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK

Declare the styles of the dialog box.

Caption "Our First Dialog Box"

This is the text this will....................

Class "DLGCLASS"

. This line is crucial It's this CLASS keyword that allows us to use the dialog box template as a window class Following the keyword is the name of the "window class" 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 Block Defines The Child Window Controls in The Dialog Box. They're Defined Between Begin and End Keywords. Generally The Syntax Is as Follows:

Control-Type "Text", ControlID, X, Y, Width, Height [, Styles]

Control-Types Are Resource Compiler's Constants So you have to consult the manual.

Now We go to the assembly source code. The intencement part is in the window Class Structure:

Mov wc.cbwndextra, DLGWindowextra

MOV wc.lpszclassname, Offset ClassName

Normally, this Member Is Left Null, Butiff Template As A Dialog Box Template As A Window Class, We Must Set this Member To The Value

Dlgwindowextra. Note That the name of the class must be identical to the one following the one's FOLLOWING THE

CLASS keyword in the dialog box template. The remaining members are initialized as usual. After you fill the window class structure, register it with RegisterClassEx. Seems familiar? This is the same routine you have to do in order to register a normal window class.

Invoke CreateDialogparam, Hinstance, Addr Dlgname, Null, Null, NULL

After registering the "window class", we create our dialog box In this example, I create it as a modeless dialog box with CreateDialogParam function This function takes 5 parameters but you only have to fill in the first two:.. The instance handle and the pointer to the name of the dialog box template. Note that the 2nd parameter is not a pointer to the class name.At this point, the dialog box and its child window controls are created by Windows. Your window procedure will receive WM_CREATE message as UsuAL.

Invoke getdlgitem, hdlg, idc_edit

Invoke setfocus, EAX

After the dialog box is created, I want to set the input focus to the edit control. If I put these codes in WM_CREATE section, GetDlgItem call will fail since at that time, the child window controls are not created yet. The only way you can do this is to call it after the dialog box and all its child window controls are created. So I put these two lines after the UpdateWindow call. GetDlgItem function gets the control ID and returns the associated control's window handle. This is how you can Get A Window Handle if you know its control.

Invoke isdialogmessage, HDLG, Addr Msg .if Eax == false Invoke TranslateMessage, Addr Msg Invoke DispatchMessage, Addr Msg .endif

The program enters the message loop and before we translate and dispatch messages, we call IsDialogMessage function to let the dialog box manager handles the keyboard logic of our dialog box for us. If this function returns TRUE, it means the message is intended for the dialog box and is processed by the dialog box manager. Note another difference from the previous tutorial. When the window procedure wants to get the text from the edit control, it calls GetDlgItemText function instead of GetWindowText. GetDlgItemText accepts a control ID instead of a window handle . That makes the call easier in the case you use a dialog box.Now let's go to the second approach to using a dialog box as a main window. in the next example, I 'll create an application modal dialog box. you'll NOT FIND A Message Loop OR A Window Procedure Because They're Not Necessary!

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 incrudelib /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 .Ndif.ndif .else Mov Eax, False Ret .ndif Mov Eax, True Ret DLGPROC ENDP End StartDialog.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 endidr_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

The Analysis Follows:

DLGPROC Proto: DWORD,: DWORD,: DWORD,: DWORD

WE DECLARE The Function Prototype for DLGPROC SOW We can Refer to it with addr Operator in the line break:

Invoke Dialogboxparam, Hinstance, Addr Dlgname, Null, Addr DLGPROC, NULL

The above line calls DialogBoxParam function which takes 5 parameters:. The instance handle, the name of the dialog box template, the parent window handle, the address of the dialog box procedure, and the dialog-specific data DialogBoxParam creates a modal dialog box. IT WILL NOT RETURN Until The Dialog Box is destroyed.

.IF uMSG == WM_INITDIALOG INVOKE GETDLGITEM, HWND, IDC_EDIT INVOKE SETFOCUS, EAX .ELSEIF UMSG == WM_Close Invoke SendMsSage, HWnd, WM_COMMAND, IDM_EXIT, 0

转载请注明原文地址:https://www.9cbs.com/read-4683.html

New Post(0)