程序运行如下;当按下一个按键,在窗口上显示该字符;
程序;masm32;Build All;
当消息为wm_char,即 uMsg==WM_CHAR;
所按下的键在wParam中,将此值转到char中;然后调用InvalidateRect,调用此函数后,窗口无效,将执行消息处理的WM_PAINT分支;
处理WM_PAINT消息时,调用TextOut,在窗口上输出了char的内容;
看代码,
char WPARAM 20h
char 标号定义在常量区,类型是WPARAM;
假设现在我想在按下 o 键时,在窗口上输出 9 ;修改代码如下图;
构建,如下图;错误,提示 标号重定义:char;
看来还得使用push pop对,改为如下;
好了,当按下 o ,输出 9;其他键不变;
.386
.model flat,stdcall
option casemap:none
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
include S:\masm32\include\windows.inc
include S:\masm32\include\user32.inc
include S:\masm32\include\kernel32.inc
include S:\masm32\include\gdi32.inc
includelib S:\masm32\lib\user32.lib
includelib S:\masm32\lib\kernel32.lib
includelib S:\masm32\lib\gdi32.lib
.data
ClassName db "SimpleWinClass",0
AppName db "wm_char Demo Window",0
char WPARAM 20h
.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL 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 hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
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,100,100,\
100,100,NULL,NULL,hInst,NULL
mov hwnd,eax
invoke ShowWindow,hwnd,SW_SHOWNORMAL
invoke UpdateWindow,hwnd
.while TRUE
invoke GetMessage,ADDR msg,NULL,0,0
.break .if (!eax)
invoke TranslateMessage,ADDR msg
invoke DispatchMessage,ADDR msg
.endw
mov eax,msg.wParam
ret
WinMain endp
WndProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL hdc:HDC
LOCAL ps:PAINTSTRUCT
.if uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.elseif uMsg==WM_CHAR
push wParam
pop char
.if char=='o'
push '9'
pop char
.endif
invoke InvalidateRect,hWnd,NULL,TRUE
.elseif uMsg==WM_PAINT
invoke BeginPaint,hWnd,ADDR ps
mov hdc,eax
invoke TextOut,hdc,0,0,ADDR char,1
invoke EndPaint,hWnd,ADDR ps
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
end start