📄 tut4.html
字号:
<!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="Author" content="Iczelion">
<meta name="GENERATOR" content="Mozilla/4.51 [en] (Win95; I) [Netscape]">
<title>Iczelion's Win32 Assembly Tutorial 4: Painting with Text</title>
</head>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">
<center>
<h1>
<font face="Arial,Helvetica"><font color="#999900">Tutorial 4: Painting
with Text</font></font></h1></center>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>In this
tutorial, we will learn how to "paint" text in the client area of a window.
We'll also learn about device context.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>You
can download the source code <a href="files/tut04.zip">here</a>.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Text in
Windows is a type of GUI object. Each character is composed of numerous
pixels (dots) that are lumped together into a distinct pattern. That's
why it's called "painting" instead of "writing". Normally, you paint text
in your own client area (actually, you can paint outside client area but
that's another story). Putting text on screen in Windows is drastically
different from DOS. In DOS, you can think of the screen in 80x25 dimension.
But in Windows, the screen are shared by several programs. Some rules must
be enforced to avoid programs writing over each other's screen. Windows
ensures this by limiting painting area of each window to its own client
area only. The size of client area of a window is also not constant. The
user can change the size anytime. So you must determine the dimensions
of your own client area dynamically.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Before
you can paint something on the client area, you must ask for permission
from Windows. That's right, you don't have absolute control of the screen
as you were in DOS anymore. You must ask Windows for permission to
paint your own client area. Windows will determine the size of your client
area, font, colors and other GDI attributes and sends a handle to device
context back to your program. You can then use the device context as a
passport to painting on your client area.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>What
is a device context? It's just a data structure maintained internally by
Windows. A device context is associated with a particular device, such
as a printer or video display. For a video display, a device context is
usually associated with a particular window on the display.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Some
of the values in the device context are graphic attributes such as colors,
font etc. These are default values which you can change at will. They exist
to help reduce the load from having to specify these attributes in every
GDI function calls.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>You
can think of a device context as a default environment prepared for you
by Windows. You can override some default settings later if you so wish.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
a program need to paint, it must obtain a handle to a device context. Normally,
there are several ways to accomplish this.</font></font></font>
<ul><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>call
BeginPaint in response to WM_PAINT message.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>call
GetDC in response to other messages.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>call
CreateDC to create your own device context</font></font></font></b></ul>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>One thing
you must remember, after you're through with the device context handle,
you must release it during the processing of a single message. Don't obtain
the handle in response to one message and release it in response to another.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Windows
posts WM_PAINT messages to a window to notify that it's now time to repaint
its client area. Windows does not save the content of client area of a
window. Instead, when a situation occurs that warrants a repaint
of client area (such as when a window was covered by another and is just
uncovered), Windows puts WM_PAINT message in that window's message queue.
It's the responsibility of that window to repaint its own client area.
You must gather all information about how to repaint your client area in
the WM_PAINT section of your window procedure, so the window procudure
can repaint the client area when WM_PAINT message arrives.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Another
concept you must come to terms with is the invalid rectangle. Windows defines
an invalid rectangle as the smallest rectangular area in the client area
that needs to be repainted. When Windows detects an invalid rectangle in
the client area of a window , it posts WM_PAINT message to that window.
In response to WM_PAINT message, the window can obtain a paintstruct structure
which contains, among others, the coordinate of the invalid rectangle.
You call BeginPaint in response to WM_PAINT message to validate the invalid
rectangle. If you don't process WM_PAINT message, at the very least you
must call DefWindowProc or ValidateRect to validate the invalid rectangle
else Windows will repeatedly send you WM_PAINT message.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Below
are the steps you should perform in response to a WM_PAINT message:</font></font></font>
<ul><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>Get
a handle to device context with BeginPaint.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>Paint
the client area.</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>Release
the handle to device context with EndPaint</font></font></font></b></ul>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Note that
you don't have to explicitly validate the invalid rectangle. It's automatically
done by the BeginPaint call. Between BeginPaint-Endpaint pair, you can
call any GDI functions to paint your client area. Nearly all of them require
the handle to device context as a parameter.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Content:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>We will
write a program that displays a text string "Win32 assembly is great and
easy!" in the center of the client area.</font></font></font>
<br>
<blockquote><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>option
casemap:none</font></font></font></b><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proto :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1></font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.DATA</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ClassName
db "SimpleWinClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>AppName
db "Our First Window",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>OurText
db "Win32 assembly is great and easy!",0</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.DATA?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>hInstance
HINSTANCE ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>CommandLine
LPSTR ?</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.CODE</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>start:</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetModuleHandle, NULL</font></font></font></b> <br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov hInstance,eax</font></font></font></b> <br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetCommandLine<br>
mov CommandLine,eax</font></font></font></b><br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke ExitProcess,eax</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proc hInst:HINSTANCE, hPrevInst:HINSTANCE, CmdLine:LPSTR, CmdShow:DWORD</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL wc:WNDCLASSEX</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL msg:MSG</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL hwnd:HWND</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbSize,SIZEOF WNDCLASSEX</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.style, CS_HREDRAW or CS_VREDRAW</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpfnWndProc, OFFSET WndProc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbClsExtra,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbWndExtra,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
push hInst</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
pop wc.hInstance</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hbrBackground,COLOR_WINDOW+1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpszMenuName,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpszClassName,OFFSET ClassName</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke LoadIcon,NULL,IDI_APPLICATION</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hIcon,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hIconSm,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke LoadCursor,NULL,IDC_ARROW</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hCursor,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke RegisterClassEx, addr wc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
hInst,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -