Dialogs

Dialogs are the most complicated resource elements. In contrast to the resources that you considered previously, no identifier is specified for a dialog. The dialog is accessed by its name (a string).

 #define WS_SYSMENU       0x00080000L     #define WS_MINIMIZEBOX   0x00020000L     #define WS_MAXIMIZEBOX   0x000l0000L     DIAL1 DIALOG 0, 0, 240, 120     STYLE WS_SYSMENU  WS_MINIMIZEBOXWS_MAXIMIZEBOX     CAPTION "Example of a dialog"     FONT 8, "Arial"     {     } 

As you can see, the dialog definition starts with the line containing the DIALOG keyword. The same line specifies the size and position of the dialog. Further lines specify other properties of the dialog. Then, there are braces. In this case, these braces do not enclose anything. This means that the window doesn't contain any controls. The types of dialog and other elements are defined by the constants placed at the start of the file. These constants are standard and, according to the C/C++ rules, are stored in the RESOURCE.H file. As before, I'll define all constants directly in the resource file. I'd like you to note that the constants are defined according to the C language notation.

Before I explain the example provided in Listing 9.2, consider the specific features of working with dialogs. A dialog is similar to a normal window. Like any normal window, it has the window procedure. The dialog procedure has the same parameters as a normal window procedure. However, significantly fewer messages arrive to the dialog procedure. At the same time, the messages accepted by a dialog are essentially the same as messages of a normal window. The only difference is that instead of the WM_CREATE message, the WM_INITDIALOG message arrives. The dialog procedure can return either zero or nonzero values. A nonzero value must be returned when the dialog procedure handles the message delivered to it; if it delegates message processing to the system, it must return zero.

The differences in the behavior of a dialog and that of a normal window can be easily explained. If you create a normal window, all properties of that window are defined by the following factors: class properties defined when creating a window and procedure reaction to specific messages. When creating a dialog, all its properties are specified in resources. Some of these properties are set when the CreateWindow function is implicitly called during a call to the function that creates a dialog ( DialogBox , DialogBoxParam , etc.). Other properties depend on the behavior of the internal function generated by the system when creating the dialog. If something happens to the dialog, the message first arrives to the internal procedure, and then the dialog procedure that you have created in the program is called. If the procedure returns 0, then the internal procedure continues processing this message; otherwise , the internal procedure doesn't handle the message. Thus, since I have briefly covered the mechanisms regulating the dialog box operation, consider the program presented in Listing 9.2. A brief explanation will be provided after the listing.

Listing 9.2: The use of simple resources
image from book
 // The DIAL.RC file // Constant definitions #define WS_SYSMENU     0x00080000L #define WS_MINIMIZEBOX 0x00020000L #define WS_MAXIMIZEBOX 0x000l0000L // Identifiers #define STR1 1 #define STR2 2 #define IDI_ICON1 3 // Defining an icon IDI_ICON1 ICON "ico1.ico" // Defining a dialog DIAL1 DIALOG 0, 0, 240, 120 STYLE WS_SYSMENU  WS_MINIMIZEBOX  WS_MAXIMIZEBOX CAPTION "Example of a dialog" FONT 8, "Arial" { } // Strings definitions STRINGTABLE {     STR1, "Message"     STR2, "Program version 1.00" } ; The DIAL.INC file ; Constants ; The message arrives when the window is closed WM_CLOSE      equ 10h WM_INITDIALOG equ 110h WM_SETICON    equ 80h ; Prototypes of external procedures EXTERN      MessageBoxA@16:NEAR EXTERN      ExitProcess@4:NEAR EXTERN      GetModuleHandleA@ 4:NEAR EXTERN      DialogBoxParamA@20:NEAR EXTERN      EndDialog@8:NEAR EXTERN      LoadStringA@16:NEAR EXTERN      LoadIconA@8:NEAR EXTERN      SendMessageA@16:NEAR ; Structures ; Message structure MSGSTRUCT STRUC         MSHWNID      DD ?         MSMESSAGE   DD ?         MSWPARAM    DD ?         MSLPARAM    DD ?         MSTIME      DD ?         MSPT        DD ? MSGSTRUCT ENDS ; The DIAL.ASM file .586P ; Flat memory model .MODEL FLAT, stdcall include dial.inc ; Includelib directives for the linker to link libraries includelib c:\masm32\lib\user32.lib includelib c:\masm32\lib\kernel32.lib ;------------------------ ; Data segment _DATA SEGMENT         MSG      MSGSTRUCT <?>         HINST    DD 0 ; Application descriptor         PA       DB "DIAL1", 0         BUF1     DB 40 dup (0)         BUF2     DB 40 dup (0) _DATA ENDS ; Code segment TEXT SEGMENT START: ; Get the application handle     PUSH  0     CALL  GetModuleHandleA@4     MOV   [HINST], EAX ;-------------------------------- ; Load the string     PUSH  40     PUSH  OFFSET BUF1     PUSH  1     PUSH  [HINST]     CALL  LoadStringA@16 ; Load the string     PUSH  40     PUSH  OFFSET BUF2     PUSH  2     PUSH  [HINST] ;--------------------     CALL  LoadStringA@16     PUSH  0 ; MB_OK     PUSH  OFFSET BUF1     PUSH  OFFSET BUF2     PUSH  0     CALL  MessageBoxA@16 ; Create the dialog     PUSH  0     PUSH  OFFSET WNDPROC ; Window procedure     PUSH  0     PUSH  OFFSET PA ; Resource name (DIAL1)     PUSH  [HINST]     CALL  DialogBoxParamA@20     CMP   EAX, -1     JNE   KOL KOL: ;---------------------------------     PUSH  0     CALL  ExitProcess@4 ;--------------------------------- ; Dialog procedure ; Position of parameters in the stack ; [EBP+014H] ; LPARAM ; [EBP+10H] ;  WAPARAM ; [EBP+0CH] ;  MES ; [EBP+8] ;    HWND WNDPROC PROC     PUSH  EBP     MOV   EBP, ESP     PUSH  EBX     PUSH  ESI     PUSH  EDI ;---------------------------------     CMP   DWORD PTR [EBP+0CH], WM_CLOSE     JNE   L1     PUSH  0     PUSH  DWORD PTR [EBP+08H]     CALL  EndDialog@8     JMP   FINISH L1:     CMP   DWORD PTR [EBP+0CH], WM_INITDIALOG     JNE   FINISH ; Load the icon     PUSH  3 ; Icon identifier     PUSH  '[HINST] ; Process identifier     CALL  LoadIconA@8 ; Set the icon     PUSH  EAX     PUSH  0 ; Icon type (small)     PUSH  WM_SETICON     PUSH  DWORD PTR [EBP+08H]     CALL  SendMessageA@16 FINISH:     POP   EDI     POP   ESI     POP   EBX     POP   EBP     MOV   EAX, 0     RET   16 WNDPROC ENDP _TEXT ENDS END START 
image from book
 

Now consider how this program works.

  • The resource file structure must be clear to you because earlier you considered all resources used there in detail. I'd only note that the resource file contains several elements. All resources, except the dialog box, must have identifiers For the dialog, this role is played by its name, which in this case appears as DIAL1 .

  • Before opening the dialog, the program demonstrates how to work with such resources as strings. As you can see, everything is straightforward here. First, it is necessary to load the string into the buffer using the Loadstring function; after accomplishing this, it is possible to work with it as with any normal string.

  • The dialog call is self-evident, so I'll proceed with a description of the dialog procedure. Start with the WM_INITDIALOG message. This message, similar to the WM_CREATE message for a standard window, arrives only once: when the window is created. This is convenient for carrying out certain operations at the start of program execution (e.g., initialization). You'll use this possibility to define the dialog icon. First, it is necessary to load the icon; then, send the WM_SETICON message, which sets the icon for this window. The second message that you must process is WM_CLOSE . This message arrives when a user clicks the "x" button in the top right corner of the window. Having received this message, the program executes the EndDialog function, which removes the dialog from the memory, exits the DialogBoxParamA function, and finally closes the program.

As mentioned earlier, the dialog procedure must return a nonzero value if it handles the current message. As shown in this example, principally, this isn't always necessary. From now on, I'll concentrate your attention on those cases, in which this is required.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net