Sending data to a notepad using processes

后端 未结 5 1165
余生分开走
余生分开走 2021-01-23 21:11

I want to send every item from my listbox to a notepad,but my logic is kinda beating me.

private void send_Click(object sender, EventArgs e)
{
    var notepad =          


        
相关标签:
5条回答
  • 2021-01-23 21:48

    An another option you can find the Edit control of notepad using FindWindowEx and send a WM_SETTEXT message to it using SendMessage. This way you don't need to bring the notepad window to front.

    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
        string lpszClass, string lpszWindow);
    [DllImport("User32.dll")]
    static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, string 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!");
        }
    }
    
    0 讨论(0)
  • 2021-01-23 21:55

    You can do it with 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.ToString());
                            input.Keyboard.KeyPress(VirtualKeyCode.RETURN);
    
                        }
                    }
                }
    
    0 讨论(0)
  • 2021-01-23 22:03

    I have a sample test, it works if you change SendKeys.Send to SendKeys.SendWait

        List<string> data = new List<string>() { "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}");
        }
    

    Because the key applied later than update Clipboard and loop, so that issue happend.

    0 讨论(0)
  • 2021-01-23 22:06

    Better than using System.Threading.Thread.Sleep(); use await Task.Delay(); https://stackoverflow.com/a/20084603/6886308

    0 讨论(0)
  • 2021-01-23 22:13

    From @GillBates suggestion I used System.Threading.Thread.Sleep(); and it seem to work great now.

    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);
                    string text = "";
    
                    foreach (var item in listBox1.Items)
                    {
                        text = item.ToString();
                        Clipboard.SetText(text);
                        SendKeys.Send("^V");
                        SendKeys.Send("{ENTER}");
                        System.Threading.Thread.Sleep(150);
                    }
    
                }
    }
    
    0 讨论(0)
提交回复
热议问题