Developing Visual Studio .NET Macros and Add-Ins phần 6 ppsx

41 428 0
Developing Visual Studio .NET Macros and Add-Ins phần 6 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

} base.Dispose( disposing ); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager( typeof(LocalizedForm)); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.timer1 = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize) (this.timer1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.AccessibleDescription = ((string)(resources. GetObject(“label1.AccessibleDescription”))); this.label1.AccessibleName = ((string)(resources. GetObject(“label1.AccessibleName”))); this.label1.Anchor = ((System.Windows.Forms.AnchorStyles) (resources.GetObject(“label1.Anchor”))); this.label1.AutoSize = ((bool)(resources. GetObject(“label1.AutoSize”))); this.label1.Dock = ((System.Windows.Forms.DockStyle) (resources.GetObject(“label1.Dock”))); this.label1.Enabled = ((bool)(resources. GetObject(“label1.Enabled”))); this.label1.Font = ((System.Drawing.Font)(resources. GetObject(“label1.Font”))); this.label1.Image = ((System.Drawing.Image)(resources. GetObject(“label1.Image”))); this.label1.ImageAlign = ((System.Drawing.ContentAlignment) (resources.GetObject(“label1.ImageAlign”))); this.label1.ImageIndex = ((int)(resources.GetObject (“label1.ImageIndex”))); this.label1.ImeMode = ((System.Windows.Forms.ImeMode) (resources.GetObject(“label1.ImeMode”))); this.label1.Location = ((System.Drawing.Point) (resources.GetObject(“label1.Location”))); this.label1.Name = “label1”; this.label1.RightToLeft = 180 Chapter 8 ((System.Windows.Forms.RightToLeft) (resources.GetObject(“label1.RightToLeft”))); this.label1.Size = ((System.Drawing.Size)(resources. GetObject(“label1.Size”))); this.label1.TabIndex = ((int)(resources. GetObject(“label1.TabIndex”))); this.label1.Text = resources.GetString(“label1.Text”); this.label1.TextAlign = ((System.Drawing.ContentAlignment) (resources.GetObject(“label1.TextAlign”))); this.label1.Visible = ((bool)(resources. GetObject(“label1.Visible”))); // // label2 // this.label2.AccessibleDescription = ((string)(resources. GetObject(“label2.AccessibleDescription”))); this.label2.AccessibleName = ((string)(resources. GetObject(“label2.AccessibleName”))); this.label2.Anchor = ((System.Windows.Forms.AnchorStyles) (resources.GetObject(“label2.Anchor”))); this.label2.AutoSize = ((bool)(resources. GetObject(“label2.AutoSize”))); this.label2.Dock = ((System.Windows.Forms.DockStyle) (resources.GetObject(“label2.Dock”))); this.label2.Enabled = ((bool)(resources. GetObject(“label2.Enabled”))); this.label2.Font = ((System.Drawing.Font)(resources. GetObject(“label2.Font”))); this.label2.Image = ((System.Drawing.Image) (resources.GetObject(“label2.Image”))); this.label2.ImageAlign = ((System.Drawing.ContentAlignment) (resources.GetObject(“label2.ImageAlign”))); this.label2.ImageIndex = ((int)(resources.GetObject (“label2.ImageIndex”))); this.label2.ImeMode = ((System.Windows.Forms.ImeMode) (resources.GetObject(“label2.ImeMode”))); this.label2.Location = ((System.Drawing.Point)(resources. GetObject(“label2.Location”))); this.label2.Name = “label2”; this.label2.RightToLeft = ((System.Windows.Forms.RightToLeft) (resources.GetObject(“label2.RightToLeft”))); this.label2.Size = ((System.Drawing.Size)(resources. GetObject(“label2.Size”))); this.label2.TabIndex = ((int)(resources. GetObject(“label2.TabIndex”))); this.label2.Text = resources.GetString(“label2.Text”); this.label2.TextAlign = ((System.Drawing.ContentAlignment) (resources.GetObject(“label2.TextAlign”))); this.label2.Visible = ((bool)(resources. Life Cycles, Debugging, and Satellite DLLs 181 GetObject(“label2.Visible”))); // // label3 // this.label3.AccessibleDescription = ((string)(resources. GetObject(“label3.AccessibleDescription”))); this.label3.AccessibleName = ((string)(resources. GetObject(“label3.AccessibleName”))); this.label3.Anchor = ((System.Windows.Forms.AnchorStyles) (resources.GetObject(“label3.Anchor”))); this.label3.AutoSize = ((bool)(resources .GetObject(“label3.AutoSize”))); this.label3.Dock = ((System.Windows.Forms.DockStyle) (resources.GetObject(“label3.Dock”))); this.label3.Enabled = ((bool)(resources. GetObject(“label3.Enabled”))); this.label3.Font = ((System.Drawing.Font)(resources. GetObject(“label3.Font”))); this.label3.Image = ((System.Drawing.Image) (resources.GetObject(“label3.Image”))); this.label3.ImageAlign = ((System.Drawing.ContentAlignment) (resources.GetObject(“label3.ImageAlign”))); this.label3.ImageIndex = ((int)(resources. GetObject(“label3.ImageIndex”))); this.label3.ImeMode = ((System.Windows.Forms.ImeMode) (resources.GetObject(“label3.ImeMode”))); this.label3.Location = ((System.Drawing.Point) (resources.GetObject(“label3.Location”))); this.label3.Name = “label3”; this.label3.RightToLeft = ((System.Windows.Forms.RightToLeft) (resources.GetObject(“label3.RightToLeft”))); this.label3.Size = ((System.Drawing.Size)(resources. GetObject(“label3.Size”))); this.label3.TabIndex = ((int)(resources. GetObject(“label3.TabIndex”))); this.label3.Text = resources.GetString(“label3.Text”); this.label3.TextAlign = ((System.Drawing. ContentAlignment)(resources. GetObject(“label3.TextAlign”))); this.label3.Visible = ((bool)(resources. GetObject(“label3.Visible”))); // // timer1 // this.timer1.Enabled = true; this.timer1.SynchronizingObject = this; this.timer1.Elapsed += new System.Timers. ElapsedEventHandler(this.timer1_Elapsed); // 182 Chapter 8 // LocalizedForm // this.AccessibleDescription = ((string)(resources. GetObject(“$this.AccessibleDescription”))); this.AccessibleName = ((string)(resources.GetObject (“$this.AccessibleName”))); this.Anchor = ((System.Windows.Forms.AnchorStyles) (resources.GetObject(“$this.Anchor”))); this.AutoScroll = ((bool)(resources. GetObject(“$this.AutoScroll”))); this.AutoScrollMargin = ((System.Drawing.Size) (resources.GetObject(“$this.AutoScrollMargin”))); this.AutoScrollMinSize = ((System.Drawing.Size) (resources.GetObject(“$this.AutoScrollMinSize”))); this.BackgroundImage = ((System.Drawing.Image) (resources.GetObject(“$this.BackgroundImage”))); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label3, this.label2, this.label1}); this.Dock = ((System.Windows.Forms.DockStyle)(resources. GetObject(“$this.Dock”))); this.Enabled = ((bool)(resources.GetObject(“$this.Enabled”))); this.Font = ((System.Drawing.Font)(resources. GetObject(“$this.Font”))); this.ImeMode = ((System.Windows.Forms.ImeMode) (resources.GetObject(“$this.ImeMode”))); this.Location = ((System.Drawing.Point)(resources. GetObject(“$this.Location”))); this.Name = “LocalizedForm”; this.RightToLeft = ((System.Windows.Forms.RightToLeft) (resources.GetObject(“$this.RightToLeft”))); this.Size = ((System.Drawing.Size)(resources. GetObject(“$this.Size”))); this.TabIndex = ((int)(resources. GetObject(“$this.TabIndex”))); this.Visible = ((bool)(resources. GetObject(“$this.Visible”))); ((System.ComponentModel.ISupportInitialize) (this.timer1)).EndInit(); this.ResumeLayout(false); } #endregion private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { System.DateTime dt = System.DateTime.Now; Life Cycles, Debugging, and Satellite DLLs 183 label2.Text = dt.ToLongDateString(); label3.Text = dt.ToLongTimeString(); } } } When you run this add-in, you will see the form for it. By default the form will be in English. If you switch your computer’s culture to either French (France) or Spanish (Mexico), and then restart the IDE, your add-in will be in the appropriate language. Notice also that the date and time will use the format standard of the particular culture. Moving Forward In this chapter I discussed three important topics: the life cycle of an add-in, how to debug an add-in, and how to use satellite DLLs to allow your software to present infor- mation in a local language. Globalization is something that many programmers tend to neglect (especially in the United States), but if you add globalization features to your programs, your software will be much more well received by people in other countries. In the next chapter, I explain how you can manipulate solutions and project pro- grammatically from either an add-in or a macro. Stay tuned! 184 Chapter 8 TEAMFLY Team-Fly ® 185 In Chapter 5, “Just Enough .NET Architecture,” I talked briefly about project objects and how you can access project information using different objects, depending on the language the project is written in. I expand on that discussion here, by explaining how you can modify the project information. If you have not read Chapter 5, I encourage you to read the section titled “The Visual Studio Project Types” there before continuing with this chapter, because I assume here that you understand how to access generic project information through the Project object, and that this Project object also contains an Object property. The Object property gives you access to a COM object that contains language-specific information. Why would you want to modify project information? Suppose you are building a project in which you call several external tools during the build process. To access these external tools, you set various Custom Build steps in the properties for the particular files in your project. You could set the Custom Build step for each file separately or you could write a macro that sets the information for you. This, in fact, is exactly what I did in Chapter 8 in the macro that adds resource files to a project. As another example, you might be developing a class library contained in an assem- bly for commercial use by other programmers. In order to make things as easy on your clients as possible (and to minimize support calls!) you might include with your library a macro or add-in that automatically adds the assembly to a project in the form of a Manipulating Solutions and Projects CHAPTER 9 reference, carefully setting the project properties. Then the user can begin program- ming with your class library without having to spend time messing with the project settings. You could include such a macro with your assembly. Determining the Currently Selected Project The following macro code obtains the currently selected project in the Solution Explorer. When you have a macro that manipulates a project, using this code, you can find out which project name the IDE user has clicked in the Solution Explorer. The IDE user can also click on one of the items inside a project, and this code will obtain the project containing the selected item. Sub FindProject() Dim projs As System.Array Dim proj As Project projs = DTE.ActiveSolutionProjects() If projs.Length > 0 Then proj = projs.GetValue(0) MsgBox(proj.Name) End If End Sub This macro first obtains the root DTE object, and from there a list of the active pro- jects through the ActiveSolutionProjects property. (Note: The reason that “pro- jects” is plural here is because the IDE user can click on multiple items in the Solution Explorer by clicking one item and then holding down the Ctrl key and clicking another item.) The preceding macro obtains only the first selected project in such cases. If you want your macro to operate on all the selected projects, you can use this macro instead: Sub FindProjects() Dim projs As System.Array Dim proj As Project projs = DTE.ActiveSolutionProjects() If projs.Length > 0 Then For Each proj In projs MsgBox(proj.Name) Next End If End Sub You can use similar code in an add-in as well. Remember that in add-ins, you do not access the root DTE object through the DTE variable name; instead, you grab the DTE object from the first parameter in the OnConnection method. Here’s the OnConnec- tion method for a VB.NET add-in that registers a command that will get the selected projects. (If you want to try this out, make sure you check the Add-in wizard option to create a Tool menu item; that will give you a class that’s derived from IDTCommand- Target so you can implement commands.) 186 Chapter 9 Public Sub OnConnection(ByVal application As Object, _ ByVal connectMode As Extensibility.ext_ConnectMode, _ ByVal addInInst As Object, ByRef custom As System.Array) _ Implements Extensibility.IDTExtensibility2.OnConnection applicationObject = Ctype(application, EnvDTE.DTE) addInInstance = Ctype(addInInst, EnvDTE.AddIn) Dim objAddIn As AddIn = Ctype(addInInst, AddIn) Dim CommandObj As Command Try CommandObj = applicationObject.Commands.AddNamedCommand( _ objAddIn, “GetProjects”, “AddinProjectManip2”, _ “Gets the selected project names “, True, 59, Nothing, _ 1 + 2) Catch e As System.Exception End Try End Sub Notice that I’m adding a command called GetProjects. Here’s the QueryStatus method that enables the command: Public Sub QueryStatus(ByVal cmdName As String, ByVal neededText _ As vsCommandStatusTextWanted, ByRef statusOption As vsCommandStatus, _ ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus If neededText = EnvDTE.vsCommandStatusTextWanted. _ vsCommandStatusTextWantedNone Then If cmdName = “AddinProjectManip2.Connect.GetProjects” Then statusOption = Ctype(vsCommandStatus.vsCommandStatusEnabled _ + vsCommandStatus.vsCommandStatusSupported, vsCommandStatus) Else statusOption = vsCommandStatus.vsCommandStatusUnsupported End If End If End Sub And, finally, here’s the code that executes the command. You can see that I just pasted in the macro code (that’s why I chose Visual Basic for this add-in) and then replaced DTE with applicationObject. Public Sub Exec(ByVal cmdName As String, ByVal executeOption As _ vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, _ ByRef handled As Boolean) Implements IDTCommandTarget.Exec handled = False If (executeOption = vsCommandExecOption. _ vsCommandExecOptionDoDefault) Then If cmdName = “AddinProjectManip2.Connect.GetProjects” Then Dim projs As System.Array Dim proj As Project projs = applicationObject.ActiveSolutionProjects() Manipulating Solutions and Projects 187 If projs.Length > 0 Then For Each proj In projs MsgBox(proj.Name) Next End If handled = True Exit Sub End If End If End Sub To try out this add-in, build its project, start a new instance of Visual Studio .NET, and open a solution (any solution will do). In the Solution Explorer, select a couple pro- jects by clicking one, then while holding down the Ctrl key, clicking another. Next, open the Add-in Manager and check the box next to the add-in. Then choose View➪Other Windows➪Command Window, to open a new command window, and type the following command into the command window: AddinProjectManip2.Connect.GetProjects You will see a series of message boxes open, one for each project you selected, with each message box showing the name of a project. Manipulating a Project’s Items By itself, the macros and add-ins in the preceding section aren’t particularly useful in that they only display information about a project, rather than manipulate the projects. But you can easily add code to manipulate a Project object. Here are some of the project- related objects that you might manipulate from a macro. Project.ProjectItems. Your macro could check whether a file is already part of a project, by checking for the file’s existence in the ProjectItems collection. If the file doesn’t exist, your macro could add it. For example, if you have a class library in the form of source code, your macro could automatically add the source code to the project. You can also use the ProjectItems property to obtain information on the individual items in the project, such as the source code files or the resource files. Each such item is a ProjectItem object. Think of the ProjectItems object as corresponding to the items underneath the project name in the Solution Explorer. Remember, the project has a ProjectItem object not just for files, but for folders as well. If, for example, you have a C++ project with folders called Source Files, Header Files, and Resource Files, you will have a separate ProjectItem object for the three folders as well as for each file. However, in the case of C# and VB.NET projects, you will not have a ProjectItem object for the References folder, nor its members. 188 Chapter 9 ProjectItem.IsOpen. Your macro can check this property to determine if the user currently has the file open in the IDE. For example, if the ProjectItem object corresponds to a C++ source file, then IsOpen will be true if the C++ source file is currently open in the IDE editor. If your macro or add-in is making consider- able changes to a project, you might check IsOpen for each item in the project. If any such items are open, you might display a message to the IDE user stating that the documents must be closed before proceeding. (Note: The IsOpen method works only for items in VB.NET and C# projects.) The following code is an example of a macro that uses IsOpen. This macro goes through the list of ProjectItem objects and determines which are opened, finally displaying a message box showing the list of open project items. Sub ListOpenItems() Dim projs As System.Array Dim proj As Project Dim pitem As ProjectItem projs = DTE.ActiveSolutionProjects() If projs.Length > 0 Then proj = projs.GetValue(0) Dim str As String = “” For Each pitem In proj.ProjectItems If pitem.IsOpen Then str &= pitem.Name + Chr(13) + Chr(10) End If Next MsgBox(str) End If End Sub ProjectItem.Saved. Your macro can check if a project item has been changed since the last save. For example, if the IDE user has a C++ source file open in the IDE editor, and the user edits the source code but does not save the file, then the Saved property for the corresponding project item will be false. ProjectItem.Open. Your macro can open a project item automatically. If the proj- ect item is a source file, the file will open in the editor. Your macro can then make changes to the source file. Note, however, that there’s a trick to making the Open function work: The Open function returns an object of class Window, and initially this Window object’s Visible property is set to False. You need to change the Visible property to True. The following code demonstrates this: Sub OpenAllSourceFiles() Dim projs As System.Array Dim proj As Project Dim pitem As ProjectItem projs = DTE.ActiveSolutionProjects() If projs.Length > 0 Then proj = projs.GetValue(0) Dim ext As String = “” For Each pitem In proj.ProjectItems ext = System.IO.Path.GetExtension(pitem.Name) Manipulating Solutions and Projects 189 [...]... to both add-ins and macros In the next chapter I explain how to manage the Document objects and the various objects that deal with the user interface These techniques, too, apply to both add-ins and macros CHAPTER 10 Programming the Document and User Interface Objects I begin this chapter by showing you how you can use the Document and related objects to create documents and modify, close, and save... Visual Studio NET The reason is that the command line, when it runs devenv.exe (which is the executable file for Visual Studio NET) needs a full path to the command being run Thus, instead of simply putting devenv.exe for the command line, I extract the installation path from the Registry, which is also the directory of the devenv.exe program Then I use this information to construct the full path and. .. you test an add-in either by running Visual Studio NET in command-line mode or in standard GUI mode Recall that this trick involved adding a new configuration specifically for running and debugging in command-line mode This new configuration set various properties for launching the external program These properties were Start External Program, Command-Line Arguments, and Working Directory Using the language-specific... the code for including and excluding is almost the same, I broke it out into its own private function called IncExcVBSet, then I simply called that function from each of the two macros, IncludeVBSet and ExcludeVBSet (Notice in the code that you set the BuildAction property value to 1 to include the file, and 0 to exclude it.) To try out these macros, add these three files to a VB.NET project, called... your solution Using the Solution object, you can also add and remove projects To add a project, you start with a template (The templates are sprinkled throughout the Visual Studio NET installation directory.) Here’s an example of a macro that creates a new Visual Basic Windows Application and adds it to the current solution: Manipulating Solutions and Projects Sub CreateVBApp() Dim apath As String apath... global cache, the IDE is able to easily find the assembly in its latest and greatest version Moving Forward In this chapter I expanded on the project and solution information that I introduced in “The Macro and Add-in Models” section of Chapter 5 I also showed you how to: ■ ■ Determine the currently active project ■ ■ Add, remove, and modify the items in a project ■ ■ Modify a project’s settings ■ ■... purpose, you could also write a macro to exclude the files and another macro to include the files All that said, be aware of this confusing element in Visual Studio NET regarding the configuration of individual files: You can find a Properties member for both a ProjectItem and a ProjectItem object’s Configuration objects Which do you use, and when? It depends on the language Take a look at this macro,... projects and project items in a hierarchical manner I show you how you can move about this hierarchy, accessing and modifying the information Managing Documents with the Document Classes The IDE uses different editors for different types of documents, such as a code editor for source files, a visual editor for forms, a data editor for resx resource files, and so on When an editor is open and contains... the filename or the full path and filename The Form Editor and Documents If you have a form open and you run the earlier ListDocuments macro, you will see two documents for the single form One is the form editor, whose name will have a resx extension; the other is the document for the code, and will have either a vb or cs extension, depending on whether the form is a Visual Basic or C# form You can... with a visual form, you’re going to see something a little strange Take a look at this macro: Sub ShowActiveDocument() VBMacroUtilities.Setup(DTE) VBMacroUtilities.Clear() VBMacroUtilities.Print(DTE.ActiveDocument.Name) End Sub If you open up both a form editor and a code editor for a single form (such as the default Form1.vb in a VB.NET Windows application) and switch first to the code editor and run . Ctype(vsCommandStatus.vsCommandStatusEnabled _ + vsCommandStatus.vsCommandStatusSupported, vsCommandStatus) Else statusOption = vsCommandStatus.vsCommandStatusUnsupported End If End If End Sub And, . installa- tion path for Visual Studio .NET. The reason is that the command line, when it runs devenv.exe (which is the executable file for Visual Studio .NET) needs a full path to the command being run the Add-in Manager and check the box next to the add-in. Then choose View➪Other Windows➪Command Window, to open a new command window, and type the following command into the command window: AddinProjectManip2.Connect.GetProjects You

Ngày đăng: 12/08/2014, 16:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan