The Assembly Programming Master Book

The second example relates to the Edit field. The program is shown in Listing 3.2, and the result of its operation is shown in Fig. 3.1. When the Exit button is clicked, the program displays the message box with the edited string.

Figure 3.1: Running the program with the edit field (see Listing 3.2)

Notice how the message is sent to the window control. Mainly, two functions are used for this purpose: SendMessage and PostMessage. The difference between these two functions is that the first one calls window procedure with the appropriate parameters and waits until it returns the control; the second function places the message into the queue and returns the control immediately.

Listing 3.2: A window with an edit field

; The edit.inc file ; Constants WM_SETFOCUS equ 7h ; The message that arrives when the window is closed WM_DESTROY equ 2 ; The message that arrives when the window is created WM_CREATE equ 1 ; The message that arrives when something happens ; to the window controls WM_COMMAND equ 111h ; The message that allows you to send the string to the window WM_SETTEXT equ 0Ch ; The message that allows you to receive the string WM_GETTEXT equ 0Dh ; Window properties CS_VREDRAW equ 1h CS_HREDRAW equ 2h CS_GLOBALCLASS equ 4000h WSJ_TABSTOP equ 10000h WS_SYSMENU equ 80000h WS_OVERLAPPEDWINDOW equ 0+WS_TABSTOP+WS_SYSMENU STYLE equ CS_HREDRAW+CS_VREDRAW+CS_GLOBALCLASS CS_HREDRAW equ 2h BS_DEFPUSHBUTTON equ 1h WS_VISIBLE equ 10000000h WS_CHILD equ 40000000h WS_BORDER equ 800000h STYLBTN equ WS_CHILD+BS_DEFPUSHBUTTON+WS_VISIBLE+WS_TABSTOP STYLEDT equ WS_CHILD+WS_VISIBLE+WS_BORDER+WS_TABSTOP ; Standard icon identifier IDI_APPLICATION equ 32512 ; Cursor identifier IDC_ARROW equ 32512 ; Window display mode -- normal SW_SHOWNORMAL equ 1 ; Prototypes of external procedures EXTERN SetFocus@4:NEAR EXTERN SendMessageA@16:NEAR EXTERN MessageBoxA@16:NEAR EXTERN CreateWindowExA@ 48:NEAR EXTERN DefWindowProcA@16:NEAR EXTERN DispatchMessageA@4:NEAR EXTERN ExitProcess@4:NEAR EXTERN GetMessageA@16:NEAR EXTERN GetModuleHandleA@4:NEAR EXTERN LoadCursorA@8:NEAR EXTERN LoadIconA@8:NEAR EXTERN PostQuitMessage@4:NEAR EXTERN RegisterClassA@4:NEAR EXTERN ShowWindow@8:NEAR EXTERN TranslateMessage@4:NEAR EXTERN UpdateWindow@4:NEAR ; Structures ; Message structure MSGSTRUCT STRUC MSHWND DD ? MSMESSAGE DD ? MSWPARAM DD ? MSLPARAM DD ? MSTIME DD ? MSPT DD ? MSGSTRUCT ENDS ;---- Window class structure WNDCLASS STRUC CLSSTYLE DD ? CLWNDPROC DD ? CLSCBCLSEX DD ? CLSCBWNDEX DD ? CLSHINST DD ? CLSHICON DD ? CLSHCURSOR DD ? CLBKGROUND DD ? CLMENNAME DD ? CLNAME DD ? WNDCLASS ENDS ; The edit.asm file .586P ; Flat memory model .MODEL FLAT, stdcall include edit.inc ; Directives for the linker to link libraries includelib c:\masm32\lib\user32.lib includelib c:\masm32\lib\kernel32.lib ;------------------------------------------ ; Data segment _DATA SEGMENT DWORD PUBLIC USE32 'DATA' NEWHWND DD 0 MSG MSGSTRUCT <?> WC WNDCLASS <?> HINST DD 0 ; Application descriptor TITLENAME DB 'Application example - Edit window', 0 CLASSNAME DB 'CLASS32', 0 CPBUT DB 'Exit', 0 ; Exit CPEDT DB ' ', 0 CLSBUTN DB 'BUTTON', 0 CLSEDIT DB 'EDIT', 0 HWNDBTN DWORD 0 HWNDEDT DWORD 0 CAP DB 'Message', 0 MES DB 'Program termination', 0 TEXT DB 'Edit string', 0 DB 50 DUP(0) ; Buffer continuation _DATA ENDS ; Code segment _TEXT SEGMENT DWORD PUBLIC USE32 'CODE' START: ; Get the application descriptor PUSH 0 CALL GetModuleHandleA@4 MOV [HINST], EAX REG_CLASS: ; Fill the window structure ; Style MOV [WC.CLSSTYLE], STYLE ; Message-handling procedure MOV [WC.CLWNDPROC], OFFSET WNDPROC MOV [WC.CLSCBCLSEX], 0 MOV [WC.CLSCBWNDEX], 0 MOV EAX, [HINST] MOV [WC.CLSHINST], EAX ;----------Window icon PUSH IDI_APPLICATION PUSH 0 CALL LoadIconA@8 MOV [WC.CLSHICON], EAX ;---------- Window cursor PUSH IDC_ARROW PUSH 0 CALL LoadCursorA@8 MOV [WC.CLSHCURSOR], EAX ;---------- MOV [WC.CLBKGROUND], 17 ; Window color MOV DWORD PTR [WC.CLMENNAME], 0 MOV DWORD PTR [WC.CLNAME], OFFSET CLASSNAME PUSH OFFSET WC CALL RegisterClassA@4 ; Create a window of the registered class PUSH 0 PUSH [HINST] PUSH 0 PUSH 0 PUSH 150 ; DY -- Window height PUSH 400 ; DX -- Window width PUSH 100 ; Y -- Coordinate of the top left corner PUSH 100 ; X -- Coordinate of the top left corner PUSH WS_OVERLAPPEDWINDOW PUSH OFFSET TITLENAME ; Window title PUSH OFFSET CLASSNAME ; Class name PUSH 0 CALL CreateWindowExA@48 ; Check for error CMP EAX, 0 JZ _ERR MOV [NEWHWND], EAX ; Window 'descriptor ;-------------------------------- PUSH SW_SHOWNORMAL PUSH [NEWHWND] CALL ShowWindow@8 ; Show newly-created window ;-------------------------------- PUSH [NEWHWND] CALL UpdateWindow@4 ; Command to redraw the visible ; part of the window, the WM_PAINT message ; Message-handling loop MSG_LOOP: PUSH 0 PUSH 0 PUSH 0 PUSH OFFSET MSG CALL GetMessageA@16 CMP EAX, 0 JE END_LOOP PUSH OFFSET MSG CALL TranslateMessage@4 PUSH OFFSET MSG CALL DispatchMessageA@4 JMP MSG_LOOP END_LOOP: ; Exit the program (close the process) PUSH [MSG.MSWPARAM] CALL ExitProcess@4 _ERR: JMP END_LOOP ;-------------------------------Window 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_DESTROY JE WMDESTROY CMP DWORD PTR [EBP+0CH], WM_CREATE JE WMCREATE CMP DWORD PTR [EBP+0CH], WM_COMMAND JE WMCOMMND JMP DEFWNDPROC WMCOMMND: MOV EAX, HWNDBTN CMP DWORD PTR [EBP+14H], EAX JNE NODESTROY ; Get the edited string PUSH OFFSET TEXT PUSH 150 PUSH WM_GETTEXT PUSH HWNDEDT CALL SendMessageA@16 ; Display the edited string PUSH 0 PUSH OFFSET CAP PUSH OFFSET TEXT PUSH DWORD PTR [EBP+08H] ; Window descriptor CALL MessageBoxA@16 ; Exit JMP WMDESTROY NODESTROY: MOV EAX, 0 JMP FINISH WMCREATE: ; Create the button window PUSH 0 PUSH [HINST] PUSH 0 PUSH DWORD PTR [EBP+08H] PUSH 20 ; DY PUSH 60 ; DX PUSH 10 ; Y PUSH 10 ; X PUSH STYLBTN PUSH OFFSET CPBUT ; Window name PUSH OFFSET CLSBUTN ; Class name PUSH 0 CALL CreateWindowExA@48 MOV HWNDBTN, EAX ; Save the button descriptors ; Create the edit window PUSH 0 PUSH [HINST] PUSH 0 PUSH DWORD PTR [EBP+08H] PUSH 20 ; DY PUSH 350 ; DX PUSH 50 ; Y PUSH 10 ; X PUSH STYLEDT PUSH OFFSET CPEDT ; Window name PUSH OFFSET CLSEDIT ; Class name PUSH 0 CALL CreateWindowExA@48 MOV HWNDEDT, EAX ;---------Place the string into the edit field PUSH OFFSET TEXT PUSH 0 PUSH WM_SETTEXT PUSH HWNDEDT CALL SendMessageA@16 ;---------Set the focus to the edit window PUSH HWNDEDT CALL SetFocus@4 ;------------------------------------------ MOV EAX, 0 JMP FINISH DEFWNDPROC: PUSH DWORD PTR [EBP+14H] PUSH DWORD PTR [EBP+10H] PUSH DWORD PTR [EBP+0CH] PUSH DWORD PTR [EBP+08H] CALL DefWindowProcA@16 JMP FINISH WMDESTROY: PUSH 0 ; MB_OK PUSH OFFSET CAP PUSH OFFSET MES PUSH DWORD PTR [EBP+08H] ; Window descriptor CALL MessageBoxA@16 PUSH 0 CALL PostQuitMessage@4 ; WM_QUIT message MOV EAX, 0 FINISH: POP EDI POP ESI POP EBX POP EBP RET 16 WNDPROC ENDP _TEXT ENDS END START

 

Категории