.386 ;告訴編譯器程序所用指令集,(.386P)帶P表示使用特權級指令。
.model flat,stdcall;內存模式平坦的4G空間
;參數傳遞方式C從右往左,調用者恢復堆棧指針。
;PSCAL從左往右,被調用者恢復堆棧指針。
;stdcall從右往左傳遞,但是堆棧指針由被調用者恢復。
option casemap:none;區分標志的大小寫
include windows.inc;各種常量,宏的定義
include user32.inc
includelib user32.lib ; calls to functions in user32.lib and kernel32.lib
include kernel32.inc
includelib kernel32.lib
include gdi32.inc
includelib gdi32.lib

RGB macro Red,Green,Blue
xor eax,eax
mov ah,Blue
shl eax,8
mov ah,Green
mov al,Red
endm

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA ; initialized data
ClassName db "SimpleWinClass",0 ; the name of our window class
AppName db "Our First Window",0 ; the name of our window
OurText db "彭小虎是個SB!",0 ; the text of our client
FontName db "script",0
MouseClick db 0

.DATA? ; Uninitialized data
hInstance HINSTANCE ? ; Instance handle of our program
CommandLine LPSTR ?
hitpoint POINT <>
.CODE ; Here begins our code
start:
invoke GetModuleHandle, NULL ; 獲取一個應用程序或動態鏈接庫的模塊句柄,返回值存儲在eax
; Under Win32, hmodule==hinstance mov hInstance,eax
mov hInstance,eax
invoke GetCommandLine ; 獲得指向當前命令行緩沖區的一個指針
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ; call the main function,第三個參數表示窗口初始狀態
invoke ExitProcess, eax ; quit our program. The exit code is returned in eax from WinMain.
;---------------------------------------------------------------------------
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX ; create local variables on stack
LOCAL msg:MSG
LOCAL hwnd:HWND

mov wc.cbSize,SIZEOF WNDCLASSEX ; fill values in members of wc
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInstance
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 ; register our window class
invoke CreateWindowEx,NULL,\
ADDR ClassName,\ ;窗口類名
ADDR AppName,\ ;窗口標題欄名
WS_OVERLAPPEDWINDOW,\ ;窗口樣式
;窗口位置與高寬設置
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
CW_USEDEFAULT,\
NULL,\ ;雙親句柄
NULL,\ ;菜單句柄
hInst,\ ;實例句柄
NULL
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow ; display our window on desktop
invoke UpdateWindow, hwnd ; refresh the client area

.WHILE TRUE ; Enter message loop
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK.IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW
mov eax,msg.wParam ; return exit code in eax
ret
WinMain endp
;---------------------------------------------------------------------------
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL hdc:HDC
LOCAL ps:PAINTSTRUCT
LOCAL hfont:HFONT

.IF uMsg==WM_DESTROY ; if the user closes our window
invoke PostQuitMessage,NULL ; quit our application

.ELSEIF uMsg==WM_LBUTTONDOWN
mov eax,lParam
and eax,0FFFFh
mov hitpoint.x,eax
mov eax,lParam
shr eax,16
mov hitpoint.y,eax
mov MouseClick,TRUE
invoke InvalidateRect,hWnd,NULL,TRUE

.ELSEIF uMsg==WM_PAINT
invoke BeginPaint,hWnd,ADDR ps ;返回設備句柄
mov hdc,eax ;取得設備句柄
invoke CreateFont,50,16,270,0,FW_DEMIBOLD,0,0,0,GB2312_CHARSET,\
OUT_CHARACTER_PRECIS,CLIP_DEFAULT_PRECIS,\
DEFAULT_QUALITY,DEFAULT_PITCH or FF_SCRIPT,\
ADDR FontName
invoke SelectObject,hdc,eax
mov hfont,eax;保存
RGB 8,80,8
invoke SetBkColor,hdc,eax
invoke TextOut,hdc,80,120,ADDR OurText,SIZEOF OurText
invoke SelectObject,hdc, hfont;恢復

.IF MouseClick
invoke lstrlen,ADDR OurText
invoke TextOut,hdc,hitpoint.x,hitpoint.y,ADDR OurText,eax
.ENDIF

invoke EndPaint,hWnd, ADDR ps
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam ; Default message processing
ret
.ENDIF
xor eax,eax
ret
WndProc endp
;---------------------------------------------------------------------------
end start
|