问题我稍微描述一下我要取得一个程序界面某一块的图像,因为这个程序有时候会被遮住,所以只能用printwindow将截图读进内存
然后用GetDIBits将这个图转换成数组,我就可以根据坐标来取得图像了原理我知道 但是我实在搞不定代码了。
不知道有没有哪位伸出援手帮我一下,都搞了一星期了,纠结啊!

解决方案 »

  1.   

    Option ExplicitPrivate Const BI_RGB = 0&
    Private Const DIB_RGB_COLORS = 0
    Private Const OBJ_BITMAP As Long = 7Private Type RECT
        Left            As Long
        Top             As Long
        Right           As Long
        Bottom          As Long
    End TypePrivate Type BITMAPINFOHEADER
        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 TypePrivate Type RGBQUAD
        rgbBlue         As Byte
        rgbGreen        As Byte
        rgbRed          As Byte
        rgbReserved     As Byte
    End TypePrivate Type BITMAPINFO
        bmiHeader       As BITMAPINFOHEADER
        bmiColors       As RGBQUAD
    End TypePrivate Declare Function GetCurrentObject Lib "gdi32.dll" _
            (ByVal hdc As Long, ByVal uObjectType As Long) As Long
    Private Declare Function GetDIBits Lib "gdi32" _
            (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
    Private Declare Function GetWindowRect Lib "user32.dll" _
            (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
    Private Declare Function DeleteObject Lib "gdi32" _
            (ByVal hObject As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function PrintWindow Lib "user32" _
            (ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" _
            (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function SetDIBitsToDevice Lib "gdi32" _
            (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As LongPrivate bi24BitInfo As BITMAPINFO
    Private aBytes()    As BytePrivate Sub Command1_Click()
        Dim hWndTarget  As Long
        Dim rc          As RECT
        Dim iBitmap     As Long
        Dim iOldObject  As Long    hWndTarget = FindWindow("Notepad", vbNullString)
        If hWndTarget = 0 Then
            Me.Print "NotePad window not found!"
            Exit Sub
        End If    PrintWindow hWndTarget, Me.hdc, 0
        Me.Refresh    GetWindowRect hWndTarget, rc
        With bi24BitInfo.bmiHeader
            .biBitCount = 24
            .biCompression = BI_RGB
            .biPlanes = 1
            .biSize = Len(bi24BitInfo.bmiHeader)
            .biWidth = rc.Right - rc.Left
            .biHeight = rc.Bottom - rc.Top
        End With    ReDim aBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte
        iBitmap = GetCurrentObject(Me.hdc, OBJ_BITMAP)
        GetDIBits Me.hdc, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS
        DeleteObject iBitmap
    End SubPrivate Sub Command2_Click()
        Me.BackColor = vbWhite
        Me.Cls
        SetDIBitsToDevice Me.hdc, 50, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS
    End SubPrivate Sub Form_Load()
        Me.AutoRedraw = True
    End Sub