日期:2011-08-03  浏览次数:20397 次

在VB6和VB.NET中进行图象捕获

VB6中进行图象捕获

 

'----------------------------------------------------------------------------

'

'Author:lihonggen0

'Date:2002-6-19

'功能:抓屏

'----------------------------------------------------------------------------

Private Type POINTAPI

    x As Long

    y As Long

End Type

Private 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

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long

Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

 

Private Sub Command1_Click()

    Dim hdc As Long

    Dim sw As Integer

    Dim sh As Integer

    Dim CurPos As POINTAPI

    Dim Cur As Long

    Me.Hide

    DoEvents

    Picture1.AutoRedraw = True

    hdc = GetDC(0)

    GetCursorPos CurPos

    Cur = GetCursor

    Picture1.Width = Screen.Width

    Picture1.Height = Screen.Height

    sw = Screen.Width / Screen.TwipsPerPixelX

    sh = Screen.Height / Screen.TwipsPerPixelY

    BitBlt Picture1.hdc, 0, 0, sw, sh, hdc, 0, 0, vbSrcCopy

    Me.Show

    DrawIcon Picture1.hdc, CurPos.x - 10, CurPos.y - 10, Cur

    ReleaseDC 0, hdc

    Picture1.AutoRedraw = False

 

End Sub

'参考

'http://support.microsoft.com/?kbid=210108
http://support.microsoft.com/?id=161299


 

VB.NET中进行图象捕获 ,需要先引用一些API,以下是声明:

    Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer

    Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer

    Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer

    Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer,