Results 1 to 2 of 2

Thread: [RESOLVED] Wierd Screen Caputre...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Wierd Screen Caputre...

    I've made a screen shot program... but there's a strange problem with it...
    There's an option to capture the focused window.

    When the event fires it calls this function. The function captures the correct amount of space (The Windows Height and the Windows Length).

    But for some reason that i can't figure out, it doesn't get the Top and the Left correctly, when the function is called, these are both 0 & i end up capturing the top left of the screen with the focused window's size (Height and Width). I'm pretty sure it's my code, as i didn't create this code, rather got it off this forum site about 12 months ago and frankensteined it together.

    Don't mind the commented stuff, it was just me trying to resolve the error.

    Code:
    Public xpos As Long
    Public ypos As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
    
    '##############^^^ CAPTURE SCREEN ^^^################
    
    
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowRect Lib "user32" _
        (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    Public Sub Screen_Shot_Event(Picture_ As PictureBox, Optional Active_Window As Boolean = False, Optional Draw_Cursor As Boolean = False)
    Dim wScreen As Long
    Dim hScreen As Long
    Dim hdcScreen As Long
    Dim w As Long
    Dim h As Long
    
    Dim TmpPic As PictureBox
    Set TmpPic = Picture_
    TmpPic.AutoRedraw = False
    TmpPic.Width = 15000
    TmpPic.Height = 15000
    
    Dim lWnd As Long
    Dim R
    lWnd = GetForegroundWindow()
    
    Picture_.Cls
    wScreen = Screen.Width \ Screen.TwipsPerPixelX
    hScreen = Screen.Height \ Screen.TwipsPerPixelY
    Picture_.ScaleMode = vbPixels
    w = Screen.Width / Screen.TwipsPerPixelX
    h = Screen.Height / Screen.TwipsPerPixelY
    hdcScreen = GetDC(0)
    
    If Draw_Cursor = True Then DrawCursor hdcScreen
    
    If Active_Window = False Then
    Picture_.Width = (Screen.Width)
    Picture_.Height = (Screen.Height)
    'Picture_.AutoRedraw = False
    R = StretchBlt(TmpPic.hdc, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
    'Picture_.AutoRedraw = True
    Else
    If Active_Window = True Then
    Dim rct As RECT
    GetWindowRect lWnd, rct
    'Picture_.AutoRedraw = False
    R = StretchBlt(TmpPic.hdc, rct.Left * -1, rct.Top * -1, rct.Right, rct.Bottom, hdcScreen, 0, 0, rct.Right, rct.Bottom, vbSrcCopy)
    Picture_.Width = (rct.Right * Screen.TwipsPerPixelX) - (rct.Left * Screen.TwipsPerPixelX) + 70
    Picture_.Height = (rct.Bottom * Screen.TwipsPerPixelY) - (rct.Top * Screen.TwipsPerPixelY) + 70
    'Picture_.AutoRedraw = True
    End If
    End If
    Debug.Print ""
    Debug.Print lWnd, hdcScreen, rct.Top
    Set Picture_.Picture = hDCToPicture(hdcScreen, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
    
    End Sub
    Last edited by Slyke; Jun 21st, 2007 at 03:30 AM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Wierd Screen Caputre...

    Don't worry... i got it.

    Code:
    Private Declare Function GetCursorInfo Lib "user32.dll" ( _
        ByRef pci As CURSORINFO _
    ) As Long
    Private Declare Function CopyCursor Lib "user32.dll" Alias "CopyIcon" ( _
        ByVal hCursor As Long _
    ) As Long
    Private Declare Function DestroyCursor Lib "user32.dll" ( _
        ByVal hCursor As Long _
    ) As Long
    Private Declare Function GetIconInfo Lib "user32.dll" ( _
        ByVal hIcon As Long, _
        ByRef piconinfo As ICONINFO _
    ) As Long
    
    Private Declare Function DrawIcon Lib "user32.dll" ( _
        ByVal hdc As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        ByVal hIcon As Long _
    ) As Long
    
    Private Type ICONINFO
        fIcon As Long
        xHotspot As Long
        yHotspot As Long
        hbmMask As Long
        hbmColor As Long
    End Type
    
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Type CURSORINFO
        cbSize As Long
        FLAGS As Long
        hCursor As Long
        pt As POINTAPI
    End Type
    
    '##############^^^ DRAW MOUSE ^^^################
    
    Public xpos As Long
    Public ypos As Long
    Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
    
    '##############^^^ CAPTURE SCREEN ^^^################
    
    
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowRect Lib "user32" _
        (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    
    '##############^^^ ACTIVE WINDOW ^^^################
    
    
    Public Sub Screen_Shot_Event(Picture_ As PictureBox, Optional Active_Window As Boolean = False, Optional Draw_Cursor As Boolean = False)
    Dim wScreen As Long
    Dim hScreen As Long
    Dim hdcScreen As Long
    Dim w As Long
    Dim h As Long
    
    Dim TmpPic As PictureBox
    Set TmpPic = Picture_
    TmpPic.AutoRedraw = False
    TmpPic.Width = 15000
    TmpPic.Height = 15000
    
    Dim lWnd As Long
    Dim R
    lWnd = GetForegroundWindow()
    
    Picture_.Cls
    wScreen = Screen.Width \ Screen.TwipsPerPixelX
    hScreen = Screen.Height \ Screen.TwipsPerPixelY
    Picture_.ScaleMode = vbPixels
    w = Screen.Width / Screen.TwipsPerPixelX
    h = Screen.Height / Screen.TwipsPerPixelY
    hdcScreen = GetDC(0)
    
    If Draw_Cursor = True Then DrawCursor hdcScreen
    
    If Active_Window = False Then
    Picture_.Width = (Screen.Width)
    Picture_.Height = (Screen.Height)
    R = StretchBlt(TmpPic.hdc, 0, 0, w, h, hdcScreen, 0, 0, wScreen, hScreen, vbSrcCopy)
    Else
    If Active_Window = True Then
    Dim rct As RECT
    GetWindowRect lWnd, rct
    R = StretchBlt(TmpPic.hdc, rct.Left, rct.Top, rct.Right, rct.Bottom, hdcScreen, rct.Left, rct.Top, rct.Right, rct.Bottom, vbSrcCopy)
    Picture_.Width = (rct.Right * Screen.TwipsPerPixelX) - (rct.Left * Screen.TwipsPerPixelX) + 70
    Picture_.Height = (rct.Bottom * Screen.TwipsPerPixelY) - (rct.Top * Screen.TwipsPerPixelY) + 70
    Set Picture_.Picture = hDCToPicture(hdcScreen, rct.Left, rct.Top, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
    Exit Sub
    End If
    End If
    Set Picture_.Picture = hDCToPicture(hdcScreen, rct.Left, rct.Top, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
    
    End Sub
    
    Private Sub DrawCursor(ByVal hdc As Long)
        Dim ci As CURSORINFO
        Dim hCursor As Long
        Dim ii As ICONINFO
        
        ci.cbSize = Len(ci)
        If GetCursorInfo(ci) Then
            hCursor = CopyCursor(ci.hCursor)
            If GetIconInfo(hCursor, ii) Then
                Call DrawIcon(hdc, ci.pt.x - ii.xHotspot, _
                    ci.pt.y - ii.yHotspot, hCursor)
            End If
            Call DestroyCursor(hCursor)
        End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width