能力值:
( LV12,RANK:530 )
2 楼
你写的程序我基本没看懂,可能是我的水平有问题,不过,最近我也正好在学Win32汇编,看到过一个屏幕截取的例子,是全屏幕的截取,源代码如下,你可以参考一下!
工程名Computer
Computer.Rc
#define IDR_MENU 101
#define IDM_CAPTURE 40001
#define IDM_EXIT 40002
IDR_MENU MENU
BEGIN
POPUP "抓图操作"
BEGIN
MENUITEM "全屏幕抓图",IDM_CAPTURE
MENUITEM "退出",IDM_EXIT
END
END
Computer.Asm
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
include kernel32.inc
include gdi32.inc
includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib
WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
.const
IDR_MENU equ 101
IDM_CAPTURE equ 40001
IDM_EXIT equ 40002
.data
ClassName db 'WinClass',0
TitleName db '全屏幕抓图',0
Display db 'display',0
.data?
hInstance HINSTANCE ?
hdcScreen HDC ?
hdcCompatible HDC ?
hbmScreen HBITMAP ?
bm BITMAP <>
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,NULL,NULL,NULL,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 or CS_DBLCLKS
mov wc.lpfnWndProc,offset WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
mov eax,hInstance
mov wc.hInstance,eax
invoke GetStockObject,WHITE_BRUSH
mov wc.hbrBackground,eax
mov eax,IDR_MENU
mov wc.lpszMenuName,eax
mov wc.lpszClassName,offset ClassName
invoke LoadIcon,NULL,IDI_ASTERISK
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_HAND
mov wc.hCursor,eax
invoke RegisterClassEx,addr wc
invoke CreateWindowEx,WS_EX_CLIENTEDGE+WS_EX_APPWINDOW+WS_EX_TOPMOST,addr ClassName,\
addr TitleName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,400,200,\
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 hdc:HDC
LOCAL x:DWORD
LOCAL y:DWORD
.if uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.elseif uMsg==WM_COMMAND
mov eax,wParam
.if ax==IDM_CAPTURE
invoke CreateDC,addr Display,NULL,NULL,NULL
mov hdcScreen,eax
invoke CreateCompatibleDC,eax
mov hdcCompatible,eax
invoke GetDeviceCaps,hdcScreen,HORZRES
mov x,eax
invoke GetDeviceCaps,hdcScreen,VERTRES
mov y,eax
invoke CreateCompatibleBitmap,hdcScreen,x,y
mov hbmScreen,eax
invoke SelectObject,hdcCompatible,eax
invoke ShowWindow,hWnd,SW_HIDE
invoke BitBlt,hdcCompatible,0,0,x,y,hdcScreen,0,0,SRCCOPY
invoke GetObject,hbmScreen,sizeof BITMAP,addr bm
invoke GetDC,hWnd
mov hdc,eax
invoke ShowWindow,hWnd,SW_SHOW
invoke StretchBlt,hdc,0,0,bm.bmWidth,bm.bmHeight,hdcCompatible,0,0,x,y,SRCCOPY
invoke ReleaseDC,hWnd,hdc
.elseif ax==IDM_EXIT
invoke DestroyWindow,hWnd
.endif
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
end start
能力值:
( LV2,RANK:10 )
4 楼
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
include kernel32.inc
include gdi32.inc
includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib .data
BitmapName db "MyBitMap",0
FileName db "c:\a.bmp",0
Display db "Display",0
szText db "szText",0
szText1 db "szText1",0
szText2 db "szText2",0
szText3 db "szText3",0
szCaption db "szCaption",0
.data?
hInstance dd ?
hBmp dd ?
.code
_GetCaptureBmp proc uses ebx esi edi
LOCAL @hDc,@MemDc,@Data
LOCAL @bi:BITMAPINFO
invoke RtlZeroMemory,addr @bi,sizeof @bi
mov ebx,sizeof BITMAPINFO
mov @bi.bmiHeader.biSize,ebx
invoke GetSystemMetrics,SM_CXSCREEN
mov @bi.bmiHeader.biWidth,eax
invoke GetSystemMetrics,SM_CYSCREEN
mov @bi.bmiHeader.biHeight,eax
mov @bi.bmiHeader.biPlanes,1
mov @bi.bmiHeader.biBitCount,24
invoke GetDC,NULL
mov @hDc,eax
invoke CreateCompatibleDC,@hDc
mov @MemDc,eax
invoke CreateDIBSection,@MemDc,addr @bi,DIB_RGB_COLORS,addr @Data,NULL,0
mov hBmp,eax
invoke SelectObject,@MemDc,hBmp
invoke BitBlt,@MemDc, 0, 0, @bi.bmiHeader.biWidth, @bi.bmiHeader.biHeight,@hDc, 0, 0, SRCCOPY
invoke ReleaseDC,NULL, @hDc
invoke DeleteDC,@MemDc
xor eax,eax
ret
_GetCaptureBmp endp
_SaveBitmapToFile proc uses ebx esi edi _hBmp
LOCAL @hDc;设备描述表
LOCAL @iBits;当前显示分辨率下每个像素所占字节数
LOCAL @wBitCount;位图中每个像素所占字节数
LOCAL @dwPaletteSize;定义调色板大小
LOCAL @dwBmBitsSize;位图中像素字节大小
LOCAL @dwDIBSize; 位图文件大小
LOCAL @dwWritten;写入文件字节数
LOCAL @fh;定义文件句柄
LOCAL @hDib;分配内存句柄
LOCAL @hPal;分配内存句柄
LOCAL @hOldPal;调色板句柄
LOCAL @Bitmap:BITMAP;位图结构
LOCAL @bmfHdr:BITMAPFILEHEADER;位图属性结构
LOCAL @bi:BITMAPINFOHEADER;位图文件头结构
LOCAL @lpbi; 指向位图信息头结构
LOCAL @Temp2
;计算位图文件每个像素所占字节数
invoke GetStockObject,DEFAULT_PALETTE
mov @hPal,eax
invoke CreateDC, offset Display,NULL,NULL,NULL
mov @hDc,eax
invoke GetDeviceCaps,@hDc, BITSPIXEL
mov ebx,eax
invoke GetDeviceCaps,@hDc, PLANES
mul ebx
mov @iBits,eax
invoke DeleteDC,@hDc
mov @wBitCount,4;简化处理
;.if @wBitCount<=8
;计算调色板大小
;dwPaletteSize = (1 < < wBitCount) *sizeof(RGBQUAD)
mov eax,1
shl eax,4 ;1左移@wBitCount位
mov ebx,sizeof RGBQUAD
mul ebx
mov @dwPaletteSize,eax
;.endif
;设置位图信息头结构
invoke GetObject, _hBmp, sizeof BITMAP, addr @Bitmap
.if eax==0
invoke MessageBox,NULL,offset szText3,offset szCaption,MB_OK
invoke ExitProcess,NULL
.endif
mov ebx,sizeof BITMAPINFOHEADER
mov @bi.biSize,ebx
mov ebx,@Bitmap.bmWidth
mov @bi.biWidth,ebx
mov ebx,@Bitmap.bmHeight
mov @bi.biHeight,ebx
mov @bi.biPlanes,1
mov ebx,@wBitCount
mov @bi.biBitCount,bx ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov @bi.biCompression,BI_RGB
mov @bi.biSizeImage,0
mov @bi.biXPelsPerMeter,0
mov @bi.biYPelsPerMeter,0
mov @bi.biClrUsed,0
mov @bi.biClrImportant,0
;dwBmBitsSize = ((Bitmap.bmWidth *wBitCount+31)/32)* 4*Bitmap.bmHeight
xor edx,edx
mov eax,@Bitmap.bmWidth
mul @wBitCount
add eax,31
mov ebx,32
div ebx
mov ebx,4
mul ebx
mul @Bitmap.bmHeight
mov @dwBmBitsSize,eax
;为位图内容分配内存
mov ebx,@dwBmBitsSize
add ebx,@dwPaletteSize
add ebx,sizeof BITMAPINFOHEADER invoke GlobalAlloc,GHND,ebx
mov @hDib,eax
invoke GlobalLock,@hDib
mov @lpbi,eax
;*lpbi=bi
;invoke RtlFillMemory,@lpbi,sizeof @bi, addr @bi
; lea ebx,@bi ;; mov eax,@lpbi
; mov @lpbi,ebx ;; mov [eax],@bi
;mov eax,@lpbi
;mov ecx,dword ptr [@bi]
;mov dword ptr [eax],ecx
;lea esi,@bi
;mov edi,@lpbi
; mov eax,[esi]
; mov [edi],eax
;;rep movs dword ptr [edi],dword ptr [esi]
;处理调色板
mov ecx, 0Ah
lea esi,@bi.biSize
mov edi,@lpbi
rep movsd ;*lpbi = bi
.if @hPal
invoke GetDC,NULL
mov @hDc,eax
invoke SelectPalette,@hDc,@hPal,FALSE ;返回了0
mov @hOldPal,eax
invoke RealizePalette,@hDc
.endif
;获取该调色板下新的像素值
;GetDIBits(hDCC, hBmp, 0, (UINT) Bitmap.bmHeight,
;(LPSTR)lpbi + sizeof(BITMAPINFOHEADER)+dwPaletteSize,
;(LPBITMAPINFO)lpbi, DIB_RGB_COLORS)
xor ebx,ebx
mov ebx,@lpbi
add ebx,sizeof BITMAPINFOHEADER
add ebx,@dwPaletteSize
mov @Temp2,ebx
invoke GetDIBits,@hDc,_hBmp,0,@Bitmap.bmHeight,@Temp2,@lpbi,DIB_RGB_COLORS
mov @Temp2,eax
.if @Temp2==0
;invoke MessageBox,NULL,offset szText2,offset szCaption,MB_OK
;invoke ExitProcess,NULL
.endif
;恢复调色板
.if @hOldPal
invoke SelectPalette,@hDc,@hOldPal,TRUE
invoke RealizePalette,@hDc
invoke ReleaseDC,NULL,@hDc
.endif
;创建位图文件
;invoke CreateFile,FileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN,NULL
invoke CreateFile,addr FileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, NULL
mov @fh,eax
.if @fh==INVALID_HANDLE_VALUE
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
invoke ExitProcess,NULL
.endif
;设置位图文件头
mov @bmfHdr.bfType,4D42h; "BM"
;dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ dwPaletteSize + dwBmBitsSize
;xor eax,eax
mov ebx,sizeof BITMAPFILEHEADER
add ebx,sizeof BITMAPINFOHEADER
add ebx,@dwPaletteSize
add ebx,@dwBmBitsSize
mov @dwDIBSize,ebx
mov @bmfHdr.bfSize,ebx
mov @bmfHdr.bfReserved1,0
mov @bmfHdr.bfReserved2,0
;bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER)+ dwPaletteSize
;xor eax,eax
mov ebx,sizeof BITMAPFILEHEADER
add ebx,sizeof BITMAPINFOHEADER
add ebx,@dwPaletteSize
mov @bmfHdr.bfOffBits,ebx
;写入位图文件头
;WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL)
invoke WriteFile,@fh,addr @bmfHdr,sizeof BITMAPFILEHEADER,addr @dwWritten,NULL
;写入位图文件其余内容
;WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL)
invoke WriteFile,@fh,@lpbi,@dwDIBSize,addr @dwWritten,NULL
;清除
invoke GlobalUnlock,@hDib
invoke GlobalFree,@hDib
invoke CloseHandle,@fh
ret
_SaveBitmapToFile endp
start:
invoke _GetCaptureBmp
invoke _SaveBitmapToFile,hBmp
invoke ExitProcess,0
end start