How to get text indent (tabs) from some text input? - c#

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.

Related

How to get which button is which in a Yes/No/Cancel dialog using UI Automation?

In the proprietary third-party application my program has to communicate with, there is a situation where an operation I have to invoke displays a system dialog with Yes/No/Cancel buttons. I now need to push the "Yes" button. There is no vendor-side function to tell the application to do the operation that starts when I push "Yes", so I have to use UI automation.
Where I am stuck with is the localization of the Windows standard dialogs. The caption of the "Yes" button is different depending on the language Windows is used in, and the order might also be different in languages which are written from right to left.
Since we are currently only using German and English versions of Windows where this program is used, I currently just click the first element in the subtree of the window that supports the InvokePattern which is always "Yes" in these language versions, and the program is used only in-house. But I don't like this, I want a robust way to determine which button is which. Is this possible with UI automation?
Best practice is to use the AutomationID property to uniquely identify a button. Don't use the Runtime ID, as that's not guaranteed to be unique across invocations.
Well if you are only targeting English and German, you can just look for a control with a caption/text property of "Yes" (for English). In German environments, look for the German equivalent of yes which you should know at coding-time.
This is best practice I used in privious project for UI automation in localized software:
Use AutomationId(UIA)/ControlId(MSAA) if possible.
Locate corresponding resource file and load string from it. Then search it in UI. Hard code localized string in test code is bad idea. You can't grantee that
text is translated correctly even for Windows
text is already translated in specific language when you expand your scope.
text is translated consistently everywhere.
Simulate keyboard {tab} and {space} to click a button.
Use control sequence as you're currently doing, but nobody can grantee that button with index 0 is in the leftmost.
Use absolute pixel/position. Sometimes button are drawn as picture.

VS 2013 Quick Find loses focus when switching to my application - how do I preserve/restore it programmatically?

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).

How to paste text into a different application with C#

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.

Contextual Text Editor in WPF

We are currently looking to create a text-editor in WPF (.NET 4.0) which will allow writers within our team to create movie scripts. In short, the functionality should ressemble that of FinalDraft or Adobe Story (i.e.: contextual positioning of text depending on the cursor's position and user intentions)
We are currently looking at two different solutions design-wise:
One WPF control which will act as the container, and multiple small text-editing controls which will represent rows within the script. This will allow us to position the controls using their margin, while also making binding easy. The challenge here would be the handling of multi-line selections. I was thinking of using a Listbox as the container, and each listbox item would be a custom control containing a textbox. This would require the instantiation of controls depending on the user's action. Everything would be skinned to give the impression that the user is working on a blank page.
One big textbox capable of displaying custom XML data. The challenge here would be to determine where exactly the cursor is located (i.e.: is the cursor on top of an actor's name, etc.) and positioning the text appropriately (i.e.: actor names are centered and in caps, etc.)
I recently tried implementing the first solution, but having to re-implement the whole selection behavior that is built-in in basic text boxes is non-trivial and requires a lot of work. As for the second solution, binding to my business objects will be much harder than simply instantiating multiple controls with different bindings.
Do you have any other solution in mind ?
I needed a text editor for a application once. We had a big xml file for settings and the user should be able edit those.
Turns out , if your file is large enough (+ 10000 lines) the rich text box is getting pritty slow.
As for building a gui mask : only if your user wirtes some short options like text. But is i understand you want your useres to write creativ text. This "mask" gui - "lot of small places" will make them feel like they are in the 80ties.
I suggest: Dont write the Programm , only write a Plugin to an exitings editor. Some are free like:
http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor
or an add in for word - people love Word and know Word
http://www.codeproject.com/Articles/8837/Writing-a-Word-Add-in-Part-I
And for binding data and the like: Once the text is written, the user just need to press the save button and you can parse the input for information. I would not do it on the fly as it can get pretty slow. Also you say that the information is linked so only if all the data is written you can make use of it.

accessing another app's elements using C# WinForms app

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.

Categories

Resources