Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

12.3.3PLACING IMAGES ON OUR BUTTONS

We have created the buttons in our PhotoEditDlg form, and created bitmaps for these buttons within our project. The next step is to reconfigure our buttons to display these images. A button can contain both an image and text string, or only an image, or only text. For our example, we will include both the image and text on our buttons.

The steps required are as follows:

ADD THE BITMAP IMAGES TO THE BUTTONS

 

 

Action

Result

 

 

 

 

 

 

 

1

In the PhotoEditDlg.cs [Design]

See the graphic in step 2.

 

window, modify the Next button to

 

 

display its text on the left side and

 

 

image on the right side of the button.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

ImageAlign

MiddleRight

 

 

 

TextAlign

MiddleLeft

 

 

 

 

 

 

 

 

2

Assign the NextButton.bmp bitmap to

 

 

the Image property of the btnNext

 

 

control.

 

 

 

 

 

How-to

 

 

 

 

 

a. If necessary, save any changes to

 

 

the NextButton.bmp file.

 

 

b. Click on the Image item in the Prop-

 

 

erties window.

 

 

 

 

 

c. Click the button.

 

 

d. Locate and open the NextBut-

 

 

ton.bmp file.

 

 

 

 

 

 

 

 

 

 

 

3

Modify the Prev button to display its

 

 

text on the right side and image on the

 

 

left side of the button.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

ImageAlign

MiddleLeft

 

 

 

TextAlign

MiddleRight

 

 

 

 

 

 

 

 

4

Assign the PrevButton.bmp bitmap to

 

 

the Image property of the btnPrev

 

 

control.

 

 

 

 

 

 

 

 

 

 

 

402

CHAPTER 12 A .NET ASSORTMENT

As you can see, the ImageAlign and TextAlign properties are used to set the location of the image and text within the button area. These take their values from the ContentAlignment enumeration, with all nine possible combinations of Top, Middle, and Bottom with Left, Center, and Right represented. Both properties use the MiddleCenter value by default.

Assigning the Image property is a simple matter of locating the desired file in the file system. The source code generated in the InitializeComponent method by this action is rather interesting. Here is an excerpt of the PhotoEditDlg.cs file.

private void InitializeComponent()

Load the PhotoEditDlg

b

{

 

resources

 

 

 

System.Resources.ResourceManager resources

 

 

 

 

 

 

 

 

 

 

= new System.Resources.ResourceManager(typeof(PhotoEditDlg));

 

 

 

 

. . .

 

 

 

 

 

this.btnNext = new System.Windows.Forms.Button();

 

 

 

 

this.btnPrev = new System.Windows.Forms.Button();

 

 

 

 

. . .

 

 

 

 

 

//

 

Load a specific

 

 

 

// btnNext

 

bitmap resource

c

//

 

 

 

 

 

this.btnNext.Image = ((System.Drawing.Bitmap)

 

 

 

 

 

(resources.GetObject("btnNext.Image")));

 

 

 

 

 

 

this.btnNext.ImageAlign

 

 

 

 

 

= System.Drawing.ContentAlignment.MiddleRight;

 

 

 

 

. . .

 

 

 

 

 

this.btnNext.Text = "Nex&t";

 

 

 

 

 

this.btnNext.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.btnNext.Click += new System.EventHandler(this.btnNext_Click);

//

// btnPrev

//

this.btnPrev.Image = ((System.Drawing.Bitmap) (resources.GetObject("btnPrev.Image"))); C

this.btnPrev.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;

. . .

}

You will note that the names of our original files, NextButton.bmp and PrevButton.bmp, do not appear in this listing. Instead, these files are encapsulated in a cul- ture-specific resource for our library. A culture-specific resource is a text string, image, and other language or culture-specific object used in an application, library, dialog, or other construct. The term resource is often used as an abbreviation for such objects. A few comments on the previous code are in order:

bVisual Studio creates a resource file specifically for the MyPhotoAlbum project. This file is called PhotoEditDlg.resx and appears in the MyPhotoAlbum project directory. When your program is compiled in Visual Studio, this file is compiled into a

.resources file based on the fully qualified name of the dialog. The resgen.exe compiler is used to generate resource files from the command line. In our application, the

IMAGE BUTTONS

403

Manning.MyPhotoAlbum.Photo-
ResourcesMan-

.resources file, called Manning.MyPhotoAlbum.PhotoEditDlg.resources, appears in the obj directory under the MyPhotoAlbum project directory, and is included in the final MyPhotoAlbum.dll assembly produced by the compiler. The

ager class is part of the System.Resources namespace, and loads the .resources file for the given object type, in this case the

EditDlg type, so that the specific resources in this file may be accessed.

cA specific resource, in these cases our bitmap files, is loaded from the .resources file using the GetObject method. This returns the object corresponding to the given name, which can safely be typecast to the more appropriate Bitmap class.

The important points to take away from this discussion are that .resx files are used to encapsulate language-specific objects, and get compiled into .resources files for access by a program. These concepts are the basis for localization support in .NET, permitting the resources required to run a program in the United States to be encapsulated and later converted to run the same program in France, New Zealand, or Botswana with language and cultural requirements taken into account.

As long as we’re here, take a quick look at the PhotoEditDlg.resx file. Such files use an XML format to encapsulate resource objects, permitting graphical programs such as Visual Studio as well as text editors like Notepad to view and edit their contents. An excerpt of this file, including the btnNext control’s image definition, is shown as it appears on my computer. We won’t go into the details of XML or the .resx file format here, but it is useful to see how the bitmap for the Next button, named

btnNext.Image, is specified. Be careful not to change these entries, as you may adversely affect your program.

<?xml version="1.0" encoding="utf-8"?>

<root>

. . .

<xsd:schema id="root" . . . >

. . .

</xsd:schema>

. . .

<data name="btnNext.Image" type="System.Drawing.Bitmap, System.Drawing, Version=1.0.3300.0,

Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mimetype="application/x-microsoft.net.object.bytearray.base64">

<value>

Qk1OAQAAAAAAAHYAAAAoAAAAEgAAABIAAAABAAQAAAAAAAAAAADEDgAAxA4AABAAAAAQAAAAAAAA/wAA

gP8AgAD/AICA/4AAAP+AAID/gIAA/8DAwP+AgID/AAD//wD/AP8A/////wAA//8A/////wD/////////

/////////wASAP///////////wASAP///////////wASAP/////wD////wASAP/////wAP///wASAP//

////AA///wASAP//////8AD//wASAP///////wAP/wASAP8AAAAAAAAA/wASAP8AAAAAAAAA/wASAP//

/////wAP/wASAP//////8AD//wASAP//////AA///wASAP/////wAP///wASAP/////wD////wASAP//

/////////wASAP///////////wASAP///////////wASAA==

</value>

</data>

. . .

</root>

404

CHAPTER 12 A .NET ASSORTMENT

Соседние файлы в папке c#