C # add content to notepad by simulating keyboard input

Source: Internet
Author: User
Using system; using system. windows. forms; using system. runtime. interopservices; namespace test {public partial class form1: FORM {public form1 () {initializecomponent ();} // The first and third are the [dllimport ("user32.dll")] public static extern intptr findwindow (string lpclassname, string lpwindowname) used to find the window handle ); [dllimport ("user32.dll", entrypoint = "sendmessage")] Private Static extern int sendmessage (intptr hwnd, Int MSG, intptr wparam, string lparam); [dllimport ("user32.dll")] public static extern intptr find1_wex (intptr parent, intptr Childe, string strclass, string frmtext ); [dllimport ("user32.dll")] public static extern bool setforegroundwindow (intptr hwnd); bool a = true; private void button#click (Object sender, eventargs e) {test (); if (A = true) {this.txt text. TEXT = "Lele ...! "; Form2 for2 = new form2 (); for2.showdialog ();} A = false;} hotkeys H = new hotkeys (); Private void form1_load (Object sender, eventargs E) {// enter the CTRL + e shortcut key H. regist (this. handle, (INT) hotkeys. hotkeymodifiers. control, keys. e, callback); // MessageBox. show ("registered");} // reload the wndproc function protected override void wndproc (ref message m) {H. processhotkey (m); // shortcut key message processing base. wndproc (ref m);} // crossProgram Enter public void test () {const int wm_settext = 0x000c; intptr hwnd = findwindow (null, "untitled-Notepad"); intptr htextbox = find1_wex (hwnd, intptr. zero, "edit", null); intptr htextbox2 = find1_wex (hwnd, htextbox, "edit", null); // enter the last obtained handle to obtain the next handle. Sendmessage (htextbox, wm_settext, intptr. Zero, this.txt text. Text);} // method called when the shortcut key is pressed public void callback () {test ();}
}}

Source code download

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.