;运行效果
;win32汇编环境,在窗口程序中画简单图形
;展示点、直线、圆、椭圆、正方形、矩形、圆角矩形的画法
;将代码复制进radasm软件里,直接编译可运行.重要部分加备注。
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include gdi32.inc
includelib gdi32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.DATA
ClassName db "SimpleWinClass",0
AppName db "窗口程序中画简单图形",0
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;颜色宏
RGB macro red,green,blue
xor eax,eax
mov ah,blue
shl eax,8
mov ah,green
mov al,red
endm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.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 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
invoke CreateWindowEx,NULL, ADDR ClassName,ADDR AppName,WS_CAPTION or WS_SYSMENU or WS_MINIMIZEBOX ,100,100,270,470, NULL,NULL,hInst, NULL
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow
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 @stPs:PAINTSTRUCT
LOCAL @stRect:RECT
LOCAL @hDc
LOCAL @RGB
LOCAL @hPen,@oldhPen
LOCAL @hBrush,@oldhBrush
.if uMsg == WM_PAINT ;主要在WM_PAINT消息里面画,因为需要重绘,比如最小化后再恢复原状,就是由它恢复的
invoke BeginPaint,hWnd,addr @stPs ;得到设备,就是屏幕,才能显示出来
mov @hDc,eax
invoke SetPixel,@hDc,30,20,0 ;画5个点
invoke SetPixel,@hDc,35,20,0
invoke SetPixel,@hDc,40,20,0
invoke SetPixel,@hDc,45,20,0
invoke SetPixel,@hDc,50,20,0
invoke MoveToEx,@hDc,30,30,0 ;画直线
invoke LineTo,@hDc,220,30
invoke Ellipse,@hDc,30,60,80,110 ;画圆,参数分别是X1.Y1,X2,Y2,距离一样就是圆,不一样就是椭圆
invoke RoundRect,@hDc,100,60,150,110,0,0 ;画正方形,X与Y距离一样就是正方形,否则是矩形
invoke RoundRect,@hDc,170,60,220,110,20,20 ;画圆矩形,20分别是生成圆角的椭圆的宽度和高度
;下面的画法加颜色
RGB 255,0,0
mov @RGB,eax ;得到颜色
invoke CreatePen,PS_SOLID,1,@RGB ;创建新画笔,就是用来画后面形状的
mov @hPen,eax
invoke SelectObject,@hDc,@hPen ;把画笔塞进去,在它被顶替之前一直用这个
mov @oldhPen,eax ;保留原来的画笔,即前面函数的返回值eax,后面恢复原状时使用
invoke SetPixel,@hDc,30,150,@RGB ;画5个点
invoke SetPixel,@hDc,35,150,@RGB
invoke SetPixel,@hDc,40,150,@RGB
invoke SetPixel,@hDc,45,150,@RGB
invoke SetPixel,@hDc,50,150,@RGB
invoke MoveToEx,@hDc,30,160,0
invoke LineTo,@hDc,220,160
invoke Ellipse,@hDc,30,190,80,240
invoke RoundRect,@hDc,100,190,150,240,0,0
invoke RoundRect,@hDc,170,190,220,240,20,20
;下面的画法加刷子,就是填充里面的颜色
RGB 0,178,238
mov @RGB,eax
invoke CreateSolidBrush,@RGB
mov @hBrush,eax
invoke SelectObject,@hDc,@hBrush ;把画刷塞进去,在它被顶替之前一直用这个
mov @oldhBrush,eax
invoke Ellipse,@hDc,30,320,80,370
invoke RoundRect,@hDc,100,320,150,370,0,0
invoke RoundRect,@hDc,170,320,220,370,20,20
invoke SelectObject,@hDc,@oldhPen ;恢复原来的画笔
invoke SelectObject,@hDc,@oldhBrush ;恢复原来的画刷
invoke DeleteObject,@hPen ;删除自创的画笔
invoke DeleteObject,@hBrush ;删除自创的画刷
invoke EndPaint,hWnd,addr @stPs
.elseif uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start