Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

**Saving a Printscreen**

Posted on 2004-04-08
5
Medium Priority
?
791 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

722 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