C # training ground (4) Create System hotkeys and enter information in the activity window

Source: Internet
Author: User
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

  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. componentmodel;
  4. UsingSystem. Data;
  5. UsingSystem. drawing;
  6. UsingSystem. LINQ;
  7. UsingSystem. text;
  8. UsingSystem. Windows. forms;
  9. UsingSystem. runtime. interopservices;
  10. UsingSystem. diagnostics;
  11. NamespaceWindowsformsapplication1
  12. {
  13. PublicPartialClassForm1: Form
  14. {
  15. [System. runtime. interopservices. dllimport ("User32.dll")]
  16. Public Static Extern BoolRegisterhotkey (
  17. Intptr hwnd,// Handle to window
  18. IntID,// Hot Key Identifier
  19. UintFsmodifiers,// Key-modifier options
  20. Keys VK// Virtual-key code
  21. );
  22. [System. runtime. interopservices. dllimport ("User32.dll")]// Declare the API Function
  23. Public Static Extern BoolUnregisterhotkey (
  24. Intptr hwnd,// Handle to window
  25. IntID// Hot Key Identifier
  26. );
  27. [Dllimport ("User32.dll", Charset = charset. Auto, exactspelling =True)]
  28. Public Static ExternIntptr getforegroundwindow ();
  29. Public EnumKeymodifiers
  30. {
  31. None = 0,
  32. Alt = 1,
  33. Control = 2,
  34. Shift = 4,
  35. Windows = 8
  36. }
  37. PublicForm1 ()
  38. {
  39. Initializecomponent ();
  40. Registerhotkey (handle, 150, 4, keys. );// Hotkey 1: Shift +
  41. Registerhotkey (handle, 151, 2 | 4, keys. m );// Hotkey 1: Ctrl + Shift + m
  42. // Registerhotkey (handle, 152, 1 | 2, keys. Up); // key 1: CTRL + ALT + cursor arrow
  43. // Registerhotkey (handle, 153, 8, keys. p); // hotkey 1: Win + P
  44. }
  45. Protected Override VoidWndproc (RefMessage m)// Monitor Windows messages
  46. {
  47. Const IntWm_hotkey = 0x0312;// If M. MSG is 0x0312, the user presses the hot key.
  48. Switch(M. msg)
  49. {
  50. CaseWm_hotkey:
  51. Processhotkey (m );// Call the processhotkey () function when you press the hot key.
  52. Break;
  53. }
  54. Base. Wndproc (RefM );// Pass the system message from the wndproc of the parent class
  55. }
  56. Private VoidProcesshotkey (message m)
  57. {
  58. Intptr id = M. wparam;// Intptr indicates the platform-specific type of pointer or handle
  59. // MessageBox. Show (Id. tostring ());
  60. StringSID = ID. tostring ();
  61. Switch(SID)
  62. {
  63. Case "150": Opennewtxt ();Break;// Hotkey 1: Ctrl +
  64. Case "151": Sendmsg ();Break;// Hotkey 1: Ctrl + Shift + m
  65. Case "152":// Hotkey 1: CTRL + ALT + cursor up arrow
  66. This. Visible =True;
  67. Break;
  68. Case "153":// Hotkey 1: Win + P
  69. This. Visible =False;
  70. Break;
  71. }
  72. }
  73. Private VoidOpennewtxt ()
  74. {
  75. Process mytxt =NewProcess ();
  76. Mytxt. startinfo. filename = @"Notepad.exe";
  77. Mytxt. startinfo. Arguments = @"D: // newtxt.txt";
  78. Mytxt. Start ();
  79. Mytxt. waitforinputidle (1000 );
  80. Sendkeys. sendwait ("Hello! ");
  81. }
  82. Private VoidSendmsg ()
  83. {
  84. // String proname = "";
  85. Process mypro =Null;
  86. Foreach(Process thisprocInProcess. getprocesses ())
  87. {
  88. If(Thisproc. main1_whandle. toint32 () = getforegroundwindow (). toint32 ())
  89. {
  90. // Proname = thisproc. processname;
  91. Mypro = thisproc;
  92. Break;
  93. }
  94. }
  95. Try
  96. {
  97. // Two methods
  98. // String msginfo = @ "{enter}/SUM _ {} I = 0 {}}{^}{{}/infty {}{ enter }";
  99. Sendkeys. Send (@"{Enter}/begin {} split {}}{ enter} & = {enter}/end {} split {}}{ enter }");
  100. // Sendkeys. Send (msginfo );
  101. // Sendkeys. Send (@ "/begin {{} split {}}");
  102. // Sendkeys. Send ("{enter }");
  103. // Sendkeys. Send (@"{(}{)}");
  104. // Sendkeys. Send ("{enter }");
  105. // Sendkeys. Send (@ "/sum_0 {^} {{}/infinite {}}");
  106. // Sendkeys. Send ("{enter }");
  107. // Sendkeys. Send (@ "/end {{} split {}} haha ");
  108. // Sendkeys. Send ("{enter }");
  109. // Use clipboard for testing
  110. StringMsginfo2 = @"/Begin {split }"+"/R/N"+ @"& ="+"/R/N"+ @"/End {split }";
  111. Clipboard. setdataobject (msginfo2 );
  112. // Sendkeys. Send ("^ V ");
  113. }
  114. Catch(System. nullreferenceexception E)
  115. {
  116. // MessageBox. Show (E. Message );
  117. }
  118. }
  119. Private VoidForm1_formclosing (ObjectSender, formclosingeventargs E)
  120. {
  121. Unregisterhotkey (handle, 150 );// Uninstall 1st shortcut keys
  122. Unregisterhotkey (handle, 151 );// Upload 2nd shortcut keys
  123. Unregisterhotkey (handle, 152 );// Uninstall 3rd shortcut keys
  124. Unregisterhotkey (handle, 153 );// Upload 4th shortcut keys
  125. }
  126. }
  127. }

 

Running result:

In the word editing window, press Ctrl + Shift + m to enter the following text at the current cursor:

/Begin {/split}

& =

& =

/End {/split}

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.