C # Development Example-Custom screenshot tool (ii) Create project, register hotkey, display screenshot main window

Source: Internet
Author: User

Development environment

Operating system: Windows Server R2

Integrated development Environment (IDE): Microsoft Visual Studio 2010

Development language: C #

Create a project

The new project of the document


The. NET framework can choose either version 2.0 or version 4.0;

Project type selection: Windows Forms application

Enter the project name to determine


The project was created successfully, such as:


Modify main Form Properties

Modify the form's "FormBorderStyle" property to "none" to implement a form without Borders


After the modification, the window designer appears as follows:


Press to modify the other properties, and the property values in bold are the modified


Property Description:

Showicon=false, the icon for the form is not displayed;

Showintaskbar=false, so that the form does not appear in the Windows taskbar;

Sizegripstyle=hide, disable drag the lower right corner of the form can change the size of the function;

windowsstate=minimized, the window is minimized after starting;

After setting these properties, compile, run, program is in the running state, but can not see the program window;

Implementing Hotkey Functions

Here you need to use WINDOWSAPI

Registration Hotkey: RegisterHotKey

This function defines a system-wide Hotkey . function prototype: BOOL RegisterHotKey (HWND hwnd,int id,uint fsmodifiers,uint VK);

Cancel Hotkey Registration: Unregisterhotkey

This function frees the hot key that was previously enlisted by the calling thread.

Get Hotkey ID:globaladdatom

Applies only to desktop applications.

Adds a string to the global atomic table and returns the unique identifier (Atom Atom) of the string.

API and local variable definitions:

        <summary>////Add a string to the global atomic table and return the unique identifier for the string (Atom Atom). </summary>//<param name= "lpstring" > A string of your own set </param>//<returns></retur ns> [System.Runtime.InteropServices.DllImport ("Kernel32.dll")] public static extern Int32 Globaladdatom (s        Tring lpstring);        <summary>//Registration Hotkey///</summary>//<param name= "HWnd" ></param> <param name= "id" ></param>//<param name= "fsmodifiers" ></param>//<param N Ame= "VK" ></param>///<returns></returns> [System.Runtime.InteropServices.DllImport ("Us        Er32.dll ")] public static extern bool RegisterHotKey (INTPTR hWnd, int id, uint fsmodifiers, Keys VK);        <summary>//Cancel Hotkey Registration///</summary>//<param name= "HWnd" ></param> <param name= "id" ></param>       <returns></returns> [System.Runtime.InteropServices.DllImport ("User32.dll")] public s        tatic extern bool Unregisterhotkey (INTPTR hWnd, int id);        <summary>///Hotkey ID///</summary> public int hotkeyid = 100; <summary>///Hotkey Mode: 0=ctrl + Alt + A, 1=ctrl + Shift + A///</summary> public int Hot        Keymode = 1;            <summary>////The type of Control key///</summary> public enum Keymodifiers:uint { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8}// <summary>////For saving images of the entire screen captured///</summary> protected Bitmap screenimage;

Register Hotkey:

        private void Form1_Load (object sender, EventArgs e)        {            //Hide window this            . Hide ();            Register shortcut key            //NOTE: The legal range of Hotkeyid is 0x0000 to 0xBFFF, Globaladdatom function to get the value between 0xc000 to 0xFFFF, so subtract 0xc000 to meet the call request.            This.hotkeyid = Globaladdatom ("screenshot")-0xc000;            if (This.hotkeyid = = 0)            {                //If the acquisition fails, set a default value;                This.hotkeyid = 0xBFFE;             }            if (this. Hotkeymode = = 0)            {                RegisterHotKey (Handle, Hotkeyid, (UINT) Keymodifiers.control | (UINT) Keymodifiers.alt, KEYS.A);            }            else            {                RegisterHotKey (Handle, Hotkeyid, (UINT) Keymodifiers.control | (UINT) Keymodifiers.shift, KEYS.A);            }        }

Hotkey Response Function:

        <summary>///        handling shortcut key events///</summary>//        <param name= "M" ></param>        protected override void WndProc (ref Message m)        {            //if (m.msg = = 0x0014)            //{            //    return;//disable erase background message c10/>//}            const int wm_hotkey = 0x0312;            Switch (m.msg)            {case                Wm_hotkey:                    showform ();                    break;                Default: Break                    ;            }            Base. WndProc (ref m);        }

Window Implementation principle

The window is actually a full-screen top-level window with no borders, no menus, and no toolbars.

When the hotkey is pressed, the program first gets the picture of the entire screen, saves it to the "screenimage" variable, then adds a mask layer, sets it as the background of the form, sets the window size to the size of the main screen, and displays the window; it feels like a translucent mask layer on the desktop.

The code is as follows:

        <summary>////If the window is visible, the window is hidden;///or the window is displayed////</summary> protected VO ID ShowForm () {if (this. Visible) {this.            Hide (); } else {Bitmap bkimage = new Bitmap (Screen.allscreens[0]. Bounds.width, Screen.allscreens[0].                Bounds.height);                Graphics g = graphics.fromimage (bkimage); G.copyfromscreen (new point (0, 0), new Point (0, 0), Screen.allscreens[0].                Bounds.size, copypixeloperation.sourcecopy);                Screenimage = (Bitmap) bkimage.clone ();                G.fillrectangle (New SolidBrush (Color.FromArgb (Color.gray)), Screen.PrimaryScreen.Bounds); This.                BackgroundImage = Bkimage; This.                ShowInTaskbar = false; This.                FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; This.                Width = Screen.PrimaryScreen.Bounds.Width; This. Height = Screen.primarYScreen.Bounds.Height; This.                location = Screen.PrimaryScreen.Bounds.Location; This.                WindowState = formwindowstate.maximized; This.            Show (); }        }

Cancel Hotkey Registration

To cancel the hotkey registration when closing the window, the code is as follows:

        <summary>///When the window is closing///</summary>//        <param name= "Sender" ></param>        //<param name= "E" ></param>        private void Form1_formclosing (object sender, FormClosingEventArgs e)        {            if (E.closereason = = Closereason.applicationexitcall)            {                e.cancel = false;                Unregisterhotkey (this. Handle, Hotkeyid);            }            Else            {this                . Hide ();                E.cancel = true;            }        }

Here, the Hotkey Registration, window display and other functions have been basically completed.

Note: It is a good idea to add a button to the form to close or hide the window when testing this code, because the window is full-screen and cannot respond to the ESC key, so you can end the process exit only through Task Manager. It is a good idea to add a Label control on the form to display the required variable information, because the window is the top-level full-screen window, and the breakpoint is hit with no way to manipulate vs.

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.

Tags Index: