Fast SN Input Tool written in C # code

Source: Internet
Author: User

General software must Input Serial Number (SN), and we usually use the most pirated software, usually the serial number of pirated software (SN) are saved as: XXXXX-XXXXX-XXXX-XXXX form.

The serial number entered by the software is usually composed of several TextBox. Copying XXXXX to the text box one by one is very troublesome. Therefore, the SN quick input tool is generated.

Of course, these are irrelevant to the reason why I wrote this program. I wrote this program purely because a netizen and his uncle bet that they would write a program, and his uncle asked him to write it, however, unfortunately, this netizen is a beginner in programming (a rookie who is better than me). Of course, this seemingly simple program cannot be completed, and many programming knowledge programs are actually used.

To do this program, you must first understand the functions of the program. Its function is to let you copy the serial number in the form of "XXXXX-XXXXX-XXXX-XXXX", when you point to the text box, the program will automatically add XXXXX to the corresponding text box.

Since we want to process the copy serial number, we must use something related to the clipboard. Clipboard. Fortunately, I used it N times in C # before and no longer need to check windows APIs. C # provides the Clipboard class.

The string Clipboard is used. getText () is a static method that extracts the serial number with-copied just now, and saves strKeys as a string variable in my program for ease of use.

The first step is to retrieve data from the clipboard.

Next, we should consider how to process our data. Our data is finally written into several consecutive text boxes, so we can consider using String. the Split (char [], string splitoption) method splits the serial number into several substrings, and then outputs the text to the corresponding textbox handle through the windows api. However, this will undoubtedly increase the difficulty of the program. After switching several consecutive text boxes, you can use the Tab key to output the text to the text box, just press the keyboard and you will be OK. Obviously, we only need to simulate the key we want to press. In this case, I first think of the keyboard simulation event keybd_event in windows api, so I started to query the keybd_event method in MSDN. There is a KEYEVENTF_KEYUP parameter in the method, but I don't know its corresponding value, so I began to look for the value of this long integer. But I couldn't find it all the time. When I searched for KEYUP related items in MSDN, I suddenly found the System. Windows. Form. SendKeys class. It turns out that the. net framework has encapsulated the keybd_event method of this unmanaged object into the SendKeys class. directly using the SendKeys class can simulate keyboard operations.

The syntax for querying the Tab key is {Tab }.

Then I just need to convert all-in the original text strKeys into {Tab} and then hand it over to the SendKeys class for processing. This program is basically complete.

So now

StrKeys. Replace ("-", "{TAB }");
SendKeys. Send (strKeys );

These two lines of code.

In this way, the main process of my program is available:

Private void ProcessHotkey () // main handler
{
StrKeys = Clipboard. GetText ();
StrKeys. Replace ("-", "{TAB }");
SendKeys. Send (strKeys );
}

But how can we use the shortcut key to trigger this process.

So I started to search for windows api related to global shortcuts on Baidu and MSDN.

To set the 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
);

To convert the code to C #, you must first reference the namespace System. Runtime. InteropServices; to load the unmanaged class user32.dll. So there is:

[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 method for registering and uninstalling the global shortcut key, so we only need to add the statement for registering the shortcut key when Form_Load, and uninstall the global shortcut key when FormClosing. At the same time, in order to ensure that the content of the clipboard is not affected by the call of the clipboard by other programs, I first cleared the content in the clipboard during Form_Load.

So there is:

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

Clipboard. Clear (); // first Clear the Clipboard to prevent other contents from being copied in the Clipboard.
RegisterHotKey (Handle, 100, 0, Keys. F10 );
}

Private void form=formclosing (object sender, FormClosingEventArgs e)
{
UnregisterHotKey (Handle, 100); // uninstall the shortcut key
}

In other windows, how can we call my main process ProcessHotkey () after pressing the shortcut key?

Then we must rewrite the WndProc () method to call the process by monitoring system messages:

Protected override void WndProc (ref Message m) // monitor Windows messages
{
Const int WM_HOTKEY = 0x0312; // press the shortcut key
Switch (m. Msg)
{
Case WM_HOTKEY:
ProcessHotkey (); // call the main handler
Break;
}
Base. WndProc (ref m );
}

In this way, my program is complete.

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>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

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

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
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 = "Usage :";
//
// 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
//

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.