C#将控制台应用程序挂钩到记事本

我想要做的是创建一个简单的 Windows应用程序,它将自己挂钩到NotePad,然后模拟击键.我有打开NotePad的过程,将它带到前台然后模拟按下的数字1.但是,如果我单击记事本,那么任何活动状态都会成为输入的内容.

如何将此应用程序绑定到记事本,以便我可以单击并键入任何内容,此应用程序仍将命令推入记事本?

This is the DLL i’m using to simulate keypressing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using WindowsInput;

namespace NotePadTesting
{
    class Program
    {
        [DllImport("USER32.DLL",CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);


        static void Main(string[] args)
        {

            Process[] processes = Process.GetProcessesByName("notepad");

            if (processes.Length == 0)
            {

                Process.Start("notepad.exe");
                processes = Process.GetProcessesByName("notepad");

            }
            if (processes.Length == 0)
            {
                throw new Exception("Could not find notepad huh....");
            }

            IntPtr WindowHandle = processes[0].MainWindowHandle;
            SetForegroundWindow(WindowHandle);

            for (int i = 0; i < 500; i++)
            {
                System.Threading.Thread.Sleep(100);
                InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);

            }


        }
    }
}

解决方法

如果您已经拥有要输入的窗口的句柄,则可以使用 PostMessage功能.你只需要谷歌虚拟键码.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...