Envoi de données à un bloc-notes à l’aide de processus

Je veux envoyer chaque élément de ma liste dans un bloc-notes, mais ma logique me bat un peu.

private void send_Click(object sender, EventArgs e) { var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad"); if (notepad != null) { if (IsIconic(notepad.MainWindowHandle)) ShowWindow(notepad.MainWindowHandle, 9); SetForegroundWindow(notepad.MainWindowHandle); ssortingng text = ""; foreach (var item in listBox1.Items) { text = item.ToSsortingng(); Clipboard.SetText(text); SendKeys.Send("^V"); SendKeys.Send("{ENTER}"); } } } 

Dans ma logique, cela devrait envoyer chaque élément de la zone de liste à un bloc-notes, chaque élément d’une autre ligne.Mais cela ne se produit pas à chaque fois, parfois il n’envoie que le dernier élément de la liste, car il y a beaucoup d’éléments dans la liste. . Est-ce que je manque quelque chose?

Une autre option FindWindowEx rechercher le contrôle Edit du bloc-notes à l’aide de FindWindowEx et à lui envoyer un message WM_SETTEXT à l’aide de SendMessage . De cette façon, vous n’avez pas besoin de mettre la fenêtre du bloc-notes au premier plan.

 using System.Diagnostics; using System.Runtime.InteropServices; 
 [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, ssortingng lpszClass, ssortingng lpszWindow); [DllImport("User32.dll")] static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, ssortingng lParam); const int WM_SETTEXT = 0x000C; private void button1_Click(object sender, EventArgs e) { //Find notepad by its name, or use the instance which you opened yourself var notepad = Process.GetProcessesByName("notepad").FirstOrDefault(); if(notepad!=null) { var edit = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null); SendMessage(edit, WM_SETTEXT, IntPtr.Zero, "This is a Text!"); } } 

J’ai un exemple de test, cela fonctionne si vous modifiez SendKeys.Send à SendKeys.SendWait

  List data = new List() { "Test", "hope", "It", "works","Or" }; foreach (var item in data) { Clipboard.Clear(); Clipboard.SetText(item); //SendKeys.Send("^V"); //SendKeys.Send("{ENTER}"); SendKeys.SendWait("^V"); SendKeys.SendWait("{ENTER}"); } 

Parce que la clé a été appliquée après la mise à jour du Presse-papiers et de la boucle, le problème est donc survenu.

Vous pouvez le faire avec InputSimulator, Package Manage Console: Install-Package InputSimulator

  private void Button_Click_1(object sender, RoutedEventArgs e) { var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad"); if (notepad != null) { if (IsIconic(notepad.MainWindowHandle)) ShowWindow(notepad.MainWindowHandle, 9); var input = new InputSimulator(); SetForegroundWindow(notepad.MainWindowHandle); foreach (var item in listBox1.Items) { input.Keyboard.TextEntry(item.ToSsortingng()); input.Keyboard.KeyPress(VirtualKeyCode.RETURN); } } } 

Mieux que d’utiliser System.Threading.Thread.Sleep(); utilisez await Task.Delay(); https://stackoverflow.com/a/20084603/6886308

De la suggestion de @GillBates, j’ai utilisé System.Threading.Thread.Sleep(); et cela semble bien fonctionner maintenant.

 private void send_Click(object sender, EventArgs e) { var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad"); if (notepad != null) { if (IsIconic(notepad.MainWindowHandle)) ShowWindow(notepad.MainWindowHandle, 9); SetForegroundWindow(notepad.MainWindowHandle); ssortingng text = ""; foreach (var item in listBox1.Items) { text = item.ToSsortingng(); Clipboard.SetText(text); SendKeys.Send("^V"); SendKeys.Send("{ENTER}"); System.Threading.Thread.Sleep(150); } } }