C # simulate printscreen and ALT + printscreen to capture screen images

Source: Internet
Author: User

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!

C # simulate printscreen and ALT + printscreen to capture screen images

Related Article

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.