Win32 assembly tutorial four written a simple window

zhaozj2021-02-08  265

-------------------------------------------------- ------------------------------

Basic knowledge about the window

The window is the rectangular area on the screen. One window can accept the user's input from the keyboard or mouse and display the graphic output inside. An application window typically contains the program's title bar, menu, border, and scroll bar. Among them, the dialog is also a window. Different, the dialog surface typically contains several other windows called "sub-windows". The form of these sub-windings has a press button, a radio button, a checkbox, a text input area, a list box, and a scroll bar. Users see these windows as an object on the screen, can directly interact directly with these objects by pressing a button or scrolling a scroll bar.

The window receives the input of the window in the "message", and the window also uses messages to communicate with other window. For example, when changing the size of the program window, the word processor will reformat the text. The details of the window size change are processed by the operating system, but the program can respond to this system function. When the user changes the size of the window, Windows sends a message to the program indicates the size of the new window. Then, the program can adjust the content in the window to respond to changes in the size. Each window created by the program has a related window process. That is to specify a subroutine (window process) to this window, Windows sends a message to the window by calling it. The window process is processed according to this message and then returns the control to Windows.

The window is created on the basis of "Window". Windows defines the provincial window procedure. If you make Windows yourself in your own, then you can get a standard window, you can also choose the news that you are interested in, this is equivalent to production. Different subclasses also form different applications. Similarly, the sub-window is also based on the same window class and uses the same window process. For example, all buttons in all Windows programs are based on the same window class. This window class has a window process that handles all button messages, but if you design a button according to your own ideas, if you want to change the surface of the button into a bitmap, you can handle the WM_PAINT message of the button window, when Windows needs When you draw button surface, you can paint it with your own meaning.

After the Windows program starts execution, Windows creates a "message queue" for the program. This message queue is used to store messages of various windows that the program may create. There is a code in the program called "message loop", which is used to remove messages from the queue and send them to the corresponding window process. When there is no message, your program is actually transferred in the message loop.

The process of creating a window is as follows:

Emphase example handle (Hinstance)

Registration window class, actually specifies the process of processing messages for your window, defining parameters such as cursor, window style, color

Create a window

Display window

Then enter the message loop, which is constantly detected, and send it to the window process.

Creating a window in different programs is actually almost exactly the same, so you can copy this paragraph when you compose a new program, slightly modify it, most of the code is actually used in the window. In the process, because this is the difference in different programs. The programming points of the window process are as follows:

The parameter UMSG transmitted from the Windows to the window process gets the message type and goes to a different branch processed.

To return to Windows, you must return 0 in EAX when returning to Windows.

I don't have the message that I don't deal with, I must call the DEFWINDOWPROC process and turn the return value back to Windows. Otherwise, Windows will not be displayed. There are more than 280 messages specified in the UMSG parameter. In fact, there are several more important to handle, such as Windows send WM_CREATE messages when they are created, we can initialize, allocate memory, etc., while exiting WM_CLOSE will be sent, we can release the memory, etc. Clear operation, when the menu or button on Windows is pressed, the WIN32 Programmer's Reference can be referred to Win32 Programmer's Reference. Below, let's take a look at a simple program that creates a window.

A program for creating a window

.386

.Model flat, stdcall

Option Casemap: None; Case Sensitive

INCLUDE Windows.inc

INCLUDE User32.inc

INCLUDE KERNEL32.INC

INCLUDE COMCTL32.INC

INCLUDE COMDLG32.INC

INCLUDE GDI32.INC

INCLUDELIB USER32.LIB

IncludeLib kernel32.lib

INCLUDELIB COMCTL32.LIB

INCLUDELIB COMDLG32.LIB

IncludeLib GDI32.LIB

IDI_MAIN EQU 1000; icon

IDM_MAIN EQU 4000; Menu

IDM_EXIT EQU 4001

.DATA?

Hinstance DD?

HwinMain DD?

HMENU DD?

SZBuffer DB 256 DUP (?)

.DATA

SzclassName DB "Windows Template", 0

SZCAPTIONMAIN DB 'window template', 0

.code

Start:

Call_winmain

Invoke EXITPROCESS, NULL

_WinMain Proc

Local @stwcmain: WNDCLASSEX

Local @stmsg: msg

Invoke INITCOMMONCONTROLS

Invoke getModuleHandle, NULL

Mov Hinstance, EAX

Invoke Loadicon, Hinstance, IDI_MAIN

MOV Hicon, EAX

Invoke loadmenu, hinstance, idm_main

Mov Hmenu, EAX

************** Register window **************************************** **********

Invoke loadCursor, 0, IDC_ARROW

Mov @ stwcmain.hcursor, EAX

Mov @ stwcmain.cbsize, sizeof wndclassex

Mov @ stwcmain.hiconsm, 0

Mov @ stwcmain.style, cs_hredraw or cs_vredraw

Mov @ stwcmain.lpfnwndproc, Offset WNDMAINPROC

Mov @ stwcmain.cbclsextra, 0

Mov @ stwcmain.cbwndextra, 0

Mov Eax, Hinstance

Mov @ stwcmain.hinstance, EAX

Mov @ stwcmain.hicon, 0

Mov @ stwcmain.hbrbackground, color_window 1

Mov @ stwcmain.lpszclassname, offset szclassnamemov @ stwcmain.lpszMenuname, 0

Invoke registerclassex, addr @stwcmain

; ************** Establish an output window ****************************************** *********

Invoke CreateWindowex, WS_EX_CLIENTEDGE, /

Offset szclassname, Offset Szcaptionmain, /

WS_OVERLAPPEDWINDOW OR WS_VSCROLL or WS_HSCROLL, /

0, 0, 550, 300, /

NULL, HMENU, HINSTANCE, NULL

Invoke ShowWindow, Hwinmain, SW_SHOWNORMAL

Invoke UpdateWindow, HwinMain

************** Message loop ***************************************** ***********

.While true

Invoke GetMsg, NULL, 0, 0

.break .if eax == 0

Invoke TranslateMsg, Addr @stmsg

Invoke DispatchMessage, Addr @STMSG

.endw

RET

_WinMain ENDP

; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

WNDMAINPROC PROC Uses EBX EDI ESI, /

HWnd: DWORD, UMSG: DWORD, WPARAM: DWORD, LPARAM: DWORD

MOV EAX, UMSG

.IF EAX == WM_CREATE

Mov Eax, HWnd

Mov Hwinmain, EAX

Call_init

*********************************************************** *******************

.ELSEIF EAX == WM_COMMAND

.IF lparam == 0

Mov Eax, WPARAM

.IF AX == idm_exit

Call_quit

.endif

.endif

*********************************************************** *******************

.ELSEIF EAX == WM_Close

Call_quit

*********************************************************** *******************

.lse

Invoke DefWindowProc, Hwnd, UMSG, WPARAM, LPARAM

RET

.endif

XOR EAX, EAX

RET

WNDMAINPROC ENDP

_Init proc

Invoke SendMessage, HwinMain, WM_SETITION, ICON_SMALL, HICON

RET

_Init ENDP

*********************************************************** ******************* _ Quit Proc

Invoke DestroyWindow, HwinMain

Invoke PostquitMessage, NULL

RET

_Quit ENDP

*********************************************************** *******************

End Start

Analysis of window programs

Let's simply analyze this program, first programs _winmain, define two local variables @stmsg and @stwinmain in _winmain, data types are MSG and WNDCLASSEX structures. In the reference manual, you can see WNDCLASSEX definitions. All parameters of a window, such as the menu, cursor, color, window procedure, etc. .lpfnwndproc, Offset WNDMAINPROC defines the window process of processing messages, Mov @ stwcmain.lpszclassname, offset szclassname defines the name of the class you want to create, then use RegisterClassex to register this window class, note that the window is not created, you Just define a subclass, let go to create a window with the class you define. That is, use the CreateWindowEx function to create it. In the manual, CreateWindowex is defined in this:

HWND CREATEWINDOWEX

DWORD DWEXSTYLE, / / ​​EXTENDED WINDOW STYLE

LPCTSTR LPCLASSNAME, / / ​​POINTER TO Registered Class Name

LPCTSTR LPWINDOWNAME, / / ​​POINTER TO WINDOW NAME

DWORD DWSTYLE, / / ​​WINDOW STYLE

Int x, // horizontal position of window

Int Y, // Vertical Position of Window

INT NWIDTH, / / ​​WINDOW WIDTH

INT NHEIGHT, // WINDOW HEIGHT

HWND HWNDPARENT, // Handle to Parent or Owner Window

Hmenu Hmenu, // Handle To Menu, or Child-Window Identifier

Hinstance Hinstance, // Handle To Application Instance

LPVOID LPPARAM / / POINTER TO WINDOW-CREATION DATA);

The parameter dwexStyle is the style of the window, and lpClassName is the name of our own defined class. If you want to create a defined class, such as a RicheDit class, etc., just put lpClassName points to the "RicheDit32" string, of course, you don't have to use registerclass and write your own window procedure. After executing CREATEWINDOWEX, get a return value is the window handle, this value is often used frequently, so save it first. At this time, the window is not displayed on the screen, but is in a hidden state, we have to use ShowWindow to display the window and draw the contents of the window with UpdateWindow. After the window is displayed, the program enters a loop ---- message loop, I have said before, the role is to keep the Windows message and send it to the window process. GetMessage removes a message from the message queue. If the result is not wm_quit, then getMessage returns a non-zero value, otherwise returns zero, at this time, the program executes the EXITPROCESS to return the operating system. TranslateMessage converts the message to handle some shortcuts, and DispatchMessage sends the process-processed message back to Windows, processes the Windows call window process. When the window process is complete, the program returns from DispatchMessage, thus starting A getMessage call. The parameters of these letters can refer to the manual.

Analysis of the window process

The window process has four parameters, hWnd is the handle of this window, the same value, the value returned when the window is created, the UMSG is the type of message, WPARAM, and LPARAM are the parameters of the message, the meaning and value of the message are different depending on the message. . In this program, we deal with WM_CREATE, WM_COMMAND, and WM_QUIT messages, then return 0, for non-processed messages, use Invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM, and use RET to transfer the return value back to Windows. When responding to a WM_CLOSE message, we use the DestroyWindow to clear the window and generate a WM_QUIT message with PostQuitMessage so that the program returns 0 when the message loop calls GetMessage to end the message loop and end the program.

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

New Post(0)