C # analog PRTSCN implementation screenshot

Source: Internet
Author: User
Tags message queue

With a prior knowledge of the basics, now begin to implement PRTSCN and ALT+PRTSCN.

Start by creating a new WPF application named Printscreenandaltprintscreen

To Import the Keybd_event method :
You need to add a using System.Runtime.InteropServices for DllImport;

[DllImport("user32.dll")]staticexternvoid keybd_event(    byte bVk,// 虚拟键值    byte bScan,// 硬件扫描码    uint dwFlags,// 动作标识    IntPtr dwExtraInfo// 与键盘动作关联的辅加信息);

Write the PRTSCN function:

public void Printscreen () {keybd_event ((byte)0x2c,0,0x0, INTPTR. Zero);//downSystem. Windows. Forms. Application. DoEvents();//Add Reference System.Windows.FormsKeybd_event ((Byte)0x2c,0,0x2, INTPTR. Zero);//upSystem. Windows. Forms. Application. DoEvents();}

Write the ALT+PRTSCN function:

public void Altprintscreen () {keybd_event (byte) Keys. Menu,0,0x0, INTPTR. Zero);Keybd_event ((Byte)0x2c,0,0x0, INTPTR. Zero);//downSystem. Windows. Forms. Application. DoEvents();System. Windows. Forms. Application. DoEvents();Keybd_event ((Byte)0x2c,0,0x2, INTPTR. Zero);//upKeybd_event (Byte) Keys. Menu,0,0x2, INTPTR. Zero);System. Windows. Forms. Application. DoEvents();System. Windows. Forms. Application. DoEvents();}

write a function from the Clipboard to get the picture:
You need to add a using System.Drawing; Add Reference System.Drawing

Private Bitmap Getscreenimage () {System. Windows. Forms. IDataObjectNewObject = null;Bitmap Newbitmap = null;try {System. Windows. Forms. Application. DoEvents();NewObject = System. Windows. Forms. Clipboard. GetDataObject();if (System. Windows. Forms. Clipboard. ContainsImage()) {Newbitmap = (Bitmap) (System. Windows. Forms. Clipboard. GetImage(). Clone());} return Newbitmap;} catch (Exception ex) {Console. WriteLine(ex. Message);return null;}}

Write the Xmal program:
Simple addition of two image controls and two button buttons

 <window x:class= "printscreenandaltprintscreen.mainwindow" xmlns=" Http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x="/http/ Schemas.microsoft.com/winfx/2006/xaml " Title=" MainWindow " Height=" 768 "  Width="1024x768">     <Grid>          <Image Name="Image1" Margin="45,30,562,203">          </Image>          <button  Content  =< Span class= "Hljs-value" > "PRTSCN"  height  = "24"  horizontalalignment  = "left"  margin  = "182,552,0,0"  name  = "button1"  verticalalignment  = "Top"  width  = "up"   Click  = "button1_click" />           <button  Content  =< Span class= "Hljs-value" > "ALT+PRTSCN"  height  = "
     
       class= "Hljs-attribute" >horizontalalignment 
      = "left"  margin  = "718,552,0,0"  name  = "Button2"  verticalalignment  = "Top"  width  = "up"   Click  = "button2_click" />           <Image Margin="566,30,41,213" Name="Image2" />      </Grid>  </Window>

to add a click event to two buttons:
To convert bitmap to BitmapSource, you can see the blog: Bitmap and BitmapImage in WPF (C #)

private void Button2_Click (object sender, RoutedEventArgs e) {Image2. Source= NULL;Altprintscreen ();Bitmap Bitmap = Getscreenimage ();IntPtr IP = bitmap. Gethbitmap();//Create GDI bitmap objects from GDI + bitmapImaging. Createbitmapsourcefromhbitmapmethod, based on a pointer to the provided unmanaged bitmap and palette information, returns a managed BitmapSource bitmapsource BitmapSource = System. Windows. Interop. Imaging. Createbitmapsourcefromhbitmap(IP, INTPTR. Zero, Int32Rect. Empty, the System. Windows. Media. Imaging. Bitmapsizeoptions. Fromemptyoptions());Image2. Source= BitmapSource;}
private void Button1_Click (object sender, RoutedEventArgs e) {Image1. Source= NULL;Printscreen ();Bitmap Bitmap = Getscreenimage ();IntPtr IP = bitmap. Gethbitmap();//Create GDI bitmap objects from GDI + bitmapImaging. CreatebitmapsourcefromhbitmapMethod//Returns a managed BitmapSource BitmapSource BitmapSource = System based on a pointer to the provided unmanaged bitmap and palette information. Windows. Interop. Imaging. Createbitmapsourcefromhbitmap(IP, INTPTR. Zero, Int32Rect. Empty, the System. Windows. Media. Imaging. Bitmapsizeoptions. Fromemptyoptions());Image1. Source= BitmapSource;}

Finally, a little explanation of the DoEvents method:
Application.doevents Method
Handles all Windows messages that are currently in the message queue.
Grammar:

publicstaticvoidDoEvents()

When you run a Windows form, it creates a new form, and then the form waits for the event to be processed. The form handles all the code associated with the event each time the event is processed. All other events are waiting in the queue. When the code handles the event, the application does not respond. For example, if you drag a window over window B, the b window will not redraw.
If you call DoEvents in your code, your application can handle other events. For example, if you have a form that adds data to a ListBox and adds DoEvents to your code, the form will be redrawn when you drag another window onto your form. If you remove DoEvents from your code, your form will not redraw until the button's Click event handler finishes executing.
Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method.

The final hair map does not send the type:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # analog PRTSCN implementation screenshot

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.