GDI+是GDI的下一个版本,它进行了很好的改进,并且易用性更好。那么如何在Win32汇编中去使用GDI+呢?
在Win32汇编中使用GDI+,我们首先得导入GDI+的头文件gdiplus.inc和库gdiplus.lib。使用GDI+ API之前,必须先调用GdiplusStartup这个函数,作用是初始化GDI+函数库。GdiplusStartup函数的使用方法是:
invoke GdiplusStartup,hToken,GpInput,GpOutput
函数的第一个参数hToken指向一个DWORD变量,用来保存GDI+会话的一个句柄,这个句柄在后面释放GDI+资源的时候会用到。第二个参数GpInput指向结构体GdiplusStartupInput,该结构体的定义如下:
GdiplusStartupInput struct
GdiplusVersion dword ?
;指定GDI+的版本,必须为1
DebugEventCallback dword ?
;默认为0,一个用于调试的回调函数。
SuppressBackgroundThread dword ?
;默认为0,除非要是用挂钩函数和解勾函数
SuppressExternalCodecs dword ?
;指定是否让GDI+压缩外部图片codecs的布尔值。1.0不支持该功能,所以该值被忽略。
GdiplusStartupInput ends
一般GdiplusStartupInput初始化成1,0,0,0即可,并且这时候这个函数的最后一个参数GdiplusStartupOutput可以忽略,直接用NULL就可以。
在程序结束时调用GdiplusShutdown函数,释放GDI+的资源。GdiplusShutdown函数的用法是:
invoke GdiplusShutdown,hToken
下面举一个在Win32ASM中使用的例子,是在窗体中使用绘制一个黑色的矩形。
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;include文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include gdi32.inc
includelib gdi32.lib
include gdiplus.inc
includelib gdiplus.lib
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
GpInput GdiplusStartupInput<1,0,0,0>
.data?
hInstance dd ?
hWinMain dd ?
hToken dd ?
.const
szClassName db 'MyClass',0
szCaptionMain db 'GDI+!',0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;窗口过程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcWinMain proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
local @gdip
local @hBrush
mov eax,uMsg
;*******************************************************************************************
.if eax == WM_PAINT
mov ebx,hWnd
.if ebx == hWinMain
invoke GdipCreateFromHWND,hWnd,addr @gdip
invoke GdipCreateSolidFill,ColorsBlack,addr @hBrush
invoke GdipFillRectangleI,@gdip,@hBrush,10,10,160,240
invoke GdipDeleteBrush,@hBrush
invoke GdipDeleteGraphics,@gdip
.endif
;********************************************************************************************
.elseif eax == WM_CLOSE
invoke DestroyWindow,hWinMain
invoke PostQuitMessage,NULL
;*******************************************************************************************
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
;********************************************************************************************
xor eax,eax
ret
_ProcWinMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_WinMain proc
local @stWndClass:WNDCLASSEX
local @stMsg:MSG
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
;*******************************************************************************************
;注册窗口类
;*******************************************************************************************
invoke LoadCursor,0,IDC_ARROW
mov @stWndClass.hCursor,eax
push hInstance
pop @stWndClass.hInstance
mov @stWndClass.cbSize,sizeof WNDCLASSEX
mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
mov @stWndClass.lpfnWndProc,offset _ProcWinMain
mov @stWndClass.hbrBackground,COLOR_WINDOW+1
mov @stWndClass.lpszClassName,offset szClassName
invoke RegisterClassEx,addr @stWndClass
;*******************************************************************************************
;建立并显示窗口
;*******************************************************************************************
invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,\
offset szCaptionMain,WS_OVERLAPPEDWINDOW,100,100,200,300,\
NULL,NULL,hInstance,NULL
mov hWinMain,eax
invoke ShowWindow,hWinMain,SW_SHOWNORMAL
invoke UpdateWindow,hWinMain
;*******************************************************************************************
;消息循环
;*******************************************************************************************
.while TRUE
invoke GetMessage,addr @stMsg,NULL,0,0
.break .if eax == 0
invoke TranslateMessage,addr @stMsg
invoke DispatchMessage,addr @stMsg
.endw
ret
_WinMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GdiplusStartup,offset hToken,offset GpInput,NULL
call _WinMain
invoke GdiplusShutdown,hToken
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
代码在窗体消息处理程序中的窗体绘制消息中使用GDI+绘图。首先使用函数GdipCreateFromHWND从窗体的句柄中创建一个Graphics的句柄,接着使用GdipCreateSolidFill创建一个画刷,再使用GdipFillRectangleI函数,用创建的Graphics的句柄以及创建的画刷和位置信息绘制了一个矩形。绘制完成后,调用GdipDeleteBrush释放画刷,调用GdipDeleteGraphics释放Graphics资源。
具体的GDI+的一些预定义值以及结构体和函数原型以及参数说明,可以参照GDIPLUS.INC for PB/WIN part 1,虽然这个是用于PowerBASIC,但是经过小的改动也能用于Win32ASM。
同时GDI+ 的一些绘制函数有分为带I的版本和不带I的版本,例如GdipFillRectangleI和GdipFillRectangle两个版本,但是好像只有GdipFillRectangleI才能绘制成功,不知道有什么区别。