In this lesson we'll explore controls, which are the main input of our programs.
Theory:
Windows provides several predefined window classes to facilitate our use. Most of the time we use them in dialog boxes, so we generally call them child window controls. The child window control handles the message itself and notifies the parent window when its state changes. This greatly reduces our programming work, so we should use them as much as we can. In this lesson we put these controls in a window to simplify the program, but most of the time the window controls are in the dialog box. The child window controls shown in our example include: Buttons, drop-down menus, check boxes, radio buttons, edit boxes, and so on. When using a child window control, call CreateWindow or CreateWindowEx first. Because Windows has already registered these child controls, there is no need for us to register again. Of course we can't change their class names. For example, if you want to produce a button, you must specify the class name "button" when you call both of these functions. Other parameters that must be specified are the handle to the parent window and the ID number of the child control that will be produced. The ID number of the child control is used to identify the child control, so it must also be unique. When a child control is generated, a message is sent to the parent window when its state changes. Generally we should produce a word control in the wm_create message of the parent window. The message sent by the child control to the parent window is WM_COMMAND and includes the control's ID number in the base of the passed parameter Wpara, the message number is in the wparam high, and the lparam contains the handle of the child control's window. Each type of control has a different set of message codes, see the WIN32 API Reference manual for details. The parent window can also send a message to a child control by calling the function SendMessage, where the first parameter is the window handle of the child control, the second parameter is the message number to send, and the additional parameters can be passed in wparam and lparam. In fact, as long as you know the handle of a window, you can use the function to send related messages to it. So when a child window is created, the WM_COMMAND message must be processed so that the message of the child control can be received.
Example:
We will produce a window in which there is an edit box and a button. When you press the button, a dialog box pops up showing what you entered in the edit box. In addition, the application has a menu with four items:
Say Hello--Enter a string into the edit control;
Clear edit box-clears the string in the edit control;
Get Text--The pop-up dialog box displays the string in the edit control;
Exit--Exits the application.
.386
. Model Flat,stdcall
Option Casemap:none
WinMain Proto:D Word,:D Word,:D Word,:D Word
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 "Simplewinclass", 0
AppName db "Our", 0
MenuName db "Firstmenu", 0
Buttonclassname db "button", 0
ButtonText db "My A-button", 0
Editclassname db "edit", 0
TestString db "wow! I ' m in a edit box now, 0
. Data?
HINSTANCE hinstance?
CommandLine LPSTR?
Hwndbutton HWND?
Hwndedit HWND?
Buffer db dup (?); Buffer to store the text retrieved from the edit box
. const
ButtonID equ 1; The control ID of the button control
Editid equ 2; The control ID of the edit control
Idm_hello equ 1
Idm_clear equ 2
Idm_gettext equ 3
Idm_exit equ 4
. 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 Hwnd: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,null
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 Createwindowex,ws_ex_clientedge,addr ClassName, \
ADDR AppName, ws_overlappedwindow,\
Cw_usedefault, cw_usedefault,\
300,200,null,null, Hinst,null
MOV hwnd,eax
Invoke ShowWindow, Hwnd,sw_shownormal
Invoke UpdateWindow, HWND
. While TRUE
Invoke GetMessage, ADDR msg,null,0,0
. Break. IF (!EAX)
Invoke TranslateMessage, ADDR msg
Invoke DispatchMessage, ADDR msg
. 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_create
Invoke Createwindowex,ws_ex_clientedge, ADDR editclassname,null,\
Ws_child or ws_visible or Ws_border or Es_left or\
Es_autohscroll,\
50,35,200,25,hwnd,8,hinstance,null
MOV hwndedit,eax
Invoke SetFocus, Hwndedit
Invoke Createwindowex,null, ADDR buttonclassname,addr buttontext,\
Ws_child or ws_visible or bs_defpushbutton,\
75,70,140,25,hwnd,buttonid,hinstance,null
MOV hwndbutton,eax
. ELSEIF Umsg==wm_command
MOV Eax,wparam
. IF lparam==0
. IF Ax==idm_hello
Invoke Setwindowtext,hwndedit,addr teststring
. ELSEIF Ax==idm_clear
Invoke Setwindowtext,hwndedit,null
. ELSEIF Ax==idm_gettext
Invoke Getwindowtext,hwndedit,addr buffer,512
Invoke Messagebox,null,addr buffer,addr APPNAME,MB_OK
. ELSE
Invoke Destroywindow,hwnd
. ENDIF
. ELSE
. IF Ax==buttonid
SHR eax,16
. IF ax==bn_clicked
Invoke sendmessage,hwnd,wm_command,idm_gettext,0
. ENDIF
. ENDIF
. ENDIF
. ELSE
Invoke Defwindowproc,hwnd,umsg,wparam,lparam
Ret
. ENDIF
XOR Eax,eax
Ret
WndProc ENDP
End Start
Analysis:
We are now starting to analyze
.ELSEIF uMsg==WM_CREATE
invoke CreateWindowEx,WS_EX_CLIENTEDGE, \
ADDR EditClassName,NULL,\
WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT\
or ES_AUTOHSCROLL,\
50,35,200,25,hWnd,EditID,hInstance,NULL
mov hwndEdit,eax
invoke SetFocus, hwndEdit
invoke CreateWindowEx,NULL, ADDR ButtonClassName,\
ADDR ButtonText,\
WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,\
75,70,140,25,hWnd,ButtonID,hInstance,NULL
mov hwndButton,eax