Solved

**Saving a Printscreen**

Posted on 2004-04-08
5
781 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Contents of a Directory In Access VBA 5 88
Advice in Xamarin 21 109
Formula problem with Excel attachment 6 43
how to Classify of email (pdf attachments) 7 50
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

749 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