win32汇编环境,窗口程序中使用GDI+显示jpg图片

;下图为要显示的jpg格式图片

编译后显示效果

bwf 

;附源码


; 显示一张400*400的jpg图片,可以随便找一张jpg格式图片放到文件夹里面
; 下面所有代码直接复制至RadAsm里,同时把gdiplus.inc、gidplus.lib文件复制到文件夹里面,直接编译就可以运行了。重要部分加有备注,研究一下就明白了。
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        .386
        .model flat,stdcall
        option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include        windows.inc
include        gdi32.inc
includelib    gdi32.lib
include        user32.inc
includelib    user32.lib
include        kernel32.inc
includelib    kernel32.lib
include         gdiplus.inc     ;GDI+头文件
includelib      gdiplus.lib     ;GDI+库文件

;缺少gdiplus.inc、gdiplus.lib、gdiplus.dll文件的,可去下载win32汇编环境下相关文件

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
GdiplusStartupInput struct
GdiplusVersion            dword ?   ; // 指定 GDI+ 的版本。 必须为 1。
DebugEventCallback        dword ?   ; // 指向 GDI+ 可以在调试版本中调用的回调函数的指针,用于断言和警告。 默认值为 NULL。
SuppressBackgroundThread  dword ?   ; // 指定是否取消 GDI+ 后台线程的布尔值。默认值为 FALSE。
SuppressExternalCodecs    dword ?   ; // 指定是否希望 GDI+ 取消外部图像编解码器的布尔值。默认值为 FALSE。
GdiplusStartupInput ends
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        .data?

hInstance    dd        ?
hWinMain    dd        ?

gptoken    dd ?  ;定义TOKEN的接收变量
pGraphics dd ?  ; 用于存储创建的Graphics对象指针

        .const

szClassName     db    'MyClass',0
szCaptionMain    db    'win32汇编环境下显示一张jpg图片',0

pngPath         db "a.jpg",0 ; 这里替换为实际的jpg文件路径

                .data
gpstart GdiplusStartupInput <1,0,0,0>;定义这个结构并初始化,GDI+初始化函数GdiplusStartup使用的结构
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        .code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 窗口过程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcWinMain    proc    uses ebx edi esi,hWnd,uMsg,wParam,lParam
        LOCAL    @stPs:PAINTSTRUCT
        LOCAL    @stRect:RECT
        LOCAL    @hDc
                ;测试GDI+用
        LOCAL    @szBuffer[100]:byte
        LOCAL   @gdiplusBitmap
        LOCAL   @graphics
                
        mov    eax,uMsg
;********************************************************************
        .if    eax ==    WM_PAINT
                                ;测试GDI+用
                                invoke BeginPaint,hWnd,addr @stPs
                                ;刷新客户区的代码
                                mov @hDc,eax
                                invoke  GdiplusStartup,offset gptoken,offset gpstart,NULL  ;初始化使用GDI+的环境
                                                                                                      

                ;绘图代码

                               INVOKE MultiByteToWideChar,CP_ACP,0,addr pngPath,6,addr @szBuffer,100    ;下面GdipCreateBitmapFromFile函数的第一个参数是文件路径,但是需Unicode码,即宽字符表达的路径,所以必须转换先,6是a.jpg文件名加结束符有6个字节,100是上面设定的@szBuffer的字节数
                               INVOKE GdipCreateBitmapFromFile, addr @szBuffer, ADDR @gdiplusBitmap
                               INVOKE GdipCreateFromHDC, @hDc, ADDR @graphics   ;创建 Graphics 对象

                               invoke GdipDrawImageRectI,@graphics,@gdiplusBitmap,0,0,400,400  ;调整数值可以改变显示位置或缩放
                               
                               INVOKE GdipDeleteGraphics, @graphics
                               INVOKE GdipDisposeImage, @gdiplusBitmap          ;删除 Graphics 对象
                               
                               invoke GdiplusShutdown,gptoken                  ;关闭GDI+的环境
                                invoke EndPaint,hWnd,addr @stPs 
;********************************************************************
        .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,420,430,\
            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:
        call    _WinMain
        invoke    ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        end    start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一品人家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值