I think this is cool and I learned several useful tools in the process. I have plans on creating a sort of graph in a form.
But I wanted to give the user's a chance to print it --- without getting to detailed, I figured that I would grab a screen shot of the form, open Wordpad (which everyone should have), and then paste it within wordpad.
I figured they could take it from there....
So how did I do it?
First off, grab the active part of the screen using SENDKEYS with the sendwait method. I put this in a FUNCTION called screenCapture:
Public Function screenCapture() As Image
'CALL THE SENDKEYS WITH THE PRINT SCREEN METHOD -- '%' = ALT
SendKeys.SendWait("%{PRTSC}")
'CREATE AN OBJECT TO HOLD THE IMAGE - IN CASE YOU WANT TO USE IT SOMEWHERE ELSE IN YOUR CODE
Dim captImage As IDataObject = Clipboard.GetDataObject
'RETURN THAT IMAGE AND FORMAT IT AS A BITMAP - AGAIN, TO USE SOMEWHERE ELSE IN CODE (you dont need this to paste -- its ALREADY ON the clipboard)
Return captImage.GetData(DataFormats.Bitmap)
End Function
------
Now from within the button click that called the screen capture function we send a Shell command to open the WORDPAD and then use SENDKEYS again to call Control and V, thus pasting the clipboard straight into Wordpad.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Call the function
screenCapture()
'Open WordPad
Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe", vbNormalFocus)
'Use SENDKEYS AGAIN TO CTRL+V ("^" = CTRL)
SendKeys.Send("^+{V}")
End Sub
Thats about it!!! In about 2 minutes you can drag a button to your form, include the above code and you have your very own screen capture functionality!!!!!
A couple other cool things you could do is SAVE the capture -- since the function call is an image you could write this:
screenCapture().Save("c:\screenCap.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
You could also assign the captured image to a Picture Box on your form:
PictureBox1.BackgroundImage = screenCapture()
*I used Background image rather than Image because I can set the Layout of background to Stretch thus assuring that we see the entire image....
Pretty cool stuff and very quick and simple!!!
** GO SENDKEYS!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment