Sending data to a notepad using processes

后端 未结 5 1224
余生分开走
余生分开走 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!");
        }
    }
    

提交回复
热议问题