Domanda

    

Questa domanda ha già una risposta qui:

    
            
  •              Simulazione della pressione dei tasti c #                                      7 risposte                          
  •     
    

Sto avviando un processo da un'applicazione Windows. Quando premo un pulsante voglio simulare la pressione del tasto F4 in quel processo. Come posso farlo?

[Modifica successiva] Non voglio simulare la pressione del tasto F4 nel mio modulo, ma nel processo che ho avviato.

È stato utile?

Soluzione

Per inviare la chiave F4 a un altro processo dovrai attivare quel processo

http://bytes.com/groups/net-c/ 230693-activ-other-process suggerisce:

  1. Ottieni l'istanza della classe Process restituita da Process.Start
  2. Query Process.MainWindowHandle
  3. Chiama la funzione API Win32 non gestita " ShowWindow " o " SwitchToThisWindow "

Potresti quindi essere in grado di utilizzare System.Windows.Forms.SendKeys.Send (" {F4} ") come Reed ha suggerito di inviare i tasti a questo processo

EDIT:

L'esempio di codice seguente esegue il blocco note e invia " ABC " ad esso:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TextSendKeys
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        static void Main(string[] args)
            {
                Process notepad = new Process();
                notepad.StartInfo.FileName = @"C:\Windows\Notepad.exe";
                notepad.Start();

                // Need to wait for notepad to start
                notepad.WaitForInputIdle();

                IntPtr p = notepad.MainWindowHandle;
                ShowWindow(p, 1);
                SendKeys.SendWait("ABC");
            }
    }
}

Altri suggerimenti

È possibile focalizzare la finestra (SetForegroundWindow WINAPI), quindi utilizzare i moduli SendKeys di Windows per inviare F4.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top