Using the micro-message PC-side DLL library to implement the screenshot of the instance code

Source: Internet
Author: User
This article mainly for you in detail the use of PC-side DLL library implementation functions, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the DLL library implementation functions for your reference, the specific content as follows

ScreenForm.cs Code:


Using system;using system.collections.generic;using system.runtime.interopservices;using System.Windows.Forms;    namespace screent{public partial class Screenform:form {public Screenform () {InitializeComponent ();    } private void Screencapture () {DLL.PRSCRN (); } protected override void WndProc (ref Message m) {base.      WndProc (ref m);    Hotkey.processhotkey (m);    } private void Button1_Click (object sender, EventArgs e) {dll.prscrn (); } private void Form1_Load (object sender, EventArgs e) {//Register hotkey (form handle, hotkey ID, secondary key, real key) try {Hotke      Y.regist (Handle, Hotkeymodifiers.mod_alt, KEYS.F1, screencapture);      } catch (Exception te) {MessageBox.Show ("Alt + A Hotkey is occupied"); }} private void Form1_formclosed (object sender, Formclosedeventargs e) {//Note cancellation hotkey (handle, hotkey ID) hotkey.unre    Gist (Handle, screencapture); }} public class DLL {[DllImport ("PrScrn.dll", EntryPoint = "Prscrn")] public static extern int Prscrn (); Consistent with DLL} public static class Hotkey {#region system API [DllImport ("User32.dll")] [Return:marshalas (unmanage    Dtype.bool)] private static extern Bool RegisterHotKey (IntPtr hWnd, int id, hotkeymodifiers fsmodifiers, Keys VK);    [DllImport ("User32.dll")] private static extern bool Unregisterhotkey (INTPTR hWnd, int id);    #endregion public delegate void Hotkeycallbackhanlder ();    Private Const int wm_hotkey = 0x312;    private static int keyID = 10; private static readonly Dictionary<int, hotkeycallbackhanlder> keymap = new Dictionary<int, Hotkeycallbackha    Nlder> ();  <summary>///Register shortcut key///</summary>//<param name= "HWnd" > hold the handle of the accelerator window </param>// <param name= "fsmodifiers" > Key combinations </param>//<param name= "VK" > Shortcut keys for virtual keys </param>//<param N Ame= "CallBack" > callback function </param> public static void Regist (IntPtr hWnd, Hotkeymodifiers fsmodIfiers, Keys VK, Hotkeycallbackhanlder callBack) {int id = keyid++; if (!      RegisterHotKey (hWnd, ID, fsmodifiers, VK)) throw new Exception ("Regist hotkey fail.");    Keymap[id] = CallBack;    }///<summary>///Logout shortcut key////</summary>//<param name= "hWnd" > Hold Handle to Accelerator window </param> <param name= "CallBack" > callback function </param> public static void Unregist (IntPtr hWnd, Hotkeycallbackhanlder CA Llback) {foreach (var var in keymap) {if (Var). Value = = callBack) Unregisterhotkey (HWnd, var.)      Key);       }}///<summary>///Accelerator message processing///</summary> public static void Processhotkey (message m) {        if (m.msg = = Wm_hotkey) {int id = m.wparam.toint32 ();        Hotkeycallbackhanlder callback; if (keymap.        TryGetValue (ID, out callback)) {callback ();  }}}} public enum hotkeymodifiers {mod_alt = 0x1, Mod_control = 0x2,  Mod_shift = 0x4, Mod_win = 0x8}} 

Operation Result:

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.