[ Pobierz całość w formacie PDF ]
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
Same thing for the cursor, just NULL as hInstance, and a standard cursor type: IDC_ARROW, the
standard windows arrow.
invoke RegisterClassEx, ADDR wc
Now finally register the class using RegisterClassEx with a pointer to the WNDCLASSEX structure
wc as parameter.
12.5 - Creating the window
Now you've registered a class you can create a window from it:
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
http://www.chmconverter.com 44 / 53
Win32Asm Tutorial Converted by Atop CHM to PDF Converter free version!
);
dwExStyle and dwStyle are two parameters which determine the style of the window.
lpClassName is a pointer to your registered classname.
lpWindowName is the name of your window (this will be in the caption of your window if it has
one)
x, y, nWidth, nHeight determine the position and size of your window.
hWndParent is the handle of the window that owns the new window. Can be null if you don't have a
parent window.
hMenu is a handle of a windows menu (discussed in a later tutorial, null for now)
hInstance is the application instance handle
lpParam is an extra value you can use in your program
.data
AppName "FirstWindow",0
.code
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
CW_USEDEFAULT,400,300,NULL,NULL,\
hInst,NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
(note that the \-character makes the assembler read to the next line as if it where on the same line.)
Our code will create a new windows, with our classname we've just registered. The title will be
"FirstWindow" (AppName), the style is WS_OVERLAPPEDWINDOW, which is a style that creates
an overlapped window with a caption, system menu, resizable border and a minimize/maximize-
box. CW_USEDEFAULT as x and y position will make windows use the default position for the new
window. The (initial) size of the window is 400x300 pixels.
The return value of the function is the window handle, HWND, which is stored in the local variable
hwnd. Then the window is showed with ShowWindow. UpdateWindow ensures that the window
will be drawn.
12.6 - Message Loop
A window can communicate with your program and other windows using messages. The window
procedure (see later on in this tutorial) is called whenever a message is pending for a specific
window. Each window has a message loop or message pump. This is an endless loop that checks if
there's a message for your window, and if it is, passes the message to the dispatchmessage function.
This function will call your window procedure. The message loop and the windows procedure are
two different things!!!
WinMain proc
hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL hwnd:DWORD
LOCAL msg:MSG ;
........
.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW
http://www.chmconverter.com 45 / 53
Win32Asm Tutorial Converted by Atop CHM to PDF Converter free version!
This is what the message loop looks like. The .WHILE TRUE, .ENDW loop goes on until eax is 0.
GetMessage returns 0 if it receives the message WM_QUIT, which should close the window so the
program must exit the messageloop whenever GetMessage returns 0. If it doesn't, the message is
passed to TranslateMessage (this function translates keypresses to messages) and then the message
is dispatched by windows using the DispatchMessage function. The message itself in a message loop
consists of a MSG structure (LOCAL msg:MSG is added to the procedure, which adds a local
message structure called msg). You can use this message loop in all your programs.
12.7 - Window Procedure
Messages will be send to the window procedure. A window procedure should always look like this:
WndProc PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD
.code
WndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
mov eax, uMsg
.IF eax==XXXX
.ELSEIF eax==XXXX
.ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
.ENDIF
ret
WndProc endp
The window procedure should always have 4 parameters:
hWnd contains the window handle.
uMsg is the message
wParam is the first parameter for the message (message specific)
lParam is the second parameter for the message (message specific)
Messages that the window does not process should be passed to DefWindowProc, which takes care
of the processing. An example window procedure:
WndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
mov eax, uMsg
.IF eax==WM_CREATE
invoke MessageBox, NULL, ADDR AppName, ADDR AppName, NULL
.ELSEIF eax==WM_DESTROY
invoke PostQuitMessage, NULL
.ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
.ENDIF
ret
WndProc endp
This code displays the name of the application when the window is initialized. Also note that I've
added the processing of the WM_DESTROY message. This message is sent if the window should
close. The application should react to it with a PostQuitMessage.
Now take a look at the final code:
[top]
http://www.chmconverter.com 46 / 53
Win32Asm Tutorial Converted by Atop CHM to PDF Converter free version!
Win32Asm Tutorial
prev
14- N/A
14.0 - N/A
Sorry, this tutorial is not available yet. This tutorial is still under construction. Please check again
later!
[top]
http://www.chmconverter.com 47 / 53
Win32Asm Tutorial Converted by Atop CHM to PDF Converter free version!
Win32Asm Tutorial
prev next
Tools
Tools
This section describes the use of various tools. Please select a tool:
Masm
Assembler
Link
Linker
brc32
Resource compiler
rc
[top]
http://www.chmconverter.com 48 / 53
Win32Asm Tutorial Converted by Atop CHM to PDF Converter free version!
Win32Asm Tutorial
prev next
Tools\Masm
Masm
Masm is the assembler I use. Others are tasm and nasm but I prefer masm myself (see why). The use
of tasm and nasm (and other assemblers) is not discussed here.
Usage
Masm is ml.exe. The version I use is "Macro Assembler Version 6.14.8444". Syntax:
ML [ /options ] filelist [ /link linkoptions ]
Here are some important options:
assemble without linking
/c You will use this option most of the time as you will be using
an external linker like link.exe to link your files.
generate COFF format object file
/coff
This is the generic file format for the microsoft linker.
name object file
/Fo Can be used if you want to output an object file with an other
name than your source file
Use Pascal, C, or Stdcall calls
/G
Select the default calling convention for your procedures.
Add symbolic debug info
/Zi
Use this option if you want to use a debugger.
Set include path
/I
[ Pobierz całość w formacie PDF ]