C # global hotkey setting and form hotkey setting,
1. Form hotkeys
First, set the KeyPreview of the main form to true. You can directly set it in the attribute,
Or set this. KeyPreview = true in form loading;
Then add the form KeyDown event as follows:
private void FrmMain_KeyDown(object sender, KeyEventArgs e)
{If (e. alt & e. shift & e. control & e. keyCode = Keys. s) {MessageBox. show ("I pressed Control + Shift + Alt + S ");}}
2. Global hot key settings
Define API functions register hotkeys and uninstall hotkeys
I have defined the AppHotKey class here. The Code is as follows:
Public class AppHotKey {[DllImport ("kernel32.dll")] public static extern uint GetLastError (); // if the function is successfully executed, the return value is not 0. // If the function fails to be executed, the return value is 0. To get the extended error message, call GetLastError. [DllImport ("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey (IntPtr hWnd, // handle int id of the window to define the hot key, // define the hotkey ID (which cannot be repeated with other IDS) KeyModifiers fsModifiers, // identify whether the key will take effect when pressing Alt, Ctrl, Shift, Windows, and other Keys. Keys vk // defines the key content); [DllImport ("user32.dll ", setLastError = true)] public static extern bool UnregisterHotKey (IntPtr hWnd, // int id of the handle to the window whose hotkey is to be canceled // ID of the window whose hotkey is to be canceled ); // defines the name of the secondary key (convert numbers into characters for memory and removal) This enumeration directly uses the value) [Flags ()] public enum KeyModifiers {None = 0, Alt = 1, Ctrl = 2, Shift = 4, windowsKey = 8} /// <summary> // register the hotkey // </summary> /// <param name = "hwnd"> window handle </param>/ // <param name = "hotKey_id"> hotkey ID </param> // <param name = "keyModifiers"> key combination </param> // <param name =" key "> hotkey </param> public static void RegKey (IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key) {try {if (! RegisterHotKey (hwnd, hotKey_id, keyModifiers, key) {if (empty Al. GetLastWin32Error () = 1409) {MessageBox. Show ("The Hot key is occupied! ");} Else {MessageBox. Show (" An error occurred while registering the hotkey! ") ;}} Catch (Exception) {}}/// <summary> /// cancel the hotkey // </summary> /// <param name = "hwnd"> window handle </param> // /<param name = "hotKey_id"> hotkey ID </param> public static void UnRegKey (IntPtr hwnd, int hotKey_id) {// set the UnregisterHotKey (hwnd, hotKey_id) for the hotkey whose ID is hotKey_id );}}
Override the WndProc function of the form, register the hotkey when the window is created, and destroy the hotkey when the window is destroyed,The Code is as follows:
Private const int WM_HOTKEY = 0x312; // window message-hotkey private const int WM_CREATE = 0x1; // window message-create private const int WM_DESTROY = 0x2; // window Message-destroy private const int Space = 0x3572; // hotkey ID protected override void WndProc (ref Message m) {base. wndProc (ref m); switch (m. msg) {case WM_HOTKEY: // window message-hotkey ID switch (m. WParam. toInt32 () {case Space: // hotkey ID MessageBox. show ("I pressed Control + Shift + Alt + S"); break; default: break;} break; case WM_CREATE: // window message-create AppHotKey. regKey (Handle, Space, AppHotKey. keyModifiers. ctrl | AppHotKey. keyModifiers. shift | AppHotKey. keyModifiers. alt, Keys. s); break; case WM_DESTROY: // window message-destroy AppHotKey. unRegKey (Handle, Space); // destroy the hot key break; default: break ;}}
A simple program of C language Bubble Sorting
Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}
--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.
Bubble Sorting
1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.
(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.
2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49
3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.
(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">
A simple program of C language Bubble Sorting
Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}
--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.
Bubble Sorting
1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.
(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.
(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.
2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49
3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.
(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">