求思路:如何写一个C#程序去控制另外一个.exe程序?

cacaca6 2013-12-29 07:35:54
比如,我想用C#写一个程序,当程序运行的时候,会自动打开一个写字板.exe(notepad.exe),并且,我的C#程序还要模拟用户在notepad里面输入一串字 “Hello World”

这样的c#程序该怎样写?

我知道如何用C#打开写字板,非常简单,但是如何模拟用户在打开的写字板中输入字符?或者模拟鼠标运作?

多谢各路高手
...全文
583 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kxyzjm62 2014-01-02
  • 打赏
  • 举报
回复
Process.Start () SendKeys.Send
OSMeteor 2014-01-02
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
SendKeys.Send
曹总说的很明了了
编码达人 2014-01-01
  • 打赏
  • 举报
回复
我记得进程间通信可以使用多种方式: 文件映射 共享内存 匿名管道 命名管道 邮件槽 剪切板 Sockets WM_COPYDATA 等方式,第一个进程发送消息,然后被第二个进程捕获,并解析,做相应的处理
xjw163 2013-12-30
  • 打赏
  • 举报
回复
  [ DllImport("user32.dll" , EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string classname, string captionName);

        //[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
        //private extern static IntPtr FindWindowEx(IntPtr parent, IntPtr child, string classname, string captionName);

        //[DllImport("user32.dll")]
        //static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

        [ DllImport("user32.dll" )]
        [ return: MarshalAs (UnmanagedType.Bool)]
        static extern bool SetForegroundWindow( IntPtr hWnd);

        static void Main(string[] args)
        {
           
            IntPtr mwh1 = IntPtr .Zero;

            while (mwh1 == IntPtr .Zero)
            {
                Thread.Sleep(1000);
                mwh1 = FindWindow( null, "无标题 - 记事本" );
            }


            SetForegroundWindow(mwh1);
            //发送字符串
            System.Windows.Forms. SendKeys.SendWait("xjw" );
            //模拟tab键
            System.Windows.Forms. SendKeys.SendWait(" " );
            //模拟ctrl+A全选
            //System.Windows.Forms.SendKeys.SendWait("^a");
            //模拟回车键
            //System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            int n=1;
            while (n < 10)
            {
                System.Windows.Forms. SendKeys.SendWait("x" );
                Thread.Sleep(100);
                //System.Windows.Forms.SendKeys.SendWait("j");
                //Thread.Sleep(100);
                //System.Windows.Forms.SendKeys.SendWait("w");
                //Thread.Sleep(100);
                System.Windows.Forms. SendKeys.SendWait(" " );
                n += 1;
            }
飞小猪 2013-12-30
  • 打赏
  • 举报
回复
让鼠标移动的API

[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
threenewbee 2013-12-29
  • 打赏
  • 举报
回复
SendKeys.Send

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧