Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

VB2008 Tutorial - Print Screen


  • Please log in to reply
19 replies to this topic

#1 travy92

travy92

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 73 posts

Posted 10 April 2008 - 04:59 PM

Hello everyone! I recently saw TcM's code of Print Screen and i decided to try and make it to see if i could do what he could do :).

So i ended up with a full code (not advanced as TcM's though, he's a much more experienced programmer).

_________________________________________________________________

GUI
Options Form:
Posted Image


Picture Form:
Posted Image
_________________________________________________________________

What you will need:

2 Forms named:

Options.vb (Form1)
Picture.vb (Form2)



On the options form:

4 Labels named:


Name: Label1
Text: "ScreenShot Name:"

Name: Label2
Text: "ScreenShot No."

Name: Label3
Text: "Label3"

Name: Label4
Text: "Label4"


1 Command Button named:
Name: Button1
Text: "Take ScreenShot"


On the Picture Form you will need:

1 PictureBox named:
Name: PictureBox1
_________________________________________________________________

Ok now for the code:
This is the full Code, the explanation follows:


This is the code for the Options Form.

Public Class Options

    Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Text = "ScreenShot"
        Label4.Text = "0"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Hide()
        Label3.Text = "ScreenShot"
        Label4.Text = Format(Val(Label4.Text) + 1)
        Threading.Thread.Sleep(2000)
        SendKeys.Send("{Prtsc}")
        Threading.Thread.Sleep(1000)
        Picture.Show()
        Picture.PictureBox1.Image = My.Computer.Clipboard.GetImage
        Try
            Picture.PictureBox1.Image.Save("ScreenShot " & Label4.Text & ".bmp")
        Catch
            MsgBox("ERROR! There is already a file named ScreenShot " & Label4.Text & ".", MsgBoxStyle.OkOnly & MsgBoxStyle.Critical, "ERROR!")
        End Try
        Show()
        Threading.Thread.Sleep(1000)
        Activate()
    End Sub
End Class
And this is the code for the Picture Form.

Public Class Picture

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub Picture_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        Hide()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Hide()
    End Sub
End Class
_________________________________________________________________

CODE EXPLANATION

For the Options Form:

Public Class Options
End Class
Basically means "The code for Options Form" and "Close coding for options form".
_____________________

Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Text = "ScreenShot"
        Label4.Text = "0"
    End Sub
When the form loads (Options Form), Label3's text becomes "ScreenShot" and Label4's text becomes "0". Then Ends the Options_load event.

_____________________

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Hide()
        Label3.Text = "ScreenShot"
        Label4.Text = Format(Val(Label4.Text) + 1)
        Threading.Thread.Sleep(2000)
        SendKeys.Send("{Prtsc}")
        Threading.Thread.Sleep(1000)
        Picture.Show()
        Picture.PictureBox1.Image = My.Computer.Clipboard.GetImage
        Try
            Picture.PictureBox1.Image.Save("ScreenShot " & Label4.Text & ".bmp")
        Catch
            MsgBox("ERROR! There is already a file named ScreenShot " & Label4.Text & ".", MsgBoxStyle.OkOnly & MsgBoxStyle.Critical, "ERROR!")
        End Try
        Show()
        Threading.Thread.Sleep(1000)
        Activate()
    End Sub
Ok now this is a little more complicated.
When you click the Button1 Button, the following events are fired (in order of the code above):

- Hides Form
- Changes Label3's text to "ScreenShot"
- Changes Label4's text to + 1 of what it already is (eg. If Label4's text is 4 then it becomes 5)
- Make Thread Sleep (Pause for 2 sec)
- Send the keys (Prtsc) which is Print Screen
- Makes Thread Sleep (Pause for 1 sec)
- Show the Picture Form
- Get the Image that you jsut ScreenShotted
- Try to save the image with the name, but if there's already a file with the same name, do the following:
- Provides a message box telling you that there is another file with the same name
- Stops Try
- Shows the main form (Options.vb)
- Make Thread Sleep (Pause for 1 sec)
- Activate Options.vb (Options Form)
- End Codes for Button1

________________________________________

Picture Form Code:

Public Class Picture
End Class
Just means "Code for Picture Form" and "Stop code for Picture Form".

___________________

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub
When Form1 Loads (Picture.vb / Picture Form), Make the window state maximized). Stop coding for Picture.Load.

___________________

Private Sub Picture_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        Hide()
    End Sub
When you click the Picture Form, Hide the form.

___________________

Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Hide()
    End Sub
When you click PictureBox1, Hide the form.

_________________________________________________________________

Thankyou for reading this tutorial.

Made by Travy92,
Special thanks to TcM for giving me the idea :)

Attachments for my ScreenShot Program are below:

Attached Files


Edited by Jordan, 11 April 2008 - 08:42 AM.

  • 0
[SIGPIC]C:\Users\Travis\Desktop\Image Converter\Knight1.bmp[/SIGPIC]

#2 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 11 April 2008 - 08:26 AM

Nice one, travy.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 kridan

kridan

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 May 2008 - 10:34 AM

this is good
  • 0

#4 Martin_kp

Martin_kp

    CC Regular

  • Just Joined
  • PipPipPip
  • 46 posts

Posted 16 May 2008 - 08:57 PM

Hey! I typed it up in Visual Basic and:
7 ERRORS.
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 17 May 2008 - 08:05 AM

Don't type it - grab it straight from the ZIP file. Are you using VS2008?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Martin_kp

Martin_kp

    CC Regular

  • Just Joined
  • PipPipPip
  • 46 posts

Posted 18 May 2008 - 12:54 AM

How'd ya know? didtca ask someone?
  • 0

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 18 May 2008 - 04:41 AM

Er, no. This tutorial is about VS2008, so I'm confirming you're using the .NET Framework v3.5. If not, the objects are slightly different.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 Martin_kp

Martin_kp

    CC Regular

  • Just Joined
  • PipPipPip
  • 46 posts

Posted 19 May 2008 - 10:24 PM

Yeah I know. I'm using VS2008 (Visual Studio 2008 right?). Wait a minute... ARGH! I FORGOT IT'S IN 2.0 Mode last time! (forgot to exchange it.)
  • 0

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 20 May 2008 - 11:25 AM

So - you're obviously quite young (like me) - you're into programming? When did you start learning?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 Martin_kp

Martin_kp

    CC Regular

  • Just Joined
  • PipPipPip
  • 46 posts

Posted 21 May 2008 - 02:49 AM

Uh, at 9 (or was it 10?) ?
Hey wait a minute. Aren't we off the topic?
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 21 May 2008 - 11:14 AM

Yes, but seeing as you divert things from the original topic all the time, I might as well...

OK. Any more questions, we'll switch to PMs. Anyway, nice tutorial travy! It shows how useful the .NET Framework is in these sort of situations. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 29 May 2008 - 11:38 PM

Uh, at 9 (or was it 10?) ?
Hey wait a minute. Aren't we off the topic?


I guess you are... It's strange though, it's always like that when someone (Xav) makes a post.. :P

I think instead you should admire this tutorial and not discuss ages.

@Xav: I know that by now you are planning to post a sarcastic reply.. so just DON'T! Let's keep this on topic.

That aside, you think that this code will work in .NET 2003?
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download