Resolved Including mouse in screenshot

thirteentwenty

Well-known member
Joined
Oct 9, 2008
Messages
80
Location
Honolulu
Programming Experience
Beginner
OK I seem to recall there being a thread here about this but I couldn't find it... If someone can link me to it cool... if not heres my problem


I've cobbled the below code together to catch screen shots of active windows, but I'd like to include the mouse also... I'm kind of at a loss on how to do so.

with the below code is there something I can add to grab the mouse or can someone just point me in the right direction to get the included in a screenshot.

Thanks =)

VB.NET:
    '/ Helper class containing Gdi32 API functions
    Public Class newClass
        <StructLayout(LayoutKind.Sequential)> Public Structure RECT
            Public left As Integer
            Public top As Integer
            Public right As Integer
            Public bottom As Integer
        End Structure 'RECT

        Public SRCCOPY As Integer = &HCC0020
        ' BitBlt dwRop parameter
        Declare Function BitBlt Lib "gdi32.dll" ( _
            ByVal hDestDC As IntPtr, _
            ByVal x As Int32, _
            ByVal y As Int32, _
            ByVal nWidth As Int32, _
            ByVal nHeight As Int32, _
            ByVal hSrcDC As IntPtr, _
            ByVal xSrc As Int32, _
            ByVal ySrc As Int32, _
            ByVal dwRop As Int32) As Int32

        Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal nWidth As Int32, ByVal nHeight As Int32) As IntPtr
        Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As IntPtr
        Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Int32
        Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As IntPtr) As Int32
        Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
        Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr
        Declare Function GetWindowDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
        Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32
        Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Int32
        Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
        Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
        Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
        Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Integer
    End Class

   Public Function captureWindow(ByVal handle As IntPtr) As Image
        Dim srccopy As Integer = &HCC0020
        Dim hWind As IntPtr = newClass.GetForegroundWindow()
        Dim hdcSrc As IntPtr = newClass.GetWindowDC(hWind)
        Dim windowRect As New newClass.RECT
        newClass.GetWindowRect(hWind, windowRect)
        Dim width As Integer = windowRect.right - windowRect.left
        Dim height As Integer = windowRect.bottom - windowRect.top
        Dim hdcDest As IntPtr = newClass.CreateCompatibleDC(hdcSrc)
        Dim hBitmap As IntPtr = newClass.CreateCompatibleBitmap(hdcSrc, width, height)
        Dim hOld As IntPtr = newClass.SelectObject(hdcDest, hBitmap)
        newClass.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, srccopy)
        newClass.SelectObject(hdcDest, hOld)
        newClass.DeleteDC(hdcDest)
        newClass.ReleaseDC(hWind, hdcSrc)
        Dim img As Image = Image.FromHbitmap(hBitmap)
        newClass.DeleteObject(hBitmap)
        Return img
    End Function

    Public Sub CaptureWindowToFile(ByVal handle As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
        Dim img As Image = captureWindow(handle)
        img.Save(filename, format)
    End Sub

    Private Sub shotTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles shotTimer.Tick
        CaptureWindowToFile(Me.Handle, tempDir & "\window.jpg", Imaging.ImageFormat.Jpeg)
        Me.screenshotPreview.ImageLocation = tempDir & "\window.jpg"
    End Sub
 
Last edited:
Cursor.Draw...
VB.NET:
Public Function CaptureWindow(ByVal handle As IntPtr, Optional ByVal includeCursor As Boolean = True) As Image
    Dim r As RECT
    GetWindowRect(handle, r)
    Dim rct As Rectangle = Rectangle.FromLTRB(r.left, r.top, r.right, r.bottom)
    Dim bmp As New Bitmap(rct.Width, rct.Height)
    Dim g As Graphics = Graphics.FromImage(bmp)
    g.CopyFromScreen(rct.Location, Point.Empty, rct.Size)
    If includeCursor Then
        Dim cur As Cursor = Cursor.Current
        Dim pos As Point = Cursor.Position
        pos.Offset(-rct.X, -rct.Y)
        cur.Draw(g, New Rectangle(pos, cur.Size))
    End If
    g.Dispose()
    Return bmp
End Function
Note that you should not use Optional parameters if you want to port this code to other languages. Also, it could be removed in future versions of language. :)p just kidding)
 
Way awesome! thanks a million!
 
Back
Top