Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!

C#
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!");


        }

    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 26-Feb-15 16:48pm    
Why doing so? If the application is not designed for such kind of "collaboration", you should not touch it. SendKeys rarely can work.
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900