c#在另一个应用程序中写入文本 [英] c# write text in another application

查看:126
本文介绍了c#在另一个应用程序中写入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#Visual studio windows窗体:



如果没有先将应用程序放在前台,我如何在另一个应用程序中编写一些文本?



此代码强制笔记本在forground中并编写Hello World!



c# Visual studio windows form:

How do I write some text in another application without first forcing the application in the foreground ?

This code forces notebook in the forground and writes Hello World!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //Søg efter aktive vinduer
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);


        [DllImport("user32.dll")]
        private static extern bool RegisterHotkey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);



        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Gem vindue som thisWindow
            IntPtr thisWindow = FindWindow(null, "Unavngivet - Notesblok");

            // Sæt thisWindow i forgrunden
            SetForegroundWindow(thisWindow);
            SendKeys.Send("Hello World!");


        }

    }
}

推荐答案

请看我对这个问题的评论。



您可以在我最近的答案中找到一些解决方案:以编程方式按Windows 8上的'& 123'触摸键盘.NET(TabTip [ ^ ]。



这是它的P / Invoke: http://www.pinvoke.net/default.aspx/user32.sendinput [ ^ ]。



-SA
Please see my comment to the question.

You can find some solution in my recent answer: Programmatically Press ‘&123′ on Windows 8 Touch Keyboard .NET (TabTip[^].

This is the P/Invoke for it: http://www.pinvoke.net/default.aspx/user32.sendinput[^].

—SA


这篇关于c#在另一个应用程序中写入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆