Send clipboard content to another application

kutiJack
kutiJack used Ask the Experts™
on
How can be clipboard content sent from C# app to another application?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can you please explain elaborately?

Author

Commented:
1. I fill listBox in my C# application with the names of all open windows.  No problem
2. I select one of them, (Word document for example) to which I want to send clipboard content.  No problem, of course
3. I press button and selected window gets focus. No problem
4. The clipboard content is pasted into the focused window.  Big problem for me!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
4. The clipboard content is pasted into the focused window.

How about trying the simplest solution first?  Just press Ctrl-V:

    SendKeys.Send("^V");
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I tried it first, but found this not too reliable. Sometimes it works, sometimes it doesn´t and that´s why I am asking for some other more reliable way.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Are you only pasting plain text?...can you give more details on the "big picture"?

Author

Commented:
I would like this application would be able to paste various kinds of data, including images. Btw. I didn´t understand, what you ment by "big picture".
Without knowing more specific information (a more complete list of the receiving applications, controls, file types, etc. - I think that is what was meant by "Big Picture"), this is a little difficult to answer...  However, what about SendMessage?:

'Example (for an "Edit" control):

     Private Const WM_SETTEXT As Long = &HC

     Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long

     lRet = SendMessage(hwnd, WM_SETTEXT, 0, strClipboardStringData)

where 'hwnd' is the handle of the receiving "Edit" control, and strClipboardStringData is self-explanatory (it seems you already know how to copy the data from the clipboard).
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
The would be the most widely implemented message...but it only accepts PLAIN text.  =\

Author

Commented:
I plan to use this application for collecting and storing pieces of text and pictures when browsing web or reading e-documents. This would be very useful to copy all interesting information without the need to paste it every time. And at the end of research, I select the the desired text processor - maybe Word or so, press button on my application and all collected text and images are pasted all together at once into that processor. That´s my idea and so, my C# application should be able to paste plain text, rich text, images copied from web, pictures copied from documents ...

As I know, WM_SETTEXT message can´t be used here, because it only sets text of edit control or combo box, but for external applications it sets the title text. I tried to use WM_PASTE instead, it could paste text into richTextBox on my app, but when I tried to use it for notepad, it failed, although I provided handle to it and called SetForegroundWindow function before pasting. Notepad got focus, but nothing was pasted.

I spent hours searching for solution but without succes. Can you help me?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Notepad is a great example...since you don't send the input to the main window handle!  You actually have to send the messages to the  edit window which is a child of the main window.

Great illustration of how you can't just send things to the main handle...  =\
Public Class Form1
 
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
        ByVal lpsz2 As String) As Integer
 
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
        ByVal lParam As String) As Integer
 
    Private Const WM_GETTEXTLENGTH = &HE
    Private Const EM_SETSEL = &HB1
    Private Const EM_REPLACESEL = &HC2
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim p As Process
        If Process.GetProcessesByName("Notepad").Length = 0 Then
            p = System.Diagnostics.Process.Start("notepad.exe")
            p.WaitForInputIdle()
        Else
            p = Process.GetProcessesByName("Notepad")(0)
        End If
        Dim edit_hWnd As Integer = FindWindowEx(p.MainWindowHandle().ToInt32, 0, "Edit", "")
        Dim textLength As Integer = SendMessage(edit_hWnd, WM_GETTEXTLENGTH, 0, 0)
        SendMessage(edit_hWnd, EM_SETSEL, textLength, 0) ' put cursor at end of the edit box
        SendMessage(edit_hWnd, EM_REPLACESEL, 0, "Hello notepad!") ' append string to end of edit box
    End Sub
 
End Class

Open in new window

Author

Commented:
Thank you, for notepad it works well, but how about MS Word, it doesn´t have "Edit" window. Can you tell me what window should FindWindowEx function address to with MS Word?
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
The REAL point of my Notepad post was that you had to CUSTOMIZE the code for pasting to that particular app... thus making your app NON GENERIC.

I thought the point was to be able to post to ANY app...this approach is counter productive.

If you start figuring out exactly which child control needs to receive the message then where does this lead you?  You can't possibly do this for every app on the planet!  =\

Author

Commented:
Thank you, at least, I learned something new and changed my approach to programming this utility.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial