views:

328

answers:

4

Hello,

I am trying to develop a util (using system-hook) for that works like an expander (user selects some text and presses a hotkey and it is expands). It should work with Visual Studio.

I want to implement this using Windows API because I want to develop an app that works globally with any application (whether you're using VS, or wordpad, you should get the same functionality).

I've been able to do this successfully with notepad, wordpad, etc. using EM_ GETSEL and EM_REPLACESEL messages. But these APIs are not working with Visual Studio, or ms word.

What APIs should I use to be able to 1. Detect what text is selected. 2. Send input to the editor.

I am programming in C#. If you must know what I am trying to do... I am trying to make a universal port of ZenCoding that works on any editor. So all help will be appreciated.

+2  A: 

Why don't you use a System.Windows.Forms.SendKeys class for simulating keyboard input from user?

You can use:

SendKeys.SendWait("^C"); //CTRL+C
var selectedText = Clipboard.GetText();
var newText = Replace(selectedText);
SendKEys.SendWait("^V"); //CTRL+V
TcKs
I had figured on using the clipboard as a last resort, so +1 for getting the idea. But I intend to do it using API calls, for some weird reason, sendkeys doesn't sound right for this.
Cyril Gupta
+1  A: 

For part 2 you could try using Windows Input Simulator which is an open source project I've just released to Codeplex to wrap the Win32 SendInput. Instead of SendKeys which just simulates text input, you can actually simulate real key strokes and complex chords to the active window.

In your case, if the user can perform the task with the Keyboard, this project will help you, otherwise you'd need to find another solution.

Hope this helps.

michael noonan
A: 

Whatever you try, be absolutely sure to try it, ASAP, with Visual Studio 2010 beta 2. The editor has largely been rewritten, and hacks that work with an earlier version should be tested again.

John Saunders
+2  A: 

You can use WPF's Automation functionality, encapsulated in these two namespaces:

System.Windows.Automation
System.Windows.Automation.Provider

As an example, this is a method for finding an automation target element (e.g. a typical win control):

    public static AutomationElement FindElement(AutomationElement context, PropertyCondition[] conditions)
    {
        // if no conditions, there's no search to do: just return the context, will be used as target
        if (conditions == null)
        {
            return (context);
        }

        // create the condition to find
        System.Windows.Automation.Condition condition = null;
        if (conditions.Length <= 0)
        {
            throw (new ArgumentException("No conditions specified"));
        }
        else if (conditions.Length == 1)
        {
            condition = conditions[0];
        }
        else
        {
            AndCondition ac = new AndCondition(conditions);
            condition = ac;
        }

        // find the element
        CacheRequest creq = new CacheRequest();
        creq.TreeFilter = Automation.ControlViewCondition;
        using (creq.Activate())
        {
            AutomationElement e = AutomationContext(context);
            AutomationElement target = e.FindFirst(TreeScope.Subtree, condition);
            return (target);
        }
    }
Ariel