0

I want to paste text from my text box or rich text box in C# windows form application to out side of the form for example:

//on a button click event
textbox1.text=Clipboard.SetText();  // this will set text to clipboard 

now I want when I click in address bar of Firefox or Google chrome to get the same text as I typed in my windows form application, as I can do it by CTRL+V but I want a C# program to do so for me and get text from clipboard when ever I click in address bar or rename a folder.

GrahamMc
  • 3,034
  • 2
  • 24
  • 29

2 Answers2

1

You could just turn on some windows disability settings, If dragging or pasting is too awkward.

If you really want to implement this, you need a global hook on the mouse so you can recieve mouse events from outside your application. See here or perhaps here.

Then you have a problem, do you want to paste anywhere or just in address bars. First you'll have to define what an address bar window is and work out if that is what has the focus.

Its a lot of effort and the behaviour is not especially desirable. If you really want this, please expand your question and improve its readability so that this post will be useful to future visitors.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

This is completely untested, and I've never used these DLL calls in C#, but hopefully it will do the trick or at least come close...

public class Win32
{
   public const uint WM_SETTEXT = 0x000c;

   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
   public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);

   [DllImport("user32.dll")]
   static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
}

Elsewhere in the code...

IntPtr theHandle = WindowFromPoint(Cursor.Position.X, Cursor.Position.Y);

if (theHandle != null)
{
   long res = Win32.SendMessage(theHandle, WM_SETTEXT, IntPtr.Zero, Clipboard.GetText());
}

Note: I'm not entirely sure that WindowFromPiont will get the handle of the child control (i.e. the actual textbox) of another window, rather than the handle of the window itself. You might have to find the child by the cursor position. Been a long time since I've done something like this, unfortunately.

Also, if you want to get a fancier with the acquisition of the window handle, see this question: getting active window name based on mouse clicks in c#

Community
  • 1
  • 1
TimFoolery
  • 1,895
  • 1
  • 19
  • 29