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(); }
}}
原始碼下載