gdiapi.asm
来自「windows下汇编语言 学习汇编语言好助手」· 汇编 代码 · 共 185 行
ASM
185 行
;******************************
;文件:GDIAPI.asm *
;功能:使用GDI的API的例子 *
;******************************
.386p
.model flat,stdcall
include win32.inc
extrn GetMessageA:proc
extrn TranslateMessage:proc
extrn DispatchMessageA:proc
extrn ShowWindow:proc
extrn UpdateWindow:proc
extrn CreateWindowExA:proc
extrn RegisterClassA:proc
extrn DefWindowProcA:proc
extrn MessageBoxA:proc
extrn ExitProcess:proc
extrn LoadIconA:proc
extrn LoadCursorA:proc
extrn GetStockObject:proc
extrn GetModuleHandleA:proc
extrn PostQuitMessage:proc
extrn _wsprintfA:proc
extrn SetWindowTextA:proc
extrn Rectangle:proc
extrn EndPaint:proc
extrn SelectObject:proc
extrn DeleteObject:proc
extrn BeginPaint:proc
extrn CreateHatchBrush:proc
extrn CreateSolidBrush:proc
extrn CreatePenIndirect:proc
extrn GetDC:proc
extrn TextOutA:proc
extrn ReleaseDC:proc
POINT struc
x dd ?
y dd ?
ends
MSG struc
hwnd dd ?
message dd ?
wParam dd ?
lParam dd ?
time dd ?
pt POINT<>
ends
WNDCLASS struc
style dd ?
lpfnWndProc dd ?
cbClsExtra dd ?
cbWndExtra dd ?
hInstance dd ?
hIcon dd ?
hCursor dd ?
hbrBackground dd ?
lpszMenuName dd ?
lpszClassName dd ?
ends
.data
myclassname db 'MyClass',0
Caption db 'GDI Api sample',0
string db 30 dup (?)
stringTmp db '鼠标位置(%d,%d) ',0
align 4
hBrsh0 dd ?
hBrsh1 dd ?
hBrsh2 dd ?
hBrsh3 dd ?
hdc dd ?
hInst dd ?
hWnd dd ?
ps PAINTSTRUCT<>
msg MSG<>
wc WNDCLASS<>
.code
main:
call GetModuleHandleA,0
mov [hInst],eax
mov wc.lpszClassName,offset myclassname
mov eax,[hInst]
mov wc.hInstance,eax
mov wc.lpfnWndProc,offset WinMsgProc
call LoadIconA,0,IDI_APPLICATION
mov wc.hIcon,eax
call LoadCursorA,0,IDC_IBEAM
mov wc.hCursor,eax
mov wc.lpszMenuName,0
call GetStockObject,WHITE_BRUSH
mov wc.hbrBackground,eax
mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_GLOBALCLASS
mov wc.cbClsExtra,0
mov wc.cbWndExtra,0
call RegisterClassA,offset wc ;注册窗口类
;建立窗口
call CreateWindowExA,0,offset myclassname,offset Caption,WS_OVERLAPPEDWINDOW,100,100,450,300,0,0,[hInst],0
mov [hWnd],eax
call ShowWindow,[hWnd],1
call UpdateWindow,[hWnd]
MsgLoop: ;消息循环
call GetMessageA,offset msg,0,0,0
or eax,eax
jz Exit
call TranslateMessage,offset msg
call DispatchMessageA,offset msg
jmp MsgLoop
Exit:
call ExitProcess,0
;********************************************************
;消息处理函数
WinMsgProc proc uses ebx edi esi, handle:DWORD, wMsg:DWORD, wParam1:DWORD, lParam1:DWORD
cmp wMsg,WM_CREATE
jz create
cmp wMsg,WM_DESTROY
jz destory
cmp wMsg,WM_MOUSEMOVE
jz mousemove
cmp wMsg,WM_PAINT
jz paint
jmp default
create:
xor eax,eax
ret
destory:
call PostQuitMessage,0
xor eax,eax
ret
mousemove: ;鼠标移动,显示鼠标位置
mov eax,lParam1
movzx ebx,ax
shr eax,16
call _wsprintfA,offset string,offset stringTmp,ebx,eax
push eax
call GetDC,handle
mov hdc,eax
call TextOutA,hdc,10,210,offset string
call ReleaseDC,handle,hdc
add esp,4*4
xor eax,eax
ret
paint: ;画矩形
;建立画刷
call CreateHatchBrush,HS_FDIAGONAL,000000
mov hBrsh0,eax
call CreateSolidBrush,00000ffH
mov hBrsh1,eax
call CreateSolidBrush, 00ff00H
mov hBrsh2,eax
call CreateHatchBrush,HS_HORIZONTAL,0ff0000H
mov hBrsh3,eax
call BeginPaint,handle,offset ps
mov hdc,eax
;画矩形
call SelectObject,hdc,hBrsh0
call Rectangle,hdc,10,10,200,200
call SelectObject,hdc,hBrsh1
call Rectangle,hdc,110,10,200,200
call SelectObject,hdc,hBrsh2
call Rectangle,hdc,210,10,400,200
call SelectObject,hdc,hBrsh3
call Rectangle,hdc,310,10,400,200
call EndPaint,handle,offset ps
;删除画刷
call DeleteObject,hBrsh0
call DeleteObject,hBrsh1
call DeleteObject,hBrsh2
call DeleteObject,hBrsh3
xor eax,eax
ret
default:
call DefWindowProcA,handle,wMsg,wParam1,lParam1
ret
WinMsgProc endp
end main
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?