Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

Windows Forms

308

PART III

FIGURE 3.4.2

The French connection to resources.

There was a reason for only modifying one of the resources in the French satellite assembly. This illustrates that the resource manager will use strings and other resources from the correct locale or culture if they exist, but it will fall back to the default information in the main assembly if they do not. The “WindowText” string is not in the satellite assembly, so the default was used.

Using Visual Studio.NET for Internationalization

Visual Studio.NET will manage resources for you and help you to create localized forms and components. Figure 3.4.3 shows the Localizable property being set for a form. Setting this property to true will cause the design time environment to store all the relevant information in the resource file for you.

FIGURE 3.4.3

Setting the Localizale property.

The InitializeComponent method for Form1 will now contain code to initialize a resource manager and retrieve any information that might change as the application is transferred between locales from the resources.

Windows Forms Example Application (Scribble .NET)

309

CHAPTER 3.4

As you saw with the handmade example shown in Figure 3.4.2, the physical extents of text strings could change as they are translated, so as well as the text itself, you will find position and size information stored in the resources. InitializeComponent method for Form1 is shown in Listing 3.4.5.

LISTING 3.4.5 Form1.cs: InitializeComponent Method

1:private void InitializeComponent()

2:{

3:System.Resources.ResourceManager resources =

4:new System.Resources.ResourceManager(typeof(Form1));

5:this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

6:this.ClientSize = ((System.Drawing.Size)(resources.GetObject(“$this.ClientSize”)));

7:this.Text = resources.GetString(“$this.Text”);

8: 9: }

You can see that the IDE has created a new resource manager on line 3, and lines 6 and 7 retrieve text and client size information from the resources.

When a form is made localizable in this way, all the components you put on the form will also save their text, size, and position information to a resource file. Listing 3.4.4 shows the InitializeComponent method after the addition of a label to Form1.

LISTING 3.4.4 Form1.cs: InitializeComponent after Adding a Label

1:private void InitializeComponent()

2:{

3:System.Resources.ResourceManager resources =

4:new System.Resources.ResourceManager(typeof(Form1));

5:this.label1 = new System.Windows.Forms.Label();

6:this.label1.Location =

7:((System.Drawing.Point)(resources.GetObject(“label1.Location”)));

8:this.label1.Size =

9:((System.Drawing.Size)(resources.GetObject(“label1.Size”)));

10:this.label1.TabIndex =

11:((int)(resources.GetObject(“label1.TabIndex”)));

12:this.label1.Text = resources.GetString(“label1.Text”);

13:this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

14:this.ClientSize =

15:((System.Drawing.Size)(resources.GetObject(“$this.ClientSize”)));

16:this.Controls.AddRange(new System.Windows.Forms.Control[]{this.label1});

17:this.Text = resources.GetString(“$this.Text”);

18:}

3.4

INDOWSWEA FXAMPLEPPLICATION ORMS

Windows Forms

310

PART III

Now the label child component of the form stores its location, size, tab-index, and text in the resources.

Image Resources

So far, we have dealt with string resources but, like Win32 programs, .NET applications store images in resources for icons, backgrounds, and other things. Ultimately, the images get stored in the compiled .resource form, but, when you create them in Visual Studio or by hand, they are converted to the XML-based.resx form. Obviously, editing an image file as text in XML form is going to be no fun at all. The prescribed method for placing images in the resources is with the tools, such as VS.NET, provided by Microsoft.

A component, for example, might have an image as a background. You would place the component on the form and edit the component’s background image property. The editor will allow you to select an image that is then placed into the resource for you. Figure 3.4.4 shows an image used on a form in this way.

FIGURE 3.4.4

Placing a background image on a form.

Windows Forms Example Application (Scribble .NET)

311

CHAPTER 3.4

Using Image Lists

Many components require images for their user interface. The ListView and TreeView classes can use images to enhance their appearances. Image lists are collections of bitmap images held in a single file. These images can have a transparent background keyed from a chosen color so that only the chosen portion of the image is shown.

The demo code for this section will be created as we go. Using Visual Studio, create a new C# application and call it imagelistdemo. Follow along for the rest of the chapter to complete the job.

To create an image list with Visual Studio, you should first drag an ImageList component from the toolbox onto the form. Now create some images. MS Paint is suitable for this task, just remember to save the images into a common directory. Figure 3.4.5 shows a 16×16 icon being edited in MS Paint.

NOTE

For the purposes of this example, we have prepared four small icons, icon1.bmp through icon4.bmp, available from the Web site.

FIGURE 3.4.5

Editing an icon image.

The background of the icon is filled with magenta. This is a color that does not appear anywhere on the actual icon image, so it is safe to use it as the transparency key. To add a collection of images to the ImageList object, you can select the Images collection in the object properties and click the Browse button. This brings up a dialog, as shown in Figure 3.4.6, that

3.4

INDOWSWEA FXAMPLEPPLICATION ORMS

Windows Forms

312

PART III

can be used to add as many images as you need to the list. Note that it is important to keep all images in an image list the same size—in this instance, 16×16.

FIGURE 3.4.6

Adding images to an image list.

You can see from the image collection editor dialog that the four images are numbered from 0–3, and each has properties that can be viewed within the editor.

Now, to use these images in your ListView object, from the toolbox, drag a ListView object onto the form. For the moment, we’ll simply get the ListView to display small icons. So select the listView1 object and show its properties. The property called SmallImageList can now be set to use the imageList1 that we just created.

Let’s take a sneak peek at the InitializeComponent code (see Listing 3.4.5) right away to see how these tasks have affected the code.

LISTING 3.4.5 Form1.cs: InitializeComponent

1:private void InitializeComponent()

2:{

3:this.components = new System.ComponentModel.Container();

4:System.Resources.ResourceManager resources =

5:new System.Resources.ResourceManager(typeof(Form1));

6:this.listView1 = new System.Windows.Forms.ListView();

7:this.imageList1 = new System.Windows.Forms.ImageList(this.components);

8:this.SuspendLayout();

9://

10:// listView1

Windows Forms Example Application (Scribble .NET)

CHAPTER 3.4

LISTING 3.4.5 Continued

11://

12:this.listView1.Location = new System.Drawing.Point(8, 8);

13:this.listView1.Name = “listView1”;

14:this.listView1.Size = new System.Drawing.Size(272, 200);

15:this.listView1.SmallImageList = this.imageList1;

16:this.listView1.TabIndex = 0;

17://

18:// imageList1

19://

20:this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;

21:this.imageList1.ImageSize = new System.Drawing.Size(16, 16);

22:this.imageList1.ImageStream ((System.Windows.Forms.ImageListStreamer)(resources.GetObject(“imageList1.ImageStream”)));

23:this.imageList1.TransparentColor = System.Drawing.Color.Magenta;

24://

25:// Form1

26://

27:this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

28:this.ClientSize = new System.Drawing.Size(292, 273);

29:this.Controls.AddRange(new System.Windows.Forms.Control[] {

30:this.listView1});

31:this.Name = “Form1”;

32:this.Text = “Form1”;

33:this.ResumeLayout(false);

34:}

Lines 4 and 5 created the ResourceManager for the form. Lines 12–16 create the ListView and sets up the SmallImageList property to use the imageList1 component. Lines 20–24 initialize the image list by getting the image list streamer, setting the transparent color and the image size.

To use the resources in the ListView, select the properties for the ListView object, select the Items collection property, and click the button marked with ellipsis (…). This brings up the dialog shown in Figure 3.4.7 that lets you add and edit items in the list box.

NOTE

Note that your programs can also accomplish adding items to components dynamically. Visual Studio provides an additional RAD way of doing things.

313

3.4

INDOWSWEA FXAMPLEPPLICATION ORMS

Windows Forms

314

PART III

FIGURE 3.4.7

Using resource-based images in ListViewItem objects.

Each of the items added to the tree can be given some text and an image to display. The ListView also provides events similar to those seen in the tree view demonstration from Chapter 3.2, “User Interface Components,” when list items are selected, edited, and so on. Handling these events allows you to interact with the list.

Let’s take a look now at the final InitializeComponent listing for the example shown (see Listing 3.4.6).

LISTING 3.4.6 Form1.cs: InitializeComponent

1:private void InitializeComponent()

2:{

3:this.components = new System.ComponentModel.Container();

4:System.Resources.ResourceManager resources = new

5:System.Resources.ResourceManager(typeof(Form1));

6:System.Windows.Forms.ListViewItem listViewItem1 = new

7:System.Windows.Forms.ListViewItem(“Folder”,0);

8:System.Windows.Forms.ListViewItem listViewItem2 = new

9:System.Windows.Forms.ListViewItem(“Open Folder”, 1);

10:System.Windows.Forms.ListViewItem listViewItem3 = new

11:System.Windows.Forms.ListViewItem(“Book”, 2);

12:System.Windows.Forms.ListViewItem listViewItem4 = new

13:System.Windows.Forms.ListViewItem(“Wrench”, 3);

14:this.imageList1 = new System.Windows.Forms.ImageList(this.components);

15:this.listView1 = new System.Windows.Forms.ListView();

16:this.SuspendLayout();

17://

18:// imageList1

19://