Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Professional Visual Studio 2005 (2006) [eng]

.pdf
Скачиваний:
132
Добавлен:
16.08.2013
Размер:
21.9 Mб
Скачать

Chapter 24

Each resource added to the project has a unique name assigned to it (normally the filename for audio and graphic files that are inserted into the resource file), which you can refer to in code. These names are rendered to object properties exposed by the My.Resources object. For example, if you have an image resource called MainFormBackground, then the shortcut way of accessing this resource is My.Resources

.MainFormBackground.

Use the following sample task to verify how My.Resources works:

1.Start Visual Studio 2005 and create a new Visual Basic WindowsApplication. Double-click the My Project node in the Solution Explorer and navigate to the Resources tab.

2.Click the Add Resource drop-down and choose New Image BMP Image. When prompted, name the new image MainFormBackground and click OK to add it to the project’s resource file.

3.Visual Studio 2005 automatically opens the image editor view for the new image. A standard Windows form is 300 × 300 pixels, so set the image’s Width and Height properties to 300.

4.Draw something in the image that you want to use as the background for the form. The sample shown in Figure 24-3 draws some rounded rectangles to simulate a custom-built form interface. Once you’re satisfied, save the project so the resource file has the new image updated into the project.

Figure 24-3

326

The My Namespace

5.Open Form1 in Design view and double-click on the form’s design surface so that Visual Studio automatically inserts the default stub subroutine for the Form_Load event. Add a single line to the procedure as follows:

Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

Me.BackgroundImage = My.Resources.MainFormBackground

End Sub

This line of code sets the BackgroundImage property of the form to the resource image you added earlier. Note how the name of the resource object can be referred to directly in code. You could even interrogate the resource file’s properties and correspondingly act on them.

6.Run the application. The form will be displayed with a background of the resource image. Notice that as you type the line of code, IntelliSense will show you the list of available resources in the My.Resources list, as illustrated in Figure 24-4.

Figure 24-4

Other My Classes

The other My classes are fairly basic in their usage. The My.User class is used to determine information about the current user. When using role-based security, this includes the Current Principal, but as you saw earlier in this chapter, you can also retrieve the user’s login name and whether they belong to specific roles.

My.Settings exposes the Settings strings in your application, enabling you to edit or retrieve the information in the same way that My.Forms exposes the application form objects and My.Resources exposes the resource file contents. Settings can be either applicationwide or userwide, and can be persisted between sessions with the My.Settings.Save method.

Finally, My.Log is an alternative way of addressing the application log.

Summar y

The My namespace is an extremely powerful feature introduced with Visual Studio 2005. Although it was originally intended for Visual Basic programmers, J# and C# developers can also harness the efficiencies it offers in the code they write.

The My.Application and My.Computer objects expose a large array of information about the current application process and the computer it’s running on, enabling you to quickly determine what situation your application is in and respond effectively.

327

Part VI

Automation

Chapter 25: Code Generation Templates

Chapter 26: Macros

Chapter 27: Connection Strings

Chapter 28: Assembly Signing

Chapter 29: Preemptive Error Correction

Chapter 30: Strongly Typed Datasets

Chapter 31: Data Binding and Object Data Sources

Chapter 32: Add-Ins

Chapter 33: Third-Party Extensions

Chapter 34: Starter Kits

Code Generation Templates

Most development teams build a set of standards that specifies how they build applications. This means that every time you start a new project or add an item to an existing project, you have to go through a process to ensure that it conforms to the standard. Visual Studio 2005 enables you to create templates that can be reused, without having to modify the standard item templates that it

ships with. This chapter describes how you can create simple templates and then extend them using the IWizard interface. It also examines how you can create a multi-project template that can greatly save you some time when starting a new application.

Creating Templates

There are two types of templates: those that create new project items and those that create entire projects. Both types of templates essentially have the same structure, as you will see later, except that they are placed in different template folders. The project templates appear in the Add New Project dialog, whereas the item templates appear in the Add New Item dialog.

Item Template

Although it is possible to build a template manually, it is much quicker to create a template from an existing sample and make changes as required. This section begins by looking at a project item template — in this case, an About form that contains some basic information, such as who the application was written by and the version of the application. The About screen, shown in Figure 25-1, has been created in an existing project.

Chapter 25

Figure 25-1

To make a template out of the About form, select the Export Template item from the File menu. This starts the Export Template Wizard, shown in Figure 25-2. The first step is to determine what type of template you want to create. In this case, select the Item Template radio button and make sure that the project in which the About form resides is selected in the drop-down list.

Figure 25-2

Click Next. You will be prompted to select the item on which you want to base the template. In this case, select the About form. The use of checkboxes is slightly deceiving, as you can only select a single item on which to base the template. After making your selection and clicking Next, the dialog shown in Figure 25-3 enables you to include any project references that you may require. This list is based on the list of references in the project in which that item resides. Because this is a form, include a reference to the System

.Windows.Forms library. If you did not, and a new item of this type were added to a class library, it is highly possible that the project would not compile, as it would not have a reference to this assembly.

The final step in the Export Template Wizard is to specify some properties of the template to be generated, such as the name, description, and icon that will appear in the Add New Item dialog. Figure 25-4 shows the final dialog in the wizard. As you can see, there are a couple of options to display the output folder upon completion and whether to automatically import the new template into Visual Studio 2005.

332

Code Generation Templates

Figure 25-3

Figure 25-4

333

Chapter 25

For some reason the team who wrote this feature decided that the output location could not be editable, and that all templates were to be placed in the My Exported Templates folder within the current user’s Documents and Settings folder. If you take a look at this folder (see Figure 25-5), you can see a number of folders that contain user settings about Visual Studio 2005.

Figure 25-5

Also notice the Templates folder in Figure 25-5. Visual Studio 2005 looks in this folder for additional templates to display when you are creating new items. Not shown here are two subfolders beneath the Templates folder that hold Item templates and Project templates, respectively. These in turn are divided by language. If you check the “Automatically import the template into Visual Studio” option on the final page of the Export Template Wizard, the new template will not only be placed in the output folder; it will also be copied to the relevant location, depending on language and template type, within the Templates folder. Visual Studio 2005 will automatically display this item template the next time you display the Add New Item dialog, as shown in Figure 25-6.

Figure 25-6

334

Code Generation Templates

Project Template

You build a project template the same way you build an item template but with one difference. Whereas the item template is based on an existing item, the project template needs to be based on an entire project. For example, you might have a simple project, as shown in Figure 25-7, that has a main form, complete with menu bar, an About form, and a splash screen.

Figure 25-7

To generate a template from this project, you follow the same steps you took to generate an item template, except you need to select Project Template when asked what type of template to generate. After completing the Export Template Wizard, the new project template will appear in the New Project dialog, shown in Figure 25-8.

Figure 25-8

Template Structure

Before examining how to build more complex templates, you need to understand what is produced by the Export Template Wizard. If you look in the My Exported Templates folder, you will see that all the templates are exported as compressed zip folders. The zip folder can contain any number of files or

335