-
Dec 4th, 2017, 05:29 AM
#1
Thread Starter
Addicted Member
[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 ?
-
Dec 4th, 2017, 06:25 AM
#2
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.
-
Dec 4th, 2017, 09:05 AM
#3
Thread Starter
Addicted Member
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.
-
Dec 4th, 2017, 09:28 AM
#4
Re: Window snapshot
Replace me.Hwnd with the form handle, eg frmMain.Hwnd or Form1.Hwnd
-
Dec 4th, 2017, 09:32 AM
#5
Thread Starter
Addicted Member
Re: Window snapshot
Originally Posted by Arnoutdv
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.
-
Dec 4th, 2017, 09:47 AM
#6
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.
-
Dec 4th, 2017, 09:53 AM
#7
Thread Starter
Addicted Member
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.
-
Dec 4th, 2017, 10:12 AM
#8
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?
-
Dec 4th, 2017, 10:20 AM
#9
Re: Window snapshot
Originally Posted by Herve_be
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
Originally Posted by Herve_be
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
-
Dec 4th, 2017, 10:27 AM
#10
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.
-
Dec 4th, 2017, 10:46 AM
#11
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.
-
Dec 4th, 2017, 10:47 AM
#12
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.
-
Dec 4th, 2017, 10:54 AM
#13
-
Dec 4th, 2017, 12:03 PM
#14
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.
-
Dec 8th, 2017, 05:05 AM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|