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 // supplementary information associated with keyboard actions );////// Simulate the Print Screen Keyboard Message to capture a full Screen image. ///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 ();}////// Simulate the Alt Print Screen Keyboard Message to capture the current window image. ///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 ){}////// Obtain the image from the clipboard //////
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!