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

类别:.NET开发 点击:0 评论:0 推荐:

 

在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

 

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, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer

 

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

 

    Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer

 

    Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer

 

Const SRCCOPY As Integer = &HCC0020

 

 

将以下代码添加到Button1_Click事件中:

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim hDC, hMDC As Integer

        Dim hBMP, hBMPOld As Integer

        Dim sw, sh As Integer

        hDC = GetDC(0)

        hMDC = CreateCompatibleDC(hDC)

        sw = Screen.PrimaryScreen.Bounds.Width

        sh = Screen.PrimaryScreen.Bounds.Height

 

        hBMP = CreateCompatibleBitmap(hDC, sw, sh)

        hBMPOld = SelectObject(hMDC, hBMP)

        BitBlt(hMDC, 0, 0, sw, sh, hDC, 0, 0, SRCCOPY)

        hBMP = SelectObject(hMDC, hBMPOld)

        PictureBox1.Image = Image.FromHbitmap(New IntPtr(hBMP))

 

        DeleteDC(hDC)

        DeleteDC(hMDC)

        DeleteObject(hBMP)

End Sub

 

 

 

 

本文地址:http://com.8s8s.com/it/it42847.htm