2009-12-05 89 views
1

我想開發一個實用程序,將輸入發送到Visual Studio(使用系統掛機)的工作方式類似於膨脹機(用戶選擇一些文本,並按下熱鍵,它是擴展)。它應該與Visual Studio一起工作。我如何使用Windows API

我想是因爲我希望開發與任何應用程序在全球工作的應用程序(無論你使用VS,或寫字板,你應該得到相同的功能)來實現這個使用Windows API。

我已經能夠用記事本,寫字板等使用EM_ GETSEL和EM_REPLACESEL消息成功地做到這一點。但是這些API不適用於Visual Studio或ms字。

我應該使用哪些API才能夠 1.檢測選擇了哪些文本。 2.發送輸入到編輯器。

我使用C#編程。如果你必須知道我正在嘗試做什麼...我正在努力使ZenCoding的一個通用端口適用於任何編輯器。所以所有的幫助將不勝感激。

回答

2

你爲什麼不使用來自用戶的模擬鍵盤輸入System.Windows.Forms.SendKeys類?

您可以使用:

SendKeys.SendWait("^C"); //CTRL+C 
var selectedText = Clipboard.GetText(); 
var newText = Replace(selectedText); 
SendKEys.SendWait("^V"); //CTRL+V 
+0

我已經想通過使用剪貼板作爲最後的手段,所以+1來獲得這個想法。但是我打算使用API​​調用來實現,出於某種奇怪的原因,sendkeys聽起來並不合適。 – 2009-12-06 03:42:13

3

對於部分2,你可以嘗試使用Windows Input Simulator這是一個開源項目,我剛剛發佈到Codeplex上包裹的Win32 SendInput。除了模擬文本輸入的SendKeys之外,您實際上可以模擬真實的擊鍵和複雜的和絃到活動窗口。

在你的情況,如果用戶可以使用鍵盤執行任務,這個項目將幫助你,否則你需要找到另一種解決方案。

希望這會有所幫助。

0

不管你嘗試,務必嘗試一下,儘快,與Visual Studio 2010測試版2的編輯器已經在很大程度上被改寫,並與前一個版本的破解工作,應再次進行測試。

2

您可以使用WPF的自動化功能,封裝在這兩個命名空間:

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

作爲一個例子,這是找到一個自動化目標元素的方法(例如,一個典型的雙贏控制) :

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); 
     } 
    }