C # simulate PrintScreen and Alt + PrintScreen to capture screen images,

Source: Internet
Author: User

C # simulate PrintScreen and Alt + PrintScreen to capture screen images,

C # simulate PrintScreen and Alt + PrintScreen to capture screen images

Keybd_event API function: This function is used to synthesize a key event. The system can use this synthetic key event to generate the WM_KEYUP or WM_KEYDOWN message. The interrupt handler of the keyboard driver calls the keybd_event function. In Windows NT, this function has been replaced by SendInput. Function prototype; VOID keybd_event (BYTE bVk, BYTE bScan, DWORD dwFlags, DWORD dwExtralnfo); parameter: bVk: defines a virtual key code. The key value must be 1 ~ In the range of 254.
BScan: The hardware scan code that defines the key.
DwFlags: defines a flag set for all aspects of function operations. The application can use a combination of the following predefined constants to set the flag.
KEYEVENTF_EXTENDEDKEY: if this value is specified, the first value of the scan code is the prefix byte of OXEO (224.
KEYEVENTF_KEYUP: if this value is specified, the key is released. If this value is not specified, the key is pressed.
DwExtralnfo: defines the 32-bit value associated with the strike key.
Return Value: this function has no return value. Complete code:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; using System. runtime. interopServices; namespace PrintScreen {public partial class Form1: Form {[DllImport ("user32.dll")] static extern void keybd_event (byte bVk, // virtual key value byte bScan, // hardware scan code uint dwFlags, // action ID IntPtr dwExtraInfo // auxiliary code associated with keyboard actions Add information); // <summary> // simulate the Print Screen Keyboard Message to capture a full Screen image. /// </Summary> public void PrintScreen () {keybd_event (byte) 0x2c, 0, 0x0, IntPtr. zero); // down Application. doEvents (); keybd_event (byte) 0x2c, 0, 0x2, IntPtr. zero); // up Application. doEvents () ;}//< summary> /// simulate the Alt Print Screen Keyboard Message to capture the current window image. /// </Summary> public void AltPrintScreen () {keybd_event (byte) Keys. menu, 0, 0x0, IntPtr. zero); keybd_event (byte) 0x2c, 0, 0x0, IntPtr. zero); // down Application. doEvents (); Application. doEvents (); keybd_event (byte) 0x2c, 0, 0x2, IntPtr. zero); // up keybd_event (byte) Keys. menu, 0, 0x2, IntPtr. zero); Application. doEvents (); Application. doEvents ();} public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {}/// <summary> /// obtain an image from the clipboard /// </summary> /// <returns> </returns> private Bitmap GetScreenImage () {IDataObject newobject = null; Bitmap NewBitmap = null; try {Application. doEvents (); newobject = Clipboard. getDataObject (); if (Clipboard. containsImage () {NewBitmap = (Bitmap) (Clipboard. getImage (). clone ();} return NewBitmap;} catch (Exception ex) {Console. writeLine (ex. message); return null ;}} private void button#click (object sender, EventArgs e) {button1.Enabled = false; pictureBox1.Image = null; PrintScreen (); pictureBox1.Image = GetScreenImage (); button1.Enabled = true; Application. doEvents ();} private void button2_Click (object sender, EventArgs e) {button2.Enabled = false; Enabled = null; AltPrintScreen (); pictureBox1.Image = GetScreenImage (); button2.Enabled = true; Application. doEvents ();}}}
Running effect:


Legacy problems:

PrintScreen has no task problems, but when using AltPrintScreen, the correct image is always not available for the first time. I don't know why! I hope you can pass by and give me some advice. I am very grateful to you!


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.