Solved

**Saving a Printscreen**

Posted on 2004-04-08
5
767 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:Trancedified
  • 2
  • 2
5 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10789003
Does it work if you set the timer interval to 1000?
0
 
LVL 1

Author Comment

by:Trancedified
ID: 10792451
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
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 10798121
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
 
LVL 1

Author Comment

by:Trancedified
ID: 10807043
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10807903
>> 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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now