加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:48:08 所属栏目:百科 来源:网络整理
导读:我想要做的是创建一个简单的 Windows应用程序,它将自己挂钩到NotePad,然后模拟击键.我有打开NotePad的过程,将它带到前台然后模拟按下的数字1.但是,如果我单击记事本,那么任何活动状态都会成为输入的内容. 如何将此应用程序绑定到记事本,以便我可以单击并键入
我想要做的是创建一个简单的 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功能.你只需要谷歌虚拟键码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读