介绍 这个代码片段说明了如何捕捉桌面或任何窗口与最小编码BMP文件。它也演示了一个简洁的方式保存设备上下文(DC)硬盘上的文件。 代码的解释 捕获窗口中,我们可以利用建立在Windows API函数。得到一个可用的函数列表在此API,使用API浏览器工具,可以添加到Visual studio插件管理器。这里是一些函数的列表从API使用: GetWindowDc -检索设备上下文的直流CreateCompatibleDc -在内存中创建设备上下文BitBlt执行像素级数据传输从一个直流到其他 如何使用API函数在Visual Basic吗? API函数必须在使用之前声明。最好的方法是声明所需的所有功能模块文件,以便它们可以访问任何形式。声明的例子: 隐藏,复制Code
Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Long) As Long
声明的语法的其他功能,请使用“API查看器”工具。 代码算法在简短的解释 计算窗口的宽度和高度复制检索设备上下文窗口创建一个新的设备上下文的称为直流(DC)使用API函数(CreateCompatibleDC)在内存中创建一个位图对象使用API在新创建的DCCopy图像数据从内存窗口DC DC使用API函数BitBlt从位图对象,创建Ole图片对象使用VB函数saveimage Olepicture保存到文件 隐藏,收缩,复制Code
Public Function GetWindowScreenshot_ (WndHandle As Long, SavePath As String, Optional BringFront As Integer = 1) As Long ' ' Function to create screeenshot of specified window and store at specified path ' On Error GoTo ErrorHandler Dim hDCSrc As Long Dim hDCMemory As Long Dim hBmp As Long Dim hBmpPrev As Long Dim WidthSrc As Long Dim HeightSrc As Long Dim Pic As PicBmp Dim IPic As IPicture Dim IID_IDispatch As guid Dim rc As RECT Dim pictr As PictureBox 'Bring window on top of all windows if specified If BringFront = 1 Then BringWindowToTop WndHandle 'Get Window Size GetWindowRect WndHandle, rc WidthSrc = rc.Right - rc.Left HeightSrc = rc.Bottom - rc.Top 'Get Window device context hDCSrc = GetWindowDC(WndHandle) 'create a memory device context hDCMemory = CreateCompatibleDC(hDCSrc) 'create a bitmap compatible with window hdc hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc) 'copy newly created bitmap into memory device context hBmpPrev = SelectObject(hDCMemory, hBmp) 'copy window window hdc to memory hdc Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _ hDCSrc, 0, 0, vbSrcCopy) 'Get Bmp from memory Dc hBmp = SelectObject(hDCMemory, hBmpPrev) 'release the created objects and free memory Call DeleteDC(hDCMemory) Call ReleaseDC(WndHandle, hDCSrc) 'fill in OLE IDispatch Interface ID With IID_IDispatch .data1 = &H20400 .data4(0) = &HC0 .data4(7) = &H46 End With 'fill Pic with necessary parts With Pic .Size = Len(Pic) 'Length of structure .Type = vbPicTypeBitmap 'Type of Picture (bitmap) .hBmp = hBmp 'Handle to bitmap .hPal = 0& 'Handle to palette (may be null) End With 'create OLE Picture object Call OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic) 'return the new Picture object SavePicture IPic, SavePath GetWindowScreenshot = 1 Exit Function ErrorHandler: GetWindowScreenshot = 0 End Function
更图形相关的代码 更多的易于使用的和免费的代码片段在VB 6断开连接;。 下载源代码和演示应用程序 演示应用程序的完整源代码可以从这里下载。 本文转载于:http://www.diyabc.com/frontweb/news2340.html