C # provides QQ screenshot functions and related questions,

Source: Internet
Author: User

C # To implement QQ functions and related issues,

For QQ, I must have known it for a long time, but I have never carefully observed the specific implementation steps of this operation. So here we will simply write down the steps in our memory:

QQ or TIM is often used by people who use Ctrl + Alt + A Shortcut Key (Hot Key) for quick implementation.

Because only one form can be displayed in the mode, you need to use Singleton mode to implement the following code in the ScreenBody form:

1: Create a ticket

private static ScreenBody screenBody=null;

2: private constructor

private ScreenBody(){InitializeComponent();}

3. Create a static method

private static ScreenBody GetSingle(){if(screenBody==null){screenBody=new ScreenBody();}return screenBody;}

We will further discuss how to add a button named btnCutter to the Main form calling Main.

Private void btnCutter_Click (object sender, EventArgs e) {// you can use BitMapimage img = new Bitmap (Screen. allScreens [0]. bounds. width, Screen. allScreens [0]. bounds. height); // create a drawing board so that we can draw the same size as the Screen Graphics g = Graphics. fromImage (img); // copy the screen image to the blank image imgg. copyFromScreen (new Point (0, 0), new Point (0, 0), Screen. allScreens [0]. bounds. size); // create a form ScreenBody body = ScreenBody. getSingle (); // indicates that the background image of the form is the screen image body. backGroundImage = img; body. showDialog ();}

For the form ScreenBody

Declare global variables

Private bool CatchStart; // determines whether the mouse presses private bool CatchFinished; // determines whether the rectangle is successfully drawn. // The private Image baseMap clicked by the mouse; // The most basic image private Rectangle CatchRectangle;

 

 

The events that must be implemented

  • Mouse down MouseDown
  • Private void ScreenBody_MouseDown (object sender, MouseEventArgs e) {// if (e. button = MouseButtons. left) {if (CatchStart = false) {CatchStart = true; // Save the coordinates DownPoint = new Point (e. x, e. Y );}}}

    Move MouseMove

  • Private void ScreenBody_MouseMove (object sender, MouseEventArgs e) {// make sure to start if (CatchStart) {// create an image so that it is the same as the screen image Bitmap copyBmp = (Bitmap) baseMap. clone (); // The coordinate Point newPoint = new Point (DownPoint. x, DownPoint. y); // create a drawing board and brush Graphics g = Graphics. fromImage (copyBmp); Pen p = new Pen (Color. azure, 1); // the paint brush color is azure width 1 // get the length of the rectangle int width = Math. abs (e. x-DownPoint. y); int height = Math. abs (e. y- DownPoint. y); if (e. X <DownPoint. x) {newPoint. X = e. x;} if (e. Y <DownPoint. y) {newPoint. Y = e. y;} CatchRectangle = new Rectangle (newPoint, new Size (width, height); g. drawRectangle (p, CatchRectangle); // release the current canvas g. dispose (); p. dispose (); // create a new artboard Graphics g1 = this from the current form. createGraphics (); // draw the image you just drew to the form. // Why not draw the image directly in the current form ??? // If the rectangle is directly painted on the form, it will cause image jitter and there are multiple rectangles. // This implementation also belongs to the secondary buffer technology g1.DrawImage (copyBmp, new Point (0, 0); g1.Dispose (); // release the copy image to prevent memory from being consumed by a large amount of copyBmp. dispose ();}

    Mouse up Mouseup

  • /// <Summary> /// the left mouse button pops up the event // </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void ScreenBody_MouseUp (object sender, mouseEventArgs e) {if (e. button = MouseButtons. left) {// if it has already started, the Left mouse button pops up to set if (CatchStart) {CatchStart = false; CatchFinished = true ;}}}

    Double-click

  • Private void ScreenBody_MouseDoubleClick (object sender, MouseEventArgs e) {if (e. button = MouseButtons. left & CatchFinished) {// create a blank image with the same rectangle size. Bitmap CatcheBmp = new Bitmap (CatchRectangle. width, CatchRectangle. height); Graphics g = Graphics. fromImage (CatcheBmp );; // draw the specified part of basemap to the blank image according to the specified size. // The specified part of baseMap specified by CatchRectangle // The second parameter specifies the position and size of the blank image/ /After painting, the CatchedBmp is no longer a blank image, instead, it has the same content as the captured image g. drawImage (baseMap, new Rectangle (0, 0, CatchRectangle. width, CatchRectangle. height); // Save the image to the Clipboard. setImage (CatcheBmp); g. dispose (); CatchFinished = false; this. backgroundImage = baseMap; CatcheBmp. dispose (); this. dialogResult = DialogResult. OK; this. close ();}}

    Right-click to exit

  • /// <Summary> /// right-click and end the task. /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void ScreenBody_MouseClick (object sender, mouseEventArgs e) {if (e. button = MouseButtons. right) {this. dialogResult = DialogResult. OK; this. close ();}}

    The most complex hot key registration is also in the Main form viewed on the Internet.

  • Declared Enumeration
     [FlagsAttribute]    public enum KeyModifiers    {        None = 0,        Alt = 1,        Ctrl = 2,        Shift = 4,        WindowsKey = 8    }

    Edit the code in the class.

  • // Reference the namespace System in C. runtime. interopServices; to load the prototype and description of the unmanaged user32.dll/** RegisterHotKey function: * BOOL RegisterHotKey (* HWND hWnd, // window to receive hot-key notification * int id, // identifier of hot key * UINT fsModifiers, // key-modifier flags * UINT vk // virtual-key code ); * parameter id is an ID value defined for you * For a thread, the value must be in the range of 0x0000-0xBFFF, and the value must be in decimal format: 0 ~ 49151 * The DLL value must be in the range of 0xC000-0 xFFFF, And the decimal value is 49152 ~ 65535 * in the same process, this value must be unique. fsModifiers indicates that the key is used together with the hotkey. * The value can be MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT, or the number 0 is none, and 1 is Alt, 2 is Control, 4 is Shift, and 8 is the virtual key code indicating the hotkey for Windows * vk */[System. runtime. interopServices. dllImport ("user32.dll")] // declare the API function public static extern bool RegisterHotKey (IntPtr hWnd, // handle to window int id, // hot key identifier uint fsModifiers, // key-modifier options Keys vk // virtual-key code); [System. runtime. interopServices. dllImport ("user32.dll")] // declare the API function public static extern bool UnregisterHotKey (IntPtr hWnd, // handle to window int id // hot key identifier );

    Then

    Private void Form1_Load (object sender, EventArgs e) {uint ctrlHotKey = (uint) (KeyModifiers. alt | KeyModifiers. ctrl); // The registration key is Alt + Ctrl + C, and "100" is the unique identification key RegisterHotKey (Handle, 100, ctrlHotKey, Keys. a);} // press the hotkey to execute the private void GlobalKeyProcess () {this. windowState = FormWindowState. minimized; // it takes some time to use the Thread to minimize the window. sleep (200); btnCutter. extends mclick ();} protected override void WndProc (ref Message m) {// If m. if the Msg value is 0x0312, the user presses the hotkey const int WM_HOTKEY = 0x0312; switch (m. msg) {case WM_HOTKEY: if (m. WParam. toString () = "100") {GlobalKeyProcess ();} break; default: break;} base. wndProc (ref m);} private void form=formclosing (object sender, FormClosingEventArgs e) {// uninstall the hotkey UnregisterHotKey (Handle, 100 );}

    The hotkey function can be implemented. However, I have encountered many problems. First, basemap has no initialization value.

  • These problems are still to be solved !!!

 

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.