C# 프로그램 에뮬레이션 키보드 입력을 수첩에 추가

2428 단어 공책
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();

        }
} }

소스 코드 다운로드

좋은 웹페이지 즐겨찾기