(Translation) Win32ASM instance -4

zhaozj2021-02-16  45

4.0 - Creating The Main Frame Creating a Main Framework

Now We Will Create The Main Frame of Our Program:

Now we will create the main framework of our program:

Add Two Prototypes after the incdude:

Add two prototypes after INCLUDE

Winmain Proto Stdcall: DWORD,: DWORD,: DWORD: DWORDWDPROC Proto stdcall: DWORD,: DWORD,: DWORD,: DWORD

There Prototypes Will Tell Masm How Many Parameters There Functions Take (4 DWORD's in this case)

These prototypes will tell MASM how many parameters need these functions (4 DWORDs in this case)

Add these to the .data and .data? Sections of Your Program:

Add these to your program. Data and .Data?

.DataAppname DB "mosaic", 0classname dB "mosaic32", 0.data? Hinstance DD?

Appname and classname area strings use for the main window, hinstance is a dword That Will Hold The Application Instance Handle.

AppName and ClassName are strings for the main window. Hinstance is a DWORD that will be equipped with a program instance handle.

START of CODE:

The start of the code part:

.code start:; Get module handle and save it invoke GetModuleHandle, NULL mov hInstance, eax; Init Common Controls library invoke InitCommonControls; Run winmain procedure and exit program invoke WinMain, hInstance, NULL, NULL, SW_SHOWNORMAL invoke ExitProcess, eax

GetModuleHandle retreives the module handle, which is stored in hInstance. InitCommonControls is an initialization function for the common control library. This library contains several controls that we will use in our program, and it should be initialized by calling InitCommonControls. After that, WinMain is Called (a Function We Will Create Soon). WinMain Will Return When The User Quits The Program, WHEREAFTER EXITPROCESS.

GetModuleHandle gets the module handle and stores in Hinstance. INITCOMMONCONTROLS is an initialization function for a general control library. This library contains several controls we have to use in our programs, and they should be initialized by INITCOMMONCONTROLS. After that, call WinMain (a function we want to create immediately). WinMain returns when the user exits the program, then calls the EXITPROCESS to exit the process. The Winmain Procedure:

WinMain process:

; ================================================== ==============================; WinMain; ======================================================================================================================================================================= ============================================================================================================================================================================================================= ============ WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORDLOCAL wc: WNDCLASSEXLOCAL msg: MSGLOCAL 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 hInstance pop wc.hInstance mov wc.hbrBackground, COLOR_WINDOW mov wc.lpszMenuName, NULL mov wc.lpszClassName, OFFSET ClassName

Here Our Icons Are Loaded from the resource file and their handles area stores wc.hicon and wc.hiconsm. Windows Will Use these Handles To Display The icons in the main window:

Here, our icon is loaded from the resource file and its handle is stored in Wc.hicon and Wc.hiconsm. Windows will use these handles to the icon is displayed in the main window: invoke LoadIcon, hInstance, ICON1_BIG mov wc.hIcon, eax invoke LoadIcon, hInstance, ICON2_SMALL mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc INVOKE CreateWindowEx, NULL, aDDR ClassName, aDDR AppName, / WS_OVERLAPPEDWINDOW-WS_MAXIMIZEBOX-WS_SIZEBOX, / CW_USEDEFAULT, CW_USEDEFAULT, 258,350, NULL, NULL, / hInst, NULL mov hwnd, eax invoke ShowWindow, hwnd, CmdShow invoke UpdateWindow , Hwnd .While True Invoke GetMessage, Addr MSG, NULL, 0, 0.Break .if (! EAX) Invoke TranslateMessage, Addr MSG .Endw Mov EAX, MSG.WParam Retwinmain Endp

The WinMain functions initializes a WNDCLASSEX structure that includes parameters of the windows class, then RegisterClassEx is called to register the window class named 'Mosaic32'. CreateWindowEx is called to create a new window based on the new class, ShowWindow and UpdateWindow will make the window Visible. THE PROGRAM GoES in an infinite loop Until the user closs the window. this loop is called The message pump (see my tutorials for more info).

The WinMain function initializes a WndClassex structure that contains window class parameters, then calls RegisterClassex to register a window class name called "Mosaic32". CreateWindowEx is called to create a new window based on new categories, showWindow and UpdateWindow will make the window visible. Then the program enters an endless loop until the user closes the window. This cycle is called a message pump (to get more information, see the tutorial in front)

The window style is ws_overlappedwindow without a sizebox (= not resize). The window is 258x350 pixels.

Window styles are WS_OVERLAPPEDWINDOW without maximizing buttons with sizebox (= non-adjustable size). The window is 258 × 350 pixels. The window also needs a window procedure that handles all the messages sent to the window In our program this is the function WndProc, which takes 4 parameters:. HWnd contains the window handle, uMsg the message, wParam and lParam the message parameters.

The window also requires a window procedure to process all messages sent to the window. In our program, this is a WndProc function. It has four parameters: hWnd contains window handles, Umeg messages, WPARAM, and iParm message parameters.

; ================================================== ==============================; window procedure; ================= ============================================================================================================================================================================================================= ============= WndProc Proc HWND: DWORD, UMSG: DWORD: DWORDMOV EAX, UMSG .IF EAX == WM_CREATE; YET to Do .elseif Eax == WM_DESTROY INVOKE PostquitMessage, Null .Else Invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM RET .EndifretWndProc Endpthe Window Procedure Processes The WHEN THE User Quits.

After You'Ve Done All this, your Files Should Look Like this: Mosaic1.zip

After you have finished doing everything, your file should be like this: mosaic.zipnow run make.bat to assemble & link your program. You Should Have No Errors (You Might Get A Warning About GDI32 But Never Mind About That) And you Should Be Able To Run The Program. IT Has A Small Icon In The Upper Left Corner, The Big Icon Is Visible In The Task List (Alt Tab) and your mosaic.exe Has The Big Icon As icon.

Now run make.bat to compile and link your program. You should not get an error (you may get a warning about GDI32, but don't care) and you should run the program. It has a small icon in the upper left corner, and the large icon can be seen in the task class (Alt Tab) and your Mosaic.exe puts a large icon as an icon.

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

New Post(0)