Results 1 to 15 of 15

Thread: [RESOLVED] Window snapshot

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Resolved [RESOLVED] Window snapshot

    Hi,
    I'm trying to save a snapshot of a form window like this
    Code:
    Public Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)  ' Screen snapshot
       Clipboard.Clear
       keybd_event vbKeySnapshot, X&, 0&, 0&
       SavePicture Clipboard.GetData(vbCFBitmap), ParFileName
    If X = 0 this works fine but I get the whole screen.
    Setting X to 1 should lead to capture only the current window but I get an error 380 (Invalid property value) on SavePicture statement.

    An idea on how to get only the window ?

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Window snapshot

    Add a Picturebox Visible to False to the form, and use this code:

    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_PRINT = &H317
    Private Const PRF_NONCLIENT = &H2&
    Private Const PRF_CLIENT = &H4&
    Private Const PRF_CHILDREN = &H10&
    Private Const PRF_OWNED = &H20&
    
    Private Sub Command1_Click()
        Picture1.AutoRedraw = True
        Picture1.BorderStyle = 0
        Picture1.Width = Me.Width
        Picture1.Height = Me.Height
        SendMessage Me.hwnd, WM_PRINT, Picture1.hDC, PRF_CHILDREN Or PRF_NONCLIENT Or PRF_CLIENT Or PRF_OWNED
        SavePicture Picture1.Image, ParFileName
        Picture1.Cls
    End Sub
    PS: It is not a good practice to use the clipboard because that erases anything that the user may have stored there.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Window snapshot

    Thank you for your reply.
    The problem is that I would like to take a snapshot from a module which doesn't know what is "Me"; I could pass it as a parameter but I should modify a lot of calls to that module.
    I prefer to try to understand why keybd_event vbKeySnapshot, 0&, 0&, 0& works and keybd_event vbKeySnapshot, 1&, 0&, 0& not.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Window snapshot

    Replace me.Hwnd with the form handle, eg frmMain.Hwnd or Form1.Hwnd

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Window snapshot

    Quote Originally Posted by Arnoutdv View Post
    Replace me.Hwnd with the form handle, eg frmMain.Hwnd or Form1.Hwnd
    Being in a module I don't know which form is calling me.

    vbKeySnapshot, 0&, 0&, 0& works fine to capture the whole screen, I would like to know how to capture only the form window.

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Window snapshot

    if you are using a function in a module, call it from the form including the hwnd.
    or simply, use a public variable where you store the hwnd when you load the application.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Window snapshot

    There is no need of hwnd when using keybd_event.
    The problem is the same when I call keybd_event directly from the form :
    keybd_event vbKeySnapshot, 0&, 0&, 0& works fine to capture the whole screen,
    I would like to know how to capture only the form window.

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Window snapshot

    you need to explain better.
    you want the "key" only to work when you are hovering above a form with your mouse?
    or you want to capture only a region of the screen?
    or the active window?

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Window snapshot

    Quote Originally Posted by Herve_be View Post
    Being in a module I don't know which form is calling me.
    If the form that you want to capture is the active one, you can replace Me with Screen.ActiveForm

    Quote Originally Posted by Herve_be View Post
    vbKeySnapshot, 0&, 0&, 0& works fine to capture the whole screen, I would like to know how to capture only the form window.
    The Clipboard may be not ready, so add a DoEvents:

    Code:
       Clipboard.Clear
       keybd_event vbKeySnapshot, 1&, 0&, 0&
       DoEvents
       SavePicture Clipboard.GetData(vbCFBitmap), ParFileName

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Window snapshot

    Tested in a Form1:

    Code:
    
    Option Explicit
    '
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Byte
    
    Private Sub Command1_Click()
        AltPrintScreen
    End Sub
    
    Private Sub AltPrintScreen()
        Dim alt_key As Long
        Const VK_MENU           As Long = &H12&
        Const VK_SNAPSHOT       As Long = &H2C&
        Const KEYEVENTF_KEYDOWN As Long = &H0&
        Const KEYEVENTF_KEYUP   As Long = &H2&
        '
        alt_key = MapVirtualKey(VK_MENU, 0&)
        keybd_event VK_MENU, alt_key, KEYEVENTF_KEYDOWN, 0&
        keybd_event VK_SNAPSHOT, 0&, 0&, 0&
        keybd_event VK_MENU, alt_key, KEYEVENTF_KEYUP, 0&
    End Sub
    
    Enjoy,
    Elroy

    EDIT1: Cleaned up the code a bit more.

    EDIT2: @Herve_be: I hope you know that this is going to put a picture of a copy of the window with the focus in the clipboard. Since, in my example, you're always clicking a button, it's going to be the form with the button.
    Last edited by Elroy; Dec 4th, 2017 at 10:37 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Window snapshot

    EDIT2: @Herve_be: I hope you know that this is going to put a picture of a copy of the window with the focus in the clipboard. Since, in my example, you're always clicking a button, it's going to be the form with the button.
    But you can hide the button before calling the function. Then set its visible prop back to True afterwards, if desired.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Window snapshot

    @Sam: Awww, boo. I was going to let him figure that one out.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Window snapshot

    sorry......

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Window snapshot

    @Sam: haha, I'm just kidding around. No problem. I did think about explaining more in my post #10, but thought I'd let Herve sort it out.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    Jurbise - Belgium
    Posts
    203

    Re: Window snapshot

    Hi,
    It seems that VBforums has been down.
    Meanwhile I solved the problem thanks to BitBlt Lib "gdi32".
    The only shortcoming is that it does not capture the whole window but only the form inside.
    Thanks a lot to everybody, problem solved.

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