[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need print screen code but what is API, Handle user32?

Posted on 2007-08-01
25
Medium Priority
?
806 Views
Last Modified: 2008-01-09
Using Windows Forms, .NET 1.1, VB.NET 2003

Hi Experts, I'm searching the archives to find code that takes a print screen of my window, and saves it to the clipboard as a bitmap file. I found code but I have few questions

What does the following mean:
1. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
2. What is the Lib "user32" above?
3. What is windows API?
4.What is a handle?

Please help!

San
0
Comment
Question by:Sanmarie
  • 10
  • 5
  • 4
  • +2
24 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 880 total points
ID: 19610714
Try this:

Public Class ScreenShot

  Public Shared Function GetImage(ByVal currentScreen As Boolean) As Image

    If currentScreen Then
      SendKeys.Send("{%}({PRTSC})")
    Else
      SendKeys.Send("{PRTSC}")
    End If

    Return Clipboard.GetDataObject.GetData("Bitmap")

  End Function

End Class

Bob
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 120 total points
ID: 19610786
or

Public Sub PrintScreen()
       Try
           SendKeys.Send("^{PRTSC}")    'printscreen
           Application.DoEvents()
           System.Threading.Thread.Sleep(200) ' To have time to catch the clipboard

           Dim data As IDataObject = Clipboard.GetDataObject()
           
           If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
               Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
            bmp.Save("C:\\cheese.jpg")
           End If
           Clipboard.SetDataObject(0)    'save memory by removing the image from the clipboard


       Catch ex As Exception
           MessageBox.Show(ex.ToString)

       End Try

   End Sub

The windows API are application programming interface that you can use. You can have more information at http://msdn2.microsoft.com/en-us/library/Aa383749.aspx
0
 

Author Comment

by:Sanmarie
ID: 19611206
Thanks. While I implement your solutions, few questions:

1. So, the windows API are the list of functions that the operating system gives for us to have access to low level operations?
2. Are the APIs and HAndles the same thing?
3. Again, what is this declaration saying?
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:hes
hes earned 120 total points
ID: 19611243
0
 

Author Comment

by:Sanmarie
ID: 19611275
Thanks for your solutions but as I code, I want to understand what I'm doing.

What do these mean?
SendKeys.Send("{%}({PRTSC})")
and
 SendKeys.Send("{PRTSC}")
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19611343
Sanmarie, follow my advice. If you don't understand the code just select the command/statement and press F1. Is dificult to explain you all the code and the links that I provide you can help you. You have to red allot to go on the right direction.

The API where used in VB5 and VB6. Now, in .Net you don't need to use it and you do the same with new methods.

SendKeys invokes the functions of the keyboard. If you do SendKeys.Send("{F1}") is the same that press F1 on your keyboard. If you open the help you will see the list of all possibilities.
0
 
LVL 20

Expert Comment

by:hes
ID: 19611366
Sanmarie
Did you look at my link, it explains the api and variables needed
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 19611371
Sanmarie just one more tip - you are in the right place for your question (EE) !

:-)
0
 
LVL 20

Expert Comment

by:hes
ID: 19611397
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 880 total points
ID: 19612047
It can be written as:

    SendKeys.Send("%{PRTSC}")

and:

    SendKeys.Send("{PRTSC}")

The {PRTSC} is a special designator that means the PrtScr key on your keyboard.

The % modifier means hold down the Alt key while pressing PrtScr.

In Windows:

    (A) If you press PrtScr (by itself), it captures the WHOLE screen.

    (B) If you press Alt-PrtScr, it captures only the ACTIVE window.

APIs and Handles are NOT the same thing...

APIs are basically pre-built functions that exposed by something else for you to use.  In this case, they are exposed by the Windows Operating System itself.  Specifically, the keybd_event() API is in the "User32" system DLL.  Other applications can have APIs.

A Handle is a unique ID (a number) that is given to each window in an application.  A form has a Handle, AND the controls inside a form also have a Handle.  A control that has a Handle is a "heavy" control.  Some controls are "lightweight", meaning they don't have their own window (and therefore don't have a Handle).  Lightweight controls draw themselves onto the parent controls surface.

Basically the API declaration is saying I want to use the kebyd_event() function from the User32.DLL.  You must specify the member types of the parameters and the return type so that the compiler (and at run-time) it will know if you have called it correctly.
0
 

Author Comment

by:Sanmarie
ID: 19612131
Thank you Idle_Mind. These were the answers I was looking for. I have read articles defining these terms but they weren't as clear as your answers.

I am clear with the other terms but one more thing:
1. So a handle uniquely identifies a window. So is it the same thing as a pointer?
2. Also, are handles assigned by the operating system or are they user defined?
3. For my own clarity, I can access a window by its name or its handle. A handle is just another way of accessing the window. Right?
0
 

Author Comment

by:Sanmarie
ID: 19612216
<In Windows:
    (A) If you press PrtScr (by itself), it captures the WHOLE screen.

    (B) If you press Alt-PrtScr, it captures only the ACTIVE window.>

By "Active window", you mean the window that has focus?

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19612265
Yes: Active Window <---> Focused Window

As to your other questions about Handles:

Essentially yes.  The Handle is the address of the ~thing~ in memory.  You have to be careful though since these addresses can be changed without your knowledge!

Handles are assigned by the operating system, but the window can request a new handle.  See Control.RecreateHandle():
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.recreatehandle.aspx
This changes the handle of the control.

So...if you have a Handle, it may or may not refer to what you think it does.  As a good rule of thumb, do NOT store the VALUE of a Handle for "later use".

If you need the handle for something, then use the controls NAME (or some kind of variable referencing that control) and ask it for the current handle:

    Form1.Handle
    PictureBox1.Handle
    someVariable.Handle

Don't do this:

    Dim storedHandle As IntPtr = PictureBox1.Handle

and then some time later in code expect "storedHandle" to still point to PictureBox1....because it might not!

Or course, when dealing with other applications, you might get the Handle of the window/control using win APIs such as FindWindow().  Again, you should not store these values for "later use".  Always get the current handle and then immediately use that value.
0
 

Author Comment

by:Sanmarie
ID: 19612352
Thanks Idle_Mind.

I have learnt so much already. Just one more thing. How do I access the contents of the clipboard?

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19612396
The others above have already given you code on how to access the Clipboard AND how to test the format of what is currently on the Clipboard.

For the documention, see:
http://msdn2.microsoft.com/en-us/library/system.windows.clipboard_members.aspx
0
 

Author Comment

by:Sanmarie
ID: 19612426
Thanks all!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19612440
I feel like I just got squeezed out of the picture!!!  C'est la vie ;)

Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19612449
=(

That should have been a Split between experts though.  I merely gave explanations on some concepts and terms.

The others provided actual code...
0
 

Author Comment

by:Sanmarie
ID: 19612476
Ok. How let me see if I can change the point allocation
0
 

Author Comment

by:Sanmarie
ID: 19612500
How do I reallocate the points?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19612537
You have to post a request here to have the question re-opened so you can reallocate points:
http://www.experts-exchange.com/Community_Support/General/

Once it is open again, here is how to Split points:
http://www.experts-exchange.com/help.jsp#hi19
0
 

Author Comment

by:Sanmarie
ID: 19616400
To be fair, I have reallocated the points. I guess I have to explain since I don't want anyone to feel left out and refrain from helping me with future questions.

Here goes:
TheLearnedOne: 220 (I actually used this code which was half my question)
Idle_Mind: 220 (Explanations were clear and this was answer to other half of my question)
jPaulino: 30 (Didn't use his code but he made an effort to help)
Hes: 30 (Made an effort)

0
 
LVL 20

Expert Comment

by:hes
ID: 19616447
No problem just glad you got it to work and understand it
0
 

Author Comment

by:Sanmarie
ID: 19616598
Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

826 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