开发者

Visual Studio 2010 extension get selected text

开发者 https://www.devze.com 2023-01-05 16:47 出处:网络
I\'m writing my own extension. I\'ve created a toolbar button. The template I used was \"Visual Studio Package\" and there was an option during the wizard to create a co开发者_如何学Cmmand button (or

I'm writing my own extension. I've created a toolbar button. The template I used was "Visual Studio Package" and there was an option during the wizard to create a co开发者_如何学Cmmand button (or something like that)

So the button shows up, but I can't work out how to get the current document or the selected text within the document.

Any help would be appreciated.


There are two ways to approach it:

  1. Handle the button globally and use DTE to get the current document (DTE.ActiveDocument) and selected text (((TextDocument)activeDoc).Selection.Text). You can get the top-level DTE object in your package by writing: DTE dte = GetService(typeof(SDTE)) as DTE; Note that the active document may be null.
  2. Create a command handler at the editor level to handle the given command. The Align Assignments extension I wrote (source) is an example of this. Here's what the command filter looks like.
0

精彩评论

暂无评论...
验证码 换一张
取 消