Getting selected item in active window - c#

I am using C# to develop an application that works with the clipboard, an was wondering if its possible to get the contents of what the user has selected in the current window, no matter what window it is (e.g. could be FireFox, notepad, etc.).

I'm not sure if you're going to be able to get a general solution for this problem. Take a look at this link;
http://www.eggheadcafe.com/software/aspnet/33899121/get-selected-text-of-active-window-.aspx
The author of this post reports that their solution works in Notepad, but not in Wordpad or Word. I don't know much about this area, but I would guess that this means there are various means through which text selection is implemented - even within Windows.
I think this also extends to Control-C, not just text selection. I believe that there are different ways of handling the copy command depending on the context in which it's used.
For example, if you press Control-C on a dialog box in Windows, it will copy the text without you have to select it manually.
Hopefully the link above is of some use to you. This seems like an interesting problem to overcome.
Edit:
Spoke too soon - just found this link;
http://social.msdn.microsoft.com/Forums/en/winformsapplications/thread/1dc356e6-9441-44de-9eda-247003fa6ef5
Looks like this is exactly what you're after - hope that works.

If something was only selected, it won't appear in the Clipboard. But if user copied some object that he had previously selected, then, yes, it will be in the Clipboard.

Related

How to list all the elements of an application?

I'm trying to click a button on a windows application from other application. Basically, I want to click app B's button from app A's code.
I can use winapi findWindow to get a handler. The problem is that I have no idea the name of the button on the app B. Is this possible to list all the names or ids of an application?
I'm using c# to make it happen.
Since you're looking at suggestions (it's a pretty generic question really, it might or might not work depending on what other app/window is, is it e.g. browser or a 3rd party app etc., does it support automation)
Take a look at this closely related answer (it might be a duplicate but you're kind of 'looking for' still so maybe note).
Accessing Elements from Other Processes
Also this one on how to 'access' other app's 'inputs'
Pinvoke SetFocus to a particular control
I have not tested this. But it looks like a intressting libary. Maybe there is some function you can use. It is called White. This is just a sample:
Application application = Application.Launch("foo.exe");
Window window = application.GetWindow("bar", InitializeOption.NoCache);
Button button = window.Get<Button>("save");
button.Click();
You can use tool such as Spy++ (included in any Visual Studio except Express editions) to find name and class of that button and then use these information as parameters of FindWindow().

c# what wpf control to use for showing text file

Whats the best control to use in wpf applicaion for loading big text files into? And its not just simple as that, i need to be able to hightlight/change background/color of some lines. I was thinking of using RichTextBox.. but though i just come and ask what other people think.
I dont need to edit the text, just highlight lines with errors
EDIT: Ah i forgot to mention, text file content is processed by the program what will change the background color of some lines. Would be nice to give user a change to do that allso when the program failes to find some error lines.
EDIT2: The application is for parsing log files, so the files might be big, over 50 000 lines..
I didn't understand if you only want to display files or allow user to edit them.
Either way, maybe ScintillaNET would be useful to you.
Well known text editor, Notepad++ uses Scintilla :)
You could grab notepad++ sources at http://sourceforge.net/projects/notepad-plus/, but It's written in c++, so the API would be a bit different than in .NET.
EDIT
Notepad++ is capable of displaying files of around 100-200MB. If you need better than that, you would need to implement some kind of paging technology/virtual mode (do not load entire file, only the portion that would be displayed on the screen depending on current scrollbar position)
If Scintilla seems an overkill, then maybe you'd be better off with simply building a DataGrid. If log entries consist of several fields, these could become columns and would be even easier to read. If you want to go that route, first parse your log file into a List of LogEntry, then use databinding to bind to it. You could read your file incrementally as new lines get added for better user experience.
I think RichTextBox will be most appropriate in this case but as CharithJ said you cannot load whole log in to it at one time it will be too much performance hit.
What I would do in scenario like this is .. I will have a Richtextbox along with two small up and down buttons for kind of scroll. I will only load some text of log at one time let's say x number of characters. Once user clicks the up or down scroll button, I would remove some text from start and end depending on which button user have clicked, up or down and based on that I would remove some text from richbox and append some other.
Either you can do something like I suggested above, or one other way could be to extend the RichTextBox control and implement logic that only forwards a limited text to RichTextBox for display at a time. You can handle scroll related events to make changes in RichTextBox's content.

Right click, text capture

I have a c# application that runs in the bottom right corner of the page, i was wondering how i could go about making my application appear when the user selects some text anywhere (say a pdf, browser etc) and then right clicks, goes to my custome selection bit say and it pastes that text in to my running application.
If anyone made any sense of that could you advise me in to what i need to be researching to get it done.
Thanks
I've got this dictionary which does something similar.
Here's its mode of operation (or at least what I think it does)
When there is a double click, it sends a key command (probably Ctrl+C) to copy the selected text. It then reads the text from the clipboard.
For your purpose, you could implement the mouse listening and sending keystrokes with this article I found on CodeProject: InputManager library.
You then retrieve the copied text from the clipboard and do whatever you want with it.
Have a look at a Clipboard monitor
You can hook into the windows messaging api, probably there is a Text-selected event.

Non intrusive 'live' help system

I'm searching a C# component or code snipped that does something like that:
I want to inform new users about the most important program functions if he opens a new window for example.
It should be a box showing text (formated if possible) that is of course not modal and has some mechanism to 'go out of the way' if the user enters the textbox area. So that he can access what's underneath it. Alternativly the window could also stick to the border of the window, but there needs to be a way that this also works if the window is maximized.
So I want to present him with a short introduction of what he can do in every corner of my app most painlessly.
Thank you!
I use a "bar" at the top of every window to display some information about the current window/dialog.
Use tooltips. They can be programmatically controlled, and you can have them appear at will. You'll need to add the functionality to your app to keep track of what tooltips have been shown to the user already.
You can add a "balloon" style by setting the IsBalloon property to true.
You can also replace them with smaller descriptions for when the user wants to hover over the control and have them displayed again.
I'm already using tooltips heavily. However, they aren't very practical when displaying bigger amounts of data and they are bound to specific user actions.
Have you considered having a contextual menu for each form / page which contains links to Adobe Captivate style presentations for each available task? That way the user can investigate an example of how to achieve a task relating to what they are trying to achieve from within the application / site.
This approach would require a good deal of maintenance and management if your code changes regularly but coordinating it with a training department can provide rich help features in your application.
See http://www.adobe.com/products/captivate/ for more information.

Replacement for yes/no/cancel MessageBox (C#)

I am looking for a decent replacement for the standard windows YES/NO or YES/NO/CANCEL MessageBox.
I have often seen these standard dialogs misused in ways such as: "To save in plain text answer YES, or to save in html answer NO". Obviously, the text should read "Save As: and the "buttons should be labeled "Text" and "HTML". It is not a yes/no question that is being asked, and although it could be phrased that way, it would not be easy to read and understand.
Microsoft gives no way to change the text on the buttons. There is no fast/simple way to build a replacement from scratch... as evidenced by the number of applications using the awkward style mentioned above.
Is there any free C# replacement dialog or MessageBox out there that lets you at least:
- specify the number of buttons
- specify the text to appear on each button
- specify the default button
I have looked and have been unable to find one.
(I would build one myself, but I am not familiar enough with all the behaviors that a fully functional control should have, since I only need/use/know a small subset. Two examples I don't use: themes and internationalization. I need something that my coworkers will also want to use.)
Check out Dissecting the MessageBox on CodeProject. The project is a bit dated, but it's pretty much exactly what you're looking for and it shouldn't take much to update it.
Depending on your target platform, a task dialog may be a good way of doing this. There is a .NET wrapper for task dialogs in the Windows API Code Pack. However these are provided only in Windows Vista and above, not in XP or 2003.
Frankly, it is not that difficult to create such a Messagebox yourself, we have such a thing working in the current app we are developing.
What you need is a FlowLayout for the buttons that will auto-align any buttons you create. Our API then has something like (params Tuple<string,DialogResult>[] buttons)
Tuple is a helper class that contains two values. The string is the Text of the button, the Dialogresult is the one our messagebox returns when the button with said text is clicked.
I agree with Frank. It wouldn't be too difficult to create your own generic form that handles this for you. Without getting into code, the form should do the following
1) Have a property to set the message you want to show to the user.
2) Have a method for adding buttons, with 2 arguments, one for the button text, and one for the dialog result
3) When the form is displayed, it should be in modal dialog mode so that the rest of the application is inactive while until one of the options is clicked.
So, to create a Save As/Don't Save/Cancel, you would add 3 buttons in step 2, all with the appropriate button text and dialog result.
Using Flow layout, you should be able to get it to display properly regardless of the size of the message, or the number of buttons.

Categories