| 发表于:2007-06-20 11:28:374楼 得分:0 |
option explicit public type bitmapfileheader bftype(0 to 1) as byte bfsize as long bfreserved1 as integer bfreserved2 as integer bfoffbits as long end type public type bitmapinfoheader '40 bytes bisize as long biwidth as long biheight as long biplanes as integer bibitcount as integer bicompression as long bisizeimage as long bixpelspermeter as long biypelspermeter as long biclrused as long biclrimportant as long end type public type rgbquad rgbblue as byte rgbgreen as byte rgbred as byte rgbreserved as byte end type public type bitmapinfo bmiheader as bitmapinfoheader bmicolors as rgbquad end type declare function getdc lib "user32 " (byval hwnd as long) as long declare function createcompatibledc lib "gdi32 " (byval hdc as long) as long declare function createdibsection lib "gdi32 " (byval hdc as long, pbitmapinfo as bitmapinfo, byval un as long, lplpvoid as long, byval handle as long, byval dw as long) as long declare function bitblt lib "gdi32 " (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long declare function selectobject lib "gdi32 " (byval hdc as long, byval hobject as long) as long declare function deletedc lib "gdi32 " (byval hdc as long) as long declare function deleteobject lib "gdi32 " (byval hobject as long) as long public declare sub copymemory lib "kernel32 " alias "rtlmovememory " (byval destination as long, byval source as long, byval length as long) public const dib_rgb_colors = 0 ' color table in rgbs public const srccopy = &hcc0020 ' (dword) dest = source '---------------------------- '作者:money 'e-mail:2258773@163.com '涵数功能:拷屏,保存为bmp文件 '成功返回真 '---------------------------- public function copyscreentobmp(byval szfile as string) as boolean dim w as long, h as long dim scrdc as long dim dib as long, m_dc as long dim bmpinfo as bitmapinfo dim bmpfilehead as bitmapfileheader dim pdata as long dim buff() as byte dim old as long dim l as long '取屏幕 高宽 w = screen.width \ 15 h = screen.height \ 15 '准备内存dc m_dc = createcompatibledc(0&) if m_dc = 0 then copyscreentobmp = false exit function end if '填充dib的bmp结构 with bmpinfo.bmiheader .bibitcount = 24 .biplanes = 1 .biheight = h .biwidth = w .bisize = 40 '本结构长度 end with dib = createdibsection(m_dc, bmpinfo, dib_rgb_colors, pdata, 0, 0) '分配内存 if dib = 0 then copyscreentobmp = false exit function end if old = selectobject(m_dc, dib) '拷屏 scrdc = getdc(0) bitblt m_dc, 0, 0, w, h, scrdc, 0, 0, srccopy '分配内存 l = w * h * 3 '补足4的倍数 if l mod 4 <> 0 then l = l + (4 - l mod 4) redim buff(1 to l) as byte '取像素数据 copymemory varptr(buff(1)), pdata, l '释放资源 selectobject m_dc, old deleteobject dib deletedc m_dc '填充bmpfile with bmpfilehead 'bm标志 .bftype(0) = asc( "b "): .bftype(1) = asc( "m ") .bfsize = len(bmpfilehead) + len(bmpinfo) + l .bfoffbits = len(bmpfilehead) + len(bmpinfo) end with '写入文件 l = freefile() open szfile for binary as l '写入文件头 put l, 1, bmpfilehead put l, , bmpinfo '写入实际像素 put l, , buff() close l copyscreentobmp = true end function '__________________________________________ forms option explicit private sub command1_click() dim ok as boolean ok = copyscreentobmp( "c:\test.bmp ") if ok then me.picture = loadpicture( "c:\test.bmp ") else msgbox "cao,拷屏失败了~ " end if end sub '------------------------ private sub form_load() end sub | | |
|