C#程式類比鍵盤輸入向記事本內新增內容

來源:互聯網
上載者:User
using System;using System.Windows.Forms;using System.Runtime.InteropServices;namespace Test{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //第一個與第三個是用於尋找視窗控制代碼的        [DllImport("user32.dll")]        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        [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 FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);        [DllImport("USER32.DLL")]        public static extern bool SetForegroundWindow(IntPtr hWnd);        bool a = true;        private void button1_Click(object sender, EventArgs e)        {            Test();            if (a == true)            {                this.txtText.Text = "樂樂…!";                Form2 for2 = new Form2();                for2.ShowDialog();            }            a = false;        }        HotKeys h = new HotKeys();        private void Form1_Load(object sender, EventArgs e)        {            //這裡註冊了Ctrl+E 快速鍵            h.Regist(this.Handle, (int)HotKeys.HotkeyModifiers.Control, Keys.E, CallBack);            //MessageBox.Show("註冊成功");        }        //重載WndProc函數        protected override void WndProc(ref Message m)        {            h.ProcessHotKey(m);//快速鍵訊息處理            base.WndProc(ref m);        }        //跨越程式輸入        public void Test()        {             const int WM_SETTEXT = 0x000C;            IntPtr hwnd = FindWindow(null, "無標題 - 記事本");            IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);            IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "EDIT", null);//填上次獲得的控制代碼,可以得到下一個的控制代碼。            SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, this.txtText.Text);        }        //按下快速鍵時被調用的方法        public void CallBack()        {            Test();        }
}}

原始碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.