This article reprinted http://blog.csdn.net/imbiz/article/details/5648682
Exercise Using API.
Implement the following functions:
1. Set the hotkey
2. Check the current activity window
3. Send messages to the activity window
4. Cancel the registration hotkey
[C-sharp] View plaincopy
-
- UsingSystem;
-
- UsingSystem. Collections. Generic;
- UsingSystem. componentmodel;
-
- UsingSystem. Data;
-
- UsingSystem. drawing;
-
- UsingSystem. LINQ;
-
- UsingSystem. text;
-
- UsingSystem. Windows. forms;
-
- UsingSystem. runtime. interopservices;
-
- UsingSystem. diagnostics;
-
-
- NamespaceWindowsformsapplication1
-
- {
-
- PublicPartialClassForm1: Form
-
- {
-
-
- [System. runtime. interopservices. dllimport ("User32.dll")]
- Public Static Extern BoolRegisterhotkey (
-
- Intptr hwnd,// Handle to window
-
- IntID,// Hot Key Identifier
- UintFsmodifiers,// Key-modifier options
-
- Keys VK// Virtual-key code
-
- );
-
- [System. runtime. interopservices. dllimport ("User32.dll")]// Declare the API Function
-
- Public Static Extern BoolUnregisterhotkey (
-
- Intptr hwnd,// Handle to window
- IntID// Hot Key Identifier
-
- );
-
-
-
- [Dllimport ("User32.dll", Charset = charset. Auto, exactspelling =True)]
- Public Static ExternIntptr getforegroundwindow ();
-
-
-
- Public EnumKeymodifiers
-
- {
-
- None = 0,
- Alt = 1,
-
- Control = 2,
-
- Shift = 4,
-
- Windows = 8
-
- }
-
-
-
- PublicForm1 ()
- {
-
- Initializecomponent ();
-
- Registerhotkey (handle, 150, 4, keys. );// Hotkey 1: Shift +
-
- Registerhotkey (handle, 151, 2 | 4, keys. m );// Hotkey 1: Ctrl + Shift + m
-
- // Registerhotkey (handle, 152, 1 | 2, keys. Up); // key 1: CTRL + ALT + cursor arrow
- // Registerhotkey (handle, 153, 8, keys. p); // hotkey 1: Win + P
-
-
- }
-
- Protected Override VoidWndproc (RefMessage m)// Monitor Windows messages
- {
-
- Const IntWm_hotkey = 0x0312;// If M. MSG is 0x0312, the user presses the hot key.
-
- Switch(M. msg)
-
- {
- CaseWm_hotkey:
-
- Processhotkey (m );// Call the processhotkey () function when you press the hot key.
-
- Break;
- }
-
-
- Base. Wndproc (RefM );// Pass the system message from the wndproc of the parent class
-
- }
-
- Private VoidProcesshotkey (message m)
-
- {
-
- Intptr id = M. wparam;// Intptr indicates the platform-specific type of pointer or handle
-
- // MessageBox. Show (Id. tostring ());
- StringSID = ID. tostring ();
-
- Switch(SID)
-
- {
-
- Case "150": Opennewtxt ();Break;// Hotkey 1: Ctrl +
- Case "151": Sendmsg ();Break;// Hotkey 1: Ctrl + Shift + m
-
- Case "152":// Hotkey 1: CTRL + ALT + cursor up arrow
-
- This. Visible =True;
- Break;
-
- Case "153":// Hotkey 1: Win + P
-
- This. Visible =False;
- Break;
-
- }
-
- }
-
-
- Private VoidOpennewtxt ()
-
- {
- Process mytxt =NewProcess ();
-
- Mytxt. startinfo. filename = @"Notepad.exe";
-
- Mytxt. startinfo. Arguments = @"D: // newtxt.txt";
-
- Mytxt. Start ();
-
- Mytxt. waitforinputidle (1000 );
- Sendkeys. sendwait ("Hello! ");
-
-
-
-
- }
-
- Private VoidSendmsg ()
-
- {
- // String proname = "";
-
- Process mypro =Null;
-
- Foreach(Process thisprocInProcess. getprocesses ())
-
- {
- If(Thisproc. main1_whandle. toint32 () = getforegroundwindow (). toint32 ())
-
- {
-
- // Proname = thisproc. processname;
-
- Mypro = thisproc;
- Break;
-
- }
-
- }
-
- Try
-
- {
-
- // Two methods
- // String msginfo = @ "{enter}/SUM _ {} I = 0 {}}{^}{{}/infty {}{ enter }";
-
- Sendkeys. Send (@"{Enter}/begin {} split {}}{ enter} & = {enter}/end {} split {}}{ enter }");
-
- // Sendkeys. Send (msginfo );
- // Sendkeys. Send (@ "/begin {{} split {}}");
-
- // Sendkeys. Send ("{enter }");
-
- // Sendkeys. Send (@"{(}{)}");
-
- // Sendkeys. Send ("{enter }");
- // Sendkeys. Send (@ "/sum_0 {^} {{}/infinite {}}");
-
- // Sendkeys. Send ("{enter }");
-
- // Sendkeys. Send (@ "/end {{} split {}} haha ");
-
- // Sendkeys. Send ("{enter }");
- // Use clipboard for testing
-
- StringMsginfo2 = @"/Begin {split }"+"/R/N"+ @"& ="+"/R/N"+ @"/End {split }";
-
- Clipboard. setdataobject (msginfo2 );
- // Sendkeys. Send ("^ V ");
-
-
- }
-
- Catch(System. nullreferenceexception E)
-
- {
-
- // MessageBox. Show (E. Message );
- }
-
- }
-
-
- Private VoidForm1_formclosing (ObjectSender, formclosingeventargs E)
-
- {
- Unregisterhotkey (handle, 150 );// Uninstall 1st shortcut keys
-
- Unregisterhotkey (handle, 151 );// Upload 2nd shortcut keys
-
- Unregisterhotkey (handle, 152 );// Uninstall 3rd shortcut keys
- Unregisterhotkey (handle, 153 );// Upload 4th shortcut keys
-
-
-
- }
-
-
-
-
-
-
-
-
-
- }
-
-
- }
Running result:
In the word editing window, press Ctrl + Shift + m to enter the following text at the current cursor:
/Begin {/split}
& =
& =
/End {/split}