win32汇编环境,窗口程序显示bmp图像文件
;运行效果
;win32汇编环境,窗口程序显示bmp图像文件
;显示的是一张尺寸236*318的bmp位图,及一张缩小为原来三分之一的位图
;将代码复制进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 "窗口程序显示bmp位图文件",0
szFileName01 db "a.bmp",0
szErrOpenFile db "无法打开文件!",0
szCheshi db "测试",0
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;代码区
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.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_OVERLAPPEDWINDOW,100,100,400,600, 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 @hDc,@hdcMem
LOCAL @hbmpBack:HBITMAP
LOCAL @hOldBmp:HBITMAP
.if uMsg == WM_CREATE
.elseif uMsg == WM_PAINT
invoke BeginPaint,hWnd,addr @stPs
mov @hDc,eax
;刷新客户区的代码
invoke CreateCompatibleDC,@hDc ;依据屏幕显示DC创建内存DC设备描述表句柄
mov @hdcMem,eax
invoke LoadImage,NULL,addr szFileName01,IMAGE_BITMAP,0,0,LR_LOADFROMFILE + LR_DEFAULTSIZE + LR_CREATEDIBSECTION
mov @hbmpBack,eax
.if eax == NULL
invoke MessageBox,hWnd,addr szErrOpenFile,addr szCheshi,MB_OK or MB_ICONINFORMATION ;如果弹出这个指示,大概率是图像有问题,建议把图像用ACD转成BMP,不要用网上在线转换,有时会少字节,不完整
.endif
invoke SelectObject,@hdcMem,@hbmpBack ;位图先放入兼容的设备上下文中(兼容DC)
mov @hOldBmp,eax
invoke SelectObject,@hdcMem,@hbmpBack ;位图句柄hbmpBack放到hdcMem中,236是图像宽度像素,318是高度像素
invoke BitBlt,@hDc,70,30,236,318,@hdcMem,0,0,SRCCOPY ;将兼容上下文的内容拷贝到设备上下文,即显示出来,不可拉伸
invoke SetStretchBltMode,@hDc,HALFTONE ;设置拉伸模式
invoke StretchBlt,@hDc,150,400,78,106,@hdcMem,0,0,236,318,SRCCOPY ;拉伸为原尺寸的三分之一
invoke SelectObject,@hdcMem,@hOldBmp ;删除现在使用的位图句柄,清除现场
invoke DeleteDC,@hdcMem ;销毁临时的内存DC
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