0

I have a form that is TopMost = true, which has a button. I want that when I click on the text field in another application and click on the button it sends an 'example' text to the control that has cursor focus.

but I was only able to do this with the onHover event that doesn't take the focus off the component

private void button1_MouseHover(object sender, EventArgs e)
{
    SendKeys.Send("test");
}

is there any way for me to click the button to set the text on the control that has focus? similar to windows Win+V

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Just make a custom Button that is not selectable (doesn't take the focus): e.g., `public class ButtonNoSel : Button { public ButtonNoSel() { SetStyle(ControlStyles.Selectable, false); } }`. Build the Project, find the Button in the Toolbox and drop it on the Form, use it in place of the standard one. -- You could also register a HotKey and use UI Automation or `SendInput()`... – Jimi Jun 18 '22 at 13:14
  • Say more about the "other" application that has the text box you want to control by clicking the button in the "first" application - are both apps you have written yourself? If the text box is in a third-party app, what I've done in the past is use [Spy++](https://docs.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022) (part of Visual Studio) to identify the HWND in the other app, and [PInvoke](https://docs.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke) to send [WM_SETTEXT](https://docs.microsoft.com/en-us/windows/win32/winmsg/wm-settext) to it. – IVSoftware Jun 18 '22 at 14:00
  • @IVSoftware You just need to call `GetFocus()` to get the handle of the focused Control. This assuming the Control has a handle. The chance that it doesn't have one is high. BUT, if you do this by clicking a Button, then the focused Control is always your Button. Spy++ is not exactly useful in most situations (Inspect much more in this context) and surely not when you deploy. – Jimi Jun 18 '22 at 14:16
  • @Jimi I like what your saying - always looking for an easier way - are you saying you can call GetFocus() in your managed app and somehow get the **Win32** native **hwnd** from a different app that's running? I'm thinking of the time I was using my own C# app to populate some fields in `Power E*Trade` app (which is Java). The use of **Spy++** was just to one-time identify the ID that E*Trade had for that particular window. How one goes about it depends on the use case, am I wrong (serious question)? – IVSoftware Jun 18 '22 at 14:28
  • @IVSoftware With `GetForegroundWindow()` or `GetWindowThreadProcessId()` (if you have the handle of the Control) + `AttachThreadInput()`, as shown here: [How to use SendInput to simulate the UP arrow key press (or other extended keys)](https://stackoverflow.com/a/71589883/7444103) -- Otherwise, you can use UI Automation and inspect / parse the Tree of a whole Window. Of course also change the text of a child Control (handle or not handle) if it exposes Automation patterns. – Jimi Jun 18 '22 at 14:33
  • Jimi already did this with SetStyle, it removes focus the same way. IVSoftware, It's for applications in general, I want to paste a text that I want in the control where the cursor is (by clicking on the button), but it always takes the focus away. I've also tried capturing the control on the button's onHover before clicking through this process here. https://www.codeproject.com/Articles/34752/Control-in-Focus-in-Other-Processes but without success =( – Caio Souza Jun 18 '22 at 14:36
  • This is because **your Window** can be activated. You should override `CreateParams`, to set styles (`WS_EX_NOACTIVATE`) that generate a Window that doesn't activate + override `ShowWithoutActivation`. – Jimi Jun 18 '22 at 14:39
  • I forgot I've posted something similar here: [How to keep a Form always on top without stealing focus from the active Window?](https://stackoverflow.com/a/65439642/7444103). The custom Button there has the same name as the one I've poste here :) – Jimi Jun 18 '22 at 14:53
  • nice jimi, this is what i want but it is in c++, i don't know how to reproduce this in c# but give an example as a solution? anyway thank you! – Caio Souza Jun 18 '22 at 15:02
  • Override CreateParams, e.g., `protected override CreateParams CreateParams { get { var cp = base.CreateParams; cp.ExStyle |= 0x08000000; return cp; } }` (this adds `WS_EX_NOACTIVATE` to the Styles) and also add `protected override bool ShowWithoutActivation => true;`. Use the custom Button. – Jimi Jun 18 '22 at 15:13
  • @CaioSouza did you get something to work? – IVSoftware Jun 19 '22 at 01:53
  • yes, I achieved, there was something similar here too. https://stackoverflow.com/questions/156046/show-a-form-without-stealing-focus thank you all. – Caio Souza Jun 19 '22 at 04:23

0 Answers0