**Saving a Printscreen**

Hi,

I'm developing a VB6 application & I found one way to save a printscreen.....

I have a Picturebox called Picture20 on my form where the property .Visible is = False. So what I want to do is disable all my buttons, kind of cleaning up the form before I take a screenshot.  

    'Disabling form controls
    button1.visible = false
    listbox1.visible = false

    Set Picture20.Picture = CaptureActiveWindow()
    SavePicture Picture20.Picture, ("C:\mypicture.bmp")

But when I save the image, button1 and listbox1 is still visible.


This other way works too, but I am unable to save the image as a .bmp or .jpg, etc. using:

    SavePicture Picture1.Picture, ("C:\mypicture.bmp")

apparently it is only available in memory?

Here is the walkthrough:
A timer control's interval is set to 100, button1 is clicked (making other controls invisible, and activating the screenshot)

'Global declarations
Private Declare Sub Sleep Lib "kernel32" (ByVal sleepTime As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

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 GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Dim blnAboveVer4 As Boolean

'//Disables controls and activates the timer
Private Sub button1_Click()

    button1.visible = false
    listbox1.visible = false
    Me.Timer1.Enabled = True

End Sub

'//Timer is now shut off while the controls are invisible and quickly takes the screenshot
Public Sub Timer1_Timer()
    Timer1.Enabled = False
    cboSelection.Visible = False

   '//Takes the screenshot

    If blnAboveVer4 Then
        keybd_event VK_SNAPSHOT, 1, 0, 0
    Else
        keybd_event VK_MENU, 0, 0, 0
        keybd_event VK_SNAPSHOT, 0, 0, 0
        keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
        keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
    End If

   '//Enables the controls again so I can use it
   button1.Visible = True
   listbox1.Visible = True
   
End Sub

For this case the way I tested to see if the VB Controls are invisible during the screenshot is by printing it out. First there's another form called "Final" and a picturebox. The clipboard screenshot is set to a picturebox (Picture1).

        Printer.Orientation = vbPRORLandscape
        Final.Picture1.Picture = Clipboard.GetData()
        Printer.PaintPicture Final.Picture1.Picture, 0, 0
        Printer.EndDoc

I wish we could just manually paste the image into Microsoft Paint and then save it, but that wouldn't be "User Friendly."
If you have any way of combining both of these codes so that it saves with the controls being .Visible = False that'd be great, or if you have any other code solutions that do the same idea, please reply.

Thanks for your time.

Chris
LVL 1
TrancedifiedAsked:
Who is Participating?
 
zzzzzoocConnect With a Mentor Commented:
Use DoEvents so the form can repaint without the controls before emulating PrintScrn. You may have other issues with your method but I didn't test it. Example below...

Form1:
=========

Private Declare Sub keybd_event Lib "USER32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2
Private Function CaptureActiveWindow() As StdPicture
    'Clear clipboard for new screenshot
    Call Clipboard.Clear
    'Screenshot..
    keybd_event vbKeySnapshot, 1, 0, 0
    keybd_event vbKeySnapshot, 1, KEYEVENTF_KEYUP, 0
    'Wait until clipboard has screenshot bitmap..
    Do Until Clipboard.GetData(vbCFBitmap) <> 0
        DoEvents
    Loop
    'Set function to return picture
    Set CaptureActiveWindow = Clipboard.GetData(vbCFBitmap)
End Function
Private Sub button1_Click()
    On Error Resume Next
    'Loop through every control and set visible=false
    'Some controls may not have a "Visible" property so On Error Resume Next
    Dim objCtrl As Control
    For Each objCtrl In Me.Controls
        objCtrl.Visible = False
        DoEvents
    Next objCtrl
    'Set our picture..
    Set Picture1.Picture = CaptureActiveWindow
    'Set controls to visible again
    For Each objCtrl In Me.Controls
        objCtrl.Visible = True
        DoEvents
    Next objCtrl
End Sub
0
 
vinnyd79Commented:
Does it work if you set the timer interval to 1000?
0
 
TrancedifiedAuthor Commented:
vinnyd79,

Yeah that is how I had it with the second method of saving a printscreen. When I try to save the image that is loaded into a picturebox.. I get an error like:

     "Invalid property value"

and it points to:

     SavePicture Picture1.Picture, ("C:\mypicture.bmp")


Chris
0
 
TrancedifiedAuthor Commented:
zzzzzooc,

Awesome code! It is a lot shorter than other printscreen routines I've seen. Some had to have a long module attached to your project, others had longer pieces of code to do such a simple task.

Thanks for your quick reply.

Chris
0
 
zzzzzoocCommented:
>> Awesome code! It is a lot shorter than other printscreen routines I've seen.

Shorter doesn't always mean better. This is a sloppy (but easy) method of doing this. Take into account that users may have information in the clipboard they need when you replace it with a screenshot.

Good luck.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.