Results 1 to 5 of 5

Thread: [VB6] - PrintWindow() function

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    [VB6] - PrintWindow() function

    i can copy everything(inclued controls) in a hwd property by using PrintWindows() API function. my problem is how can i copy these image by a size and position(crop the image).
    i did and try that without sucess
    can anyone advice me?
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [VB6] - PrintWindow() function

    Use GetWindowRect to get the size of the window Printwindow is going to capture, set the temp destination to that size, then use BitBlt to set the crop size and x,y positions you want.

    I did this in a hurry using pic boxes, maybe it help?
    Code:
    Option Explicit
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function PrintWindow Lib "User32.dll" (ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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 Sub Command1_Click()
    
        Dim Calc_hWnd As Long
        Dim Calc_Rect As RECT
        
        Me.ScaleMode = vbPixels
        
        ' temp destination
        Picture1.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
        
        ' final destination
        Picture2.ScaleMode = vbPixels
        Picture2.AutoRedraw = True
            
        ' Get Handle to windows calculator
        Calc_hWnd = FindWindow(vbNullString, "Calculator")
        
        ' get size of windows calculator
        If Calc_hWnd Then
            GetWindowRect Calc_hWnd, Calc_Rect
            ' size temp destination to calc
            Picture1.Width = Calc_Rect.Right - Calc_Rect.Left
            Picture1.Height = Calc_Rect.Bottom - Calc_Rect.Top
            ' capture it
            PrintWindow Calc_hWnd, Picture1.hDC, 0
            Picture1.Refresh 'update temp
            
            'Set final destination to crop size
            Picture2.Width = Picture1.Width / 2
            Picture2.Height = Picture1.Height / 2
    
            'copy, x=10, y=51
            BitBlt Picture2.hDC, 0, 0, _
                                Picture2.ScaleWidth, _
                                Picture2.ScaleHeight, _
                                Picture1.hDC, 10, 51, vbSrcCopy
                                 
            'update dest.
            Picture2.Refresh
            ' save it as bmp file
            SavePicture Picture2.Image, "C:\Calc_Capture.bmp"
        Else
            MsgBox "Calculator is not running!", vbInformation
        End If
        
    End Sub

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - PrintWindow() function

    Quote Originally Posted by Edgemeal View Post
    Use GetWindowRect to get the size of the window Printwindow is going to capture, set the temp destination to that size, then use BitBlt to set the crop size and x,y positions you want.

    I did this in a hurry using pic boxes, maybe it help?
    Code:
    Option Explicit
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Declare Function PrintWindow Lib "User32.dll" (ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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 Sub Command1_Click()
    
        Dim Calc_hWnd As Long
        Dim Calc_Rect As RECT
        
        Me.ScaleMode = vbPixels
        
        ' temp destination
        Picture1.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
        
        ' final destination
        Picture2.ScaleMode = vbPixels
        Picture2.AutoRedraw = True
            
        ' Get Handle to windows calculator
        Calc_hWnd = FindWindow(vbNullString, "Calculator")
        
        ' get size of windows calculator
        If Calc_hWnd Then
            GetWindowRect Calc_hWnd, Calc_Rect
            ' size temp destination to calc
            Picture1.Width = Calc_Rect.Right - Calc_Rect.Left
            Picture1.Height = Calc_Rect.Bottom - Calc_Rect.Top
            ' capture it
            PrintWindow Calc_hWnd, Picture1.hDC, 0
            Picture1.Refresh 'update temp
            
            'Set final destination to crop size
            Picture2.Width = Picture1.Width / 2
            Picture2.Height = Picture1.Height / 2
    
            'copy, x=10, y=51
            BitBlt Picture2.hDC, 0, 0, _
                                Picture2.ScaleWidth, _
                                Picture2.ScaleHeight, _
                                Picture1.hDC, 10, 51, vbSrcCopy
                                 
            'update dest.
            Picture2.Refresh
            ' save it as bmp file
            SavePicture Picture2.Image, "C:\Calc_Capture.bmp"
        Else
            MsgBox "Calculator is not running!", vbInformation
        End If
        
    End Sub
    think in these way: i have the image in a picturebox(the container image with controls), now i need crop the image. but the bitblt isn't working
    but, i don't know if these must be said(i don't know write the other word), i don't use the container size to the picturebox size. is these a problem?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [VB6] - PrintWindow() function

    Quote Originally Posted by joaquim View Post
    think in these way: i have the image in a picturebox(the container image with controls), now i need crop the image. but the bitblt isn't working
    but, i don't know if these must be said(i don't know write the other word), i don't use the container size to the picturebox size. is these a problem?
    Maybe you could upload demo of your code, but just some quick thoughts,...

    If Picbox.AutoRedraw=True then call Picbox.Refesh after the BitBlt call.

    If Picbox.AutoRedraw=False then do not call Picbox.Refesh after the BitBlt call.

    The xSrc and ySrc in BitBlt are the source capture points, so xSrc must be < the source width and ySrc < source height.

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [VB6] - PrintWindow() function

    Quote Originally Posted by Edgemeal View Post
    Maybe you could upload demo of your code, but just some quick thoughts,...

    If Picbox.AutoRedraw=True then call Picbox.Refesh after the BitBlt call.

    If Picbox.AutoRedraw=False then do not call Picbox.Refesh after the BitBlt call.

    The xSrc and ySrc in BitBlt are the source capture points, so xSrc must be < the source width and ySrc < source height.
    using these sub i can copy a container image with it controls(by image way):
    Code:
    Public Sub ImageWithControls(ByRef srcPicture As Long, ByRef dstPicture As Long)
        PrintWindow srcPicture, dstPicture, 0
    End Sub
    ok how my code works:
    Code:
     picGraphicsEffects.Picture = UserControl.Image ' coopy the uc image to picgraphicseffects picturebox(until here fine)
            UserControl.Picture = Nothing
            Load PicAnimation(PicAnimation.Count) 'then i create 1 picturebox fox put the container image
            ImageWithControls Extender.Container.hwnd, PicAnimation(PicAnimation.Count - 1).hDC
            PicAnimation(PicAnimation.Count - 1).Picture = PicAnimation(PicAnimation.Count - 1).Image ' unti here works fine
            Load PicAnimation(PicAnimation.Count)
            Debug.Print BitBlt(PicAnimation(PicAnimation.Count - 1).hDC, 0, 0, UserControl.Width, UserControl.Height, PicAnimation(PicAnimation.Count - 2).hDC, Extender.Left, Extender.Top, SRCCOPY) ' i use these line for crop the image. i only need the usercontrol position and it's size(for crop the image). but here is the problem. is like isn't working or ignored:(
    i don't understand why these bug(i know by seing
    see the image result.. the problem is the girl background image, isn't correct, like you can see by container
    Attached Images Attached Images  
    VB6 2D Sprite control

    To live is difficult, but we do it.

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