?? tut3.html
字號(hào):
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Mozilla/4.7 [en] (Win98; I) [Netscape]">
<title>Iczelion's Win32 ASM tutorial 3: A Simple Window</title>
</head>
<body text="#C0C0C0" bgcolor="#000000" link="#00FFFF" vlink="#FF34D0" alink="#98FF98">
<center>
<h1>
<font face="Arial"><font color="#999900">Tutorial 3: A Simple Window</font></font></h1></center>
<font size=-1>In this tutorial, we will build a Windows program that displays
a fully functional window on the desktop.</font>
<br><font size=-1>Download the example file <a href="files/tut03.zip">here</a></font>
<h3>
<font color="#FF9800"><font size=+0>Theory:</font></font></h3>
<font size=-1>Windows programs rely heavily on API functions for their
GUI. This approach benefits both users and programmers. For users, they
don't have to learn how to navigate the GUI of each new programs, the GUI
of Windows programs are alike. For programmers, the GUI codes are already
there,tested, and ready for use. The downside for programmers is the increased
complexity involved. In order to create or manipulate any GUI objects such
as windows, menu or icons, programmers must follow a strict recipe. But
that can be overcome by modular programming or OOP paradigm.</font>
<br><font size=-1>I'll outline the steps required to create a window on
the desktop below:</font>
<ol>
<li>
<font size=-1>Get the instance handle of your program (required)</font></li>
<li>
<font size=-1>Get the command line (not required unless your program wants
to process a command line)</font></li>
<li>
<font size=-1>Register window class (required ,unless you use predefined
window types, eg. MessageBox or a dialog box)</font></li>
<li>
<font size=-1>Create the window (required)</font></li>
<li>
<font size=-1>Show the window on the desktop (required unless you don't
want to show the window immediately)</font></li>
<li>
<font size=-1>Refresh the client area of the window</font></li>
<li>
<font size=-1>Enter an infinite loop, checking for messages from Windows</font></li>
<li>
<font size=-1>If messages arrive, they are processed by a specialized function
that is responsible for the window</font></li>
<li>
<font size=-1>Quit program if the user closes the window</font></li>
</ol>
<font size=-1>As you can see, the structure of a Windows program is rather
complex compared to a DOS program. But the world of Windows is drastically
different from the world of DOS. Windows programs must be able to coexist
peacefully with each other. They must follow stricter rules. You, as a
programmer, must also be more strict with your programming style and habit.</font>
<h3>
<font color="#CC0000"><font size=+0>Content:</font></font></h3>
<font size=-1>Below is the source code of our simple window program. Before
jumping into the gory details of Win32 ASM programming, I'll point out
some fine points which will ease your programming.</font>
<ul>
<li>
<font size=-1>You should put all Windows constants, structures and function
prototypes in an include file and include it at the beginning of your .asm
file. It'll save you a lot of effort and typo. Currently, the most complete
include file for MASM is hutch's windows.inc which you can download from
his page or my page. You can also define your own constants & structure
definitions but you should put them into a separate include file.</font></li>
<li>
<font size=-1>Use<font color="#FFFF68"> includelib</font> directive to
specify the import library used in your program. For example, if your program
calls MessageBox, you should put the line:</font></li>
<ul><font color="#FF9800"><font size=-1>includelib user32.lib</font></font></ul>
<font size=-1>at the beginning of your .asm file. This directive tells
MASM that your program will make uses of functions in that import library.
If your program calls functions in more than one library, just add an includelib
for
each library you use. Using IncludeLib directive, you don't have to worry
about import libraries at link time. You can use /LIBPATH linker switch
to tell Link where all the libs are.</font>
<li>
<font size=-1>When declaring API function prototypes, structures, or constants
in your include file, try to stick to the original names used in Windows
include files, including case. This will save you a lot of headache when
looking up some item in Win32 API reference.</font></li>
<li>
<font size=-1>Use makefile to automate your assembling process. This will
save you a lot of typing.</font></li>
</ul>
<b><font size=-1>.386</font></b>
<br><b><font size=-1>.model flat,stdcall</font></b>
<br><b><font size=-1>option casemap:none</font></b>
<br><b><font size=-1>include \masm32\include\windows.inc</font></b>
<br><b><font size=-1>include \masm32\include\user32.inc</font></b>
<br><b><font size=-1>includelib \masm32\lib\user32.lib
<font color="#009900">; calls to functions in user32.lib and kernel32.lib</font></font></b>
<br><b><font size=-1>include \masm32\include\kernel32.inc</font></b>
<br><b><font size=-1>includelib \masm32\lib\kernel32.lib</font></b>
<p><b><font size=-1>WinMain proto :DWORD,:DWORD,:DWORD,:DWORD</font></b>
<p><b><font size=-1>.DATA <font color="#009900">
; initialized data</font></font></b>
<br><b><font size=-1>ClassName db "SimpleWinClass",0 <font color="#009900">
; the name of our window class</font></font></b>
<br><b><font size=-1>AppName db "Our First Window",0 <font color="#009900">
; the name of our window</font></font></b>
<p><b><font size=-1>.DATA? <font color="#009900">
; Uninitialized data</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">hInstance HINSTANCE ?
</font><font color="#009900">; Instance handle of our program</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>CommandLine LPSTR ?</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">.CODE </font><font color="#009900">
; Here begins our code</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>start:</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">invoke GetModuleHandle, NULL </font><font color="#009900">
; get the instance handle of our program.</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">
</font><font color="#009900">; Under Win32, hmodule==hinstance mov hInstance,eax</font></font></b>
<br><b><font color="#FFFFFF"><font size=-1>mov hInstance,eax</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">invoke GetCommandLine </font><font color="#009900">
; get the command line. You don't have to call this function IF</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">
</font><font color="#009900">; your program doesn't process the command
line.</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>mov CommandLine,eax</font></font></b>
<br><b><font size=-1><font color="#CCCCCC">invoke WinMain, hInstance,NULL,CommandLine,
SW_SHOWDEFAULT </font><font color="#009900">
; call the main function</font></font></b>
<br>
<b><font size=-1><font color="#CCCCCC">invoke ExitProcess, eax </font><font color="#009900">
; quit our program. The exit code is returned in eax from WinMain.</font></font></b>
<p><b><font color="#CCCCCC"><font size=-1>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</font></font></b>
<br><b><font size=-1><font color="#CCCCCC"> LOCAL wc:WNDCLASSEX </font><font color="#009900">
; create local variables on stack</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> LOCAL msg:MSG</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> LOCAL hwnd:HWND</font></font></b>
<p><b><font size=-1><font color="#CCCCCC"> mov
wc.cbSize,SIZEOF WNDCLASSEX </font><font color="#009900">
; fill values in members of wc</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.style, CS_HREDRAW or CS_VREDRAW</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.lpfnWndProc, OFFSET WndProc</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.cbClsExtra,NULL</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.cbWndExtra,NULL</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> push
hInstance</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> pop
wc.hInstance</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.hbrBackground,COLOR_WINDOW+1</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.lpszMenuName,NULL</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.lpszClassName,OFFSET ClassName</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> invoke LoadIcon,NULL,IDI_APPLICATION</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.hIcon,eax</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.hIconSm,eax</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> invoke LoadCursor,NULL,IDC_ARROW</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> mov
wc.hCursor,eax</font></font></b>
<br><b><font size=-1><font color="#CCCCCC"> invoke RegisterClassEx,
addr wc </font><font color="#009900">
; register our window class</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1> invoke CreateWindowEx,NULL,\</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>
ADDR ClassName,\</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>
ADDR AppName,\</font></font></b>
<br><b><font color="#CCCCCC"><font size=-1>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -