📄 tut6.html
字号:
<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>
mov hwnd,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke ShowWindow, hwnd,SW_SHOWNORMAL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke UpdateWindow, hwnd</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.WHILE TRUE</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetMessage, ADDR msg,NULL,0,0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.BREAK .IF (!eax)</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke TranslateMessage, ADDR msg</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke DispatchMessage, ADDR msg</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ENDW</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov eax,msg.wParam</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
ret</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
endp</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WndProc
proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL hdc:HDC</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL ps:PAINTSTRUCT</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.IF uMsg==WM_DESTROY</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke PostQuitMessage,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ELSEIF uMsg==WM_CHAR</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
push wParam</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
pop char</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke InvalidateRect, hWnd,NULL,TRUE</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ELSEIF uMsg==WM_PAINT</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke BeginPaint,hWnd, ADDR ps</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov hdc,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke TextOut,hdc,0,0,ADDR char,1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke EndPaint,hWnd, ADDR ps</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ELSE</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke DefWindowProc,hWnd,uMsg,wParam,lParam</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
ret</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
.ENDIF</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
xor eax,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
ret</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WndProc
endp</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>end
start</font></font></font></b>
<br>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Analysis:</font></font></font></h3>
<p><br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>char
WPARAM 20h
; the character the program receives from keyboard</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>This
is the variable that will store the character received from the keyboard.
Since the character is sent in WPARAM of the window procedure, we define
the variable as type WPARAM for simplicity. The initial value is 20h or
the space since when our window refreshes its client area the first time,
there is no character input. So we want to display space instead.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>
.ELSEIF uMsg==WM_CHAR</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>
push wParam</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>
pop char</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>
invoke InvalidateRect, hWnd,NULL,TRUE</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>This
is added in the window procedure to handle the WM_CHAR message. It just
puts the character into the variable named "char" and then calls InvalidateRect.
InvalidateRect makes the specified rectangle in the client area invalid
which forces Windows to send WM_PAINT message to the window procedure.
Its syntax is as follows:</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>InvalidateRect
proto hWnd:HWND,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
lpRect:DWORD,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
bErase:DWORD</font></font></font></b>
<p><font face="Arial,Helvetica"><font size=-1><b><font color="#999900">lpRect</font></b><font color="#CCCCCC">
is a pointer to the rectagle in the client area that we want to declare
invalid. If this parameter is null, the entire client area will be marked
as invalid.</font></font></font>
<br><font face="Arial,Helvetica"><font size=-1><b><font color="#999900">bErase</font></b><font color="#CCCCCC">
is a flag telling Windows if it needs to erase the background. If this
flag is TRUE, then Windows will erase the backgroud of the invalid rectangle
when BeginPaint is called.</font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>So
the strategy we used here is that: we store all necessary information relating
to painting the client area and generate WM_PAINT message to paint the
client area. Of course, the codes in WM_PAINT section must know beforehand
what's expected of them. This seems a roundabout way of doing things but
it's the way of Windows.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Actually
we can paint the client area during processing WM_CHAR message by calling
GetDC and ReleaseDC pair. There is no problem there. But the fun begins
when our window needs to repaint its client area. Since the codes that
paint the character are in WM_CHAR section, the window procedure will not
be able to repaint our character in the client area. So the bottom line
is: put all necessary data and codes that do painting in WM_PAINT. You
can send WM_PAINT message from anywhere in your code anytime you want to
repaint the client area.</font></font></font>
<p><b><font face="Arial,Helvetica"><font color="#999900"><font size=-1>
invoke TextOut,hdc,0,0,ADDR char,1</font></font></font></b>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>When
InvalidateRect is called, it sends a WM_PAINT message back to the window
procedure. So the codes in WM_PAINT section is called. It calls BeginPaint
as usual to get the handle to device context and then call TextOut which
draws our character in the client area at x=0, y=0. When you run the program
and press any key, you will see that character echo in the upper left corner
of the client window. And when the window is minimized and maximized again,
the character is still there since all the codes and data essential to
repaint are all gathered in WM_PAINT section.</font></font></font>
<br>
<hr WIDTH="100%">
<center><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>[<a href="http://win32asm.cjb.net">Iczelion's
Win32 Assembly HomePage</a>]</font></font></font></b></center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -