Here is my scenario:
A user has two applications open. Let's say one application is Notepad with some text in it and the other one is my C# application.
A user now positions the mouse cursor somewhere inside the Notepad text and then clicks a button in my C# application. As a result, a text string from my application gets pasted in Notepad where the cursor was positioned.
My question is: what would a general approach be to accomplish the above, and possibly what classes etc. are recommended?
I would look into using interop calls to accomplish this. Look at specifically GetWindow() and SendMessage with the WM commands (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx#system_defined) and GetDesktop().
GetDesktop will allow you to obtain all of the top level child windows of the desktop (all top level windows are children of the Desktop window).
You should then be able to use GetWindow to obtain the window handle you are looking for and then SendMessage to set the text into the textbox.
Related
I have whipped up a C# clipboard application that stores multiple 'clippings' for later use. I use low-level keyboard hooks to pop open my application's window(s) on command. When the window is closed (or a clipping is double-clicked), it is supposed to paste the selected clipping into the last active window (the window prior to my application's window). I use low-level WINAPI methods to determine the last active application, snag its handle, and then return focus to it before simulating a Ctrl+V keystroke to paste.
This typically works except in one very unique scenario: I am in a WPF application project, Quick Finding in a XAML file, the cursor automatically switches to the body text, not the Quick Find textbox, and pastes it there. It seems to have something to do with the loss of focus/activation, as it moves the cursor whenever I activate another window, regardless of my own application's running.
VB files, C# files, what have you, and XAML opened in WinForm projects do not steal the Quick Find focus when switching between the VS2013 application and my own; upon returning to the last active application, the text pastes into the Quick Find box.
Only the XAML in WPF application projects gives me this problem.
So far. I know it is a fringe case, but I expect to run into more. This program is meant to be used in a coding environment and it's pretty important that it be able to handle these kinds of scenarios.
I've tried getting the internal control handle using code from http://www.codeproject.com/Articles/34752/Control-in-Focus-in-Other-Processes, so that I can return the focus to it, but it seems that the handle for the body text and the handle for the Quick Find text box are the same.
A partial solution is found in: How do I prevent the original form from losing focus when I show another form? The popup window I use is navigated primarily through my low-level shortcuts, and therefore has no need of explicit activation.
Using the mouse on it or any of my other windows (as I expect my users will sometime), will cause it to gain activation and circumvent this fix. However, it's such a fringe case it doesn't seem to matter. Hopefully this helps anyone in a similar situation (if not necessarily specifically this one).
I'd like to be able to do following actions within another application:
to change Tabs
to Copy text from within a TextBox
to click on a Button
to enter text into a TextBox
to select DropDownList element
Right now I'm using separate methods such as:
mouse_event() to change mouse coordinates and click on a button
another mouse_event(LeftMouseClick followed by the RightMouseClick) to copy a text within a TextBox
Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text) to Copy what's inside the clipboard
SendInput (for each key) - enter the text into a TextBox
Disadvantages of this approach are:
(not crucial) PC becomes unusable (you can't work while script is running)
I have to know exact pixels (read - position/coordinates) of EVERY element within an app
slow execution time (each key has to be typed separately)
I'm looking forward to create an application which can click on a TextBox/Button/List without the need of having exact coordinates of these elements.
Is such task possible with C# WinForms? My current approach works but it has it's flaws.
Any advice?
Read about Mutex. If you plan on scale-ability, read about network communication (for example, the TCP protocol. TcpListner, and TcpClient).
This sounds like a job for UI Automation. I have only used it to get text from another application, but it has functions to activate controls by name or to navigate the control tree if there is no name.
You can get text and interact with controls using AutomationElements that you find using patterns or navigating the control tree.
There is a complete framework for doing exactly this kind of thing, and it's called the UI Automation Framework
Here's some examples on how to use it.
And you can also apply this technique to generic windows's using the UI Spy to determine the automation elements.
I need to create a dialog that allows user to choose between several rather complex actions. I really like the usability of the windows 7 file replace dialog and I think it would suit my needs very well. Here's a screenshot for reference:
Is it possible to use the controls that were used for windows dialog? If not, how would you recommend creating UI similar to this dialog?
Seems like this could easily be done with a window containing a few labels for the text and three custom controls - with some images for the arrows and file icons - each of which changes their background image when the user mouses over them - and fires an event the window picks up on when they are clicked. Fairly standard WinForms stuff.
Is there a particular part of the process you need some extra help with? Like, for example, the mouse over?
How to put a C# programm (ex WPF or WF) under desctop Icons (like a wallpaper)?
I want my Old Good XP Active Desktop made by a some open source C# programm on my Win 7!)
Something like this - Fences .
They have there "windows" appearing underneath icons. I do not want to manage Icons - just put my window under them... BTW any one knows about such operations in other OS’s (Mac etc)?
And I DO NOT WANT TO MANAGE ALL THE ICONS ON MY OWN LIKE bumptop
I think it is quite possible, try this:
Find handle to window with the class "Progman".
Create some window in your application (or, perhaps, in some separate application, you will see the purpose later) and turn off its borders.
Set the parent of the newly created window to desktop handle.
Set the parent of "Progman" window you have found previousely to the window you created.
Desktop will be put on a window owned by your application and it will be possible to manipulate it in the way you want (namely, put something under it).
Also, do not forget to restore desktop's parent when application owning it is closed.
Look for FindWindow, GetClassName and SetParent at http://www.pinvoke.net/
Short answer is you can't really do it (in managed C# anyway). If it's possible, you would need to use Interop, and you'd likely be calling something that Windows doesn't offer as an API.
... although... as Ole Jak mentioned, Stardock looks to be doing it somehow...
The desktop is its own contained item. The same process handles the icons and the wallpaper "behind" those icons.
You are allowed to change the wallpaper to a different image, and you used to be able to create an Active Desktop where HTML content would be displayed, but this was discontinued in Vista.
What are you actually trying to do? Maybe there's another way to achieve a similar result?
There was DreamScene for vista. You could put a video as explorer background. I dont know if you could run a C# program to output the background video.
For example, suppose I'm editing following text (\t is for tabs, it's not text)
'\t\t\tSome text...'
and my input keyboard position is right before 'S' (there is no selected text)
and I want to get number of tabs before 'S' (in this case it's 3)
So how to get that number of tabs using Win32 API?
p.s.
Maybe some function to get whole current line (and then parse that text)?
edit:
So I want my program (that is running in background and does not have a GUI) to determine number of tabs from current active line in any text editor that is currently active (that I'm editing in).
For some applications such as the standard Windows Notepad, it may be possible to do this from another application. Notepad itself is little more than big Win32 "Edit" control inside a resizeable window, so all the regular Win32 Edit control messages can be used to get the actual text and the caret position and so on.
However, in the general case of any Windows text editor, there is no requirement for applications to use a Win32 Edit control for their text editing needs (and in fact, to do so would be just reinventing Notepad). There is no standard Win32 API method to find out where the caret is pointing, because the application is free to draw text on the screen in any way it sees fit (and will store that text internally inside the application with no corresponding API for you to call).
Best Java coding practice is to make your code operating system platform independent as far as possible. If you are trying to implement a utility that is intrinsically Windows specific (e.g. heavily dependent on Win32 APIs), it would be better to use C++ or C# rather than Java.