Win32汇编中使用GDI+

  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才能绘制成功,不知道有什么区别。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Visual C++6.0使用GDI+的一般方法 1. 载解压GDI+开发包; 2. 正确设置include & lib 目录; 3. stdafx.h 添加: #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include 4. 程序添加GDI+的包含文件gdiplus.h以及附加的类库gdiplus.lib。 通常gdiplus.h包含文件添加在应用程序的stdafx.h文件,而gdiplus.lib可用两种进行添加: 第一种是直接在stdafx.h文件添加下列语句: #pragma comment( lib, "gdiplus.lib" ) 另一种方法是: 在VC.net添加库文件在:项目菜单->属性->链接器->输入 举个例子: (1)在应用程序项目的应用类,添加一个成员变量,如下列代码: ULONG_PTR m_gdiplusToken; 其,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。 (2)在应用类添加ExitInstance的重载,并添加下列代码用来关闭GDI+: int CGDITestApp::ExitInstance() { Gdiplus::GdiplusShutdown(m_gdiplusToken); return CWinApp::ExitInstance(); } (3)在应用类的InitInstance函数添加GDI+的初始化代码: 注意:下面这些GDI+的初始化代码必须放在m_pMainWnd->UpdateWindow();之前。 CWinApp::InitInstance(); Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); (4)在需要绘图的窗口或视图类添加GDI+的绘制代码。 下面分别就单文档和基于对话框应用程序为例,说明使用GDI+的一般过程和方法。 1. 在单文档应用程序使用GDI+ 在上面的过程,我们就是以一个单文档应用程序Ex_GDIPlus作为示例的。下面列出第4步所涉及的代码: void CGDITestView::OnDraw(CDC* pDC) { CGDITestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here usingnamespace Gdiplus; Graphics graphics(pDC->m_hDC); Pen newPen(Color(255,0,0),3); HatchBrush newBrush(HatchStyleCross,Color(255,0,255,0),Color(255,0,0,255));//创建一个填充画刷,前景色为绿色,背景色为蓝色 graphics.DrawRectangle(&newPen,50,50,100,60);// 在(50,50)处绘制一个长为100,高为60的矩形 graphics.FillRectangle(&newBrush,50,50,100,60); // 在(50,50)处填充一个长为100,高为60的矩形区域 } 编译并运行,结果如图:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值