SN Quick Input Tool written in C # code

Source: Internet
Author: User
Tags bool copy empty gettext modifier split

General software to enter the serial number (SN), and everyone usually use the most is probably pirated software, usually pirated software serial number (SN) are stored as: xxxxx-xxxxx-xxxx-xxxx form.

The place where the software enters the serial number is usually composed of several text boxes (textbox). It will be very troublesome to copy xxxxx to a text box. So the SN quick input tool is generated.

Of course, this has nothing to do with the reason why I write this program. The reason I wrote this program was simply because a netizen and his uncle had a wager that they would write a program, and his uncle is to write this program, but unfortunately my net friend is a programming beginners (more than I dish rookie), of course, not complete this seemingly simple, actually need to use a lot of programming knowledge of the program slightly.

To do this program, the first of course is to understand the function of the program. Its function is to let you copy the form such as "Xxxxx-xxxxx-xxxx-xxxx" after the serial number, when you point the mouse to the text box, the program can automatically add XXXXX to the corresponding text box.

Now that we're dealing with the copied serial number, we're definitely going to use the Clipboard-related stuff. Clipboard, okay this I used to use in C # N times, no longer check the Windows API. C # has already provided this class of Clipboard.

So we used the static method of String Clipboard.gettext () to remove the sequence number that was just copied, and then save it in my program with a string variable strkeys for use.

The first step is to get the data from the Clipboard and we're done.

Then, we should consider how to deal with our data, our data is to be written in a few consecutive text boxes, then we can consider the String.Split (char[],string splitoption) This method to split the sequence number into several substrings, The text is then printed through the Windows API to the corresponding textbox handle. But this undoubtedly increased the difficulty of the program, several consecutive text box switching, using the TAB key can be done, and then the text output to the text box, directly to the keyboard to hit the OK. So it's obvious that we just need to simulate the key we're pressing, and the first thing I thought of was the keybd_event of the keyboard simulation event in the Windows API, so I started to query the Keybd_event method in MSDN with a keyeventf_ KeyUp This parameter, but I don't know his corresponding value, so I started to look up the value of the long shape. But I never found it, and just as I was looking for KeyUp related things in MSDN, I suddenly discovered the System.Windows.Form.SendKeys class. The. NET Framework has encapsulated the method of keybd_event this unmanaged object into the SendKeys class, and the SendKeys class can simulate keyboard operations directly.

And then query the TAB key to the writing is {Tab}.

So I simply convert the '-' all of the original text strkeys to {Tab} and then to the SendKeys class to handle, the program is basically completed.

So there was

Strkeys.replace ("-", "{tab}");
Sendkeys.send (Strkeys);
These two lines of code.

This will have the main process of my program:

private void Processhotkey ()//main Handler
{
Strkeys = Clipboard.gettext ();
Strkeys.replace ("-", "{tab}");
Sendkeys.send (Strkeys);
}
But how do we trigger it through a shortcut key to complete the process.

So I started looking at Baidu and MSDN for information about Windows APIs that deal with global shortcuts.

To set a shortcut key, you must use the two methods below user32.dll.

BOOL RegisterHotKey (
HWND hwnd,
int ID,
UINT Fsmodifiers,
UINT VK
);
And

BOOL Unregisterhotkey (
HWND hwnd,
int ID
);
into C # code, you first need to refer to the namespace System.Runtime.InteropServices to load the unmanaged class user32.dll. So there was:

[DllImport ("user32.dll", Setlasterror=true)]
public static extern bool RegisterHotKey (
IntPtr hWnd,//Handle to Window
int ID,//Hot Key Identifier
Keymodifiers fsmodifiers,//Key-modifier options
Keys VK//Virtual-key code
);

[DllImport ("user32.dll", Setlasterror=true)]
public static extern bool Unregisterhotkey (
IntPtr hWnd,//Handle to Window
int ID//Hot Key Identifier
);


[Flags ()]
public enum Keymodifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
This is the way to register and uninstall global shortcuts, so we just need to add a statement to register the shortcut key in the Form_Load and uninstall the global shortcut key when formclosing. Also, to ensure that the contents of the Clipboard are not disturbed by other programs calling the Clipboard, I will empty the contents of the Clipboard at Form_Load.

So there was:

private void Form1_Load (object sender, System.EventArgs e)
{
Label2. AutoSize = true;

Clipboard.clear ()//Empty the Clipboard to prevent the Clipboard from copying other content first
RegisterHotKey (Handle, 0, KEYS.F10);
}

private void Form1_formclosing (object sender, FormClosingEventArgs e)
{
Unregisterhotkey (Handle, 100);//Uninstall shortcut keys
}
Then we are in other windows, how to let press the shortcut key after calling my main process processhotkey ()?

Then we have to rewrite the WndProc () method to invoke the procedure by monitoring the system message:

protected override void WndProc (ref message m)//Monitor Windows messages
{
const int Wm_hotkey = 0x0312;//Press shortcut key
Switch (m.msg)
{
Case Wm_hotkey:
Processhotkey ()//calling the main handler
Break
}
Base. WndProc (ref m);
}
So my program is done.

All code:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Runtime.InteropServices;

Namespace WindowsApplication2
{
///
Summary description of the Form1.
///
public class Form1:System.Windows.Forms.Form
{
///
The required designer variable.
///
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
This.label1 = new System.Windows.Forms.Label ();
This.label2 = new System.Windows.Forms.Label ();
This.label3 = new System.Windows.Forms.Label ();
This.label4 = new System.Windows.Forms.Label ();
This.label5 = new System.Windows.Forms.Label ();
This. SuspendLayout ();
//
Label1
//
This.label1.AutoSize = true;
This.label1.Location = new System.Drawing.Point (49, 37);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (83, 12);
This.label1.TabIndex = 0;
This.label1.Text = "Eos.3tion production";
//
Label2
//
This.label2.AutoSize = true;
This.label2.Location = new System.Drawing.Point (49, 64);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (65, 12);
This.label2.TabIndex = 1;
This.label2.Text = "Use method:";
//
Label3
//
This.label3.AutoSize = true;
This.label3.Location = new System.Drawing.Point (65, 85);
This.label3.Name = "Label3";
This.label3.Size = new System.Drawing.Size (155, 12);
This.label3.TabIndex = 2;
This.label3.Text = "1, copy the serial number to the Clipboard." ";
//
Label4
//
This.label4.AutoSize = true;
This.label4.Location = new System.Drawing.Point (65, 107);
This.label4.Name = "Label4";
This.label4.Size = new System.Drawing.Size (179, 12);
This.label4.TabIndex = 3;
This.label4.Text = "2, position the cursor at the serial number entry. ";
//
Label5
//
This.label5.AutoSize = true;
This.label5.Location = new System.Drawing.Point (65, 128);
This.label5.Name = "Label5";
This.label5.Size = new System.Drawing.Size (77, 12);
This.label5.TabIndex = 4;
This.label5.Text = "3, press F10 key." ";
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 266);
This. Controls.Add (THIS.LABEL5);
This. Controls.Add (THIS.LABEL4);
This. Controls.Add (THIS.LABEL3);
This. Controls.Add (THIS.LABEL2);
This. Controls.Add (THIS.LABEL1);
This. Name = "Form1";
This. Text = "SN input tool (C # version Version0.1)";
This. FormClosing + = new System.Windows.Forms.FormClosingEventHandler (this. form1_formclosing);
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
This. PerformLayout ();
}
#endregion

///
The main entry point for the application.
///
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}

[DllImport ("user32.dll", Setlasterror=true)]
public static extern bool RegisterHotKey (IntPtr hWnd,
Handle to Window
int ID,//Hot Key Identifier
Keymodifiers fsmodifiers,//Key-modifier options
Keys VK//Virtual-key code
);

[DllImport ("user32.dll", Setlasterror=true)]
public static extern bool Unregisterhotkey (IntPtr hWnd,
Handle to Window
int ID//Hot Key Identifier
);

[Flags ()]
public enum Keymodifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}

private void Processhotkey ()//main Handler
{
Strkeys = Clipboard.gettext ();
Strkeys.replace ("-", "{tab}");
Sendkeys.send (Strkeys);
}

Private Label Label1;
Private Label Label2;
Private Label label3;
Private Label label4;
Private Label Label5;

String Strkeys;

private void Form1_Load (object sender, System.EventArgs e)
{
Label2. AutoSize = true;

Clipboard.clear ()//Empty the Clipboard to prevent the Clipboard from copying other content first
RegisterHotKey (Handle, 0, KEYS.F10);
}

private void Form1_formclosing (object sender, FormClosingEventArgs e)
{
Unregisterhotkey (Handle, 100);//Uninstall shortcut keys
}

protected override void WndProc (ref message m)//loop monitor Windows messages
{
const int Wm_hotkey = 0x0312;//Press shortcut key
Switch (m.msg)
{
Case Wm_hotkey:
Processhotkey ()//calling the main handler
Break
}
Base. WndProc (ref m);
}
}
}



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.