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

1.4Resizing forms

The final topic we touch on in this chapter is resizing forms. For readers familiar with MFC programming in Visual C++, you will know that it can take some work to properly resize a complicated form. The folks at Microsoft were likely aware of this and sought to simplify this task in .NET.

Before looking at our new code listing, try resizing our existing program to see what happens. The position of each control is fixed relative to the top-left corner of the form, as shown in figure 1.5.

Figure 1.5

Version 1.3 of our application uses the default resize behavior, with both controls anchored to the top and left of the window.

We would prefer the PictureBox control to resize automatically along with the window, as is shown in figure 1.6. Fortunately, Windows Forms controls provide a couple of properties to achieve this effect, namely the Anchor and Dock properties.

Figure 1.6

Version 1.4 of our application anchors the picture box control to all sides of the window, so that it resizes automatically whenever the window is resized.

26

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

Revise your code so that it matches listing 1.4. This new code sets the Anchor property for each control, and uses the version number 1.4. As before, the changes to our code from section 1.3 are shown in bold.

Listing 1.4 The PictureBox resizes based on the Anchor property setting

[assembly: System.Reflection.AssemblyVersion("1.4")]

namespace MyNamespace

{

using System;

using System.Drawing;

using System.Windows.Forms;

public class MyForm : System.Windows.Forms.Form

{

private Button btnLoad; private PictureBox pboxPhoto;

public MyForm()

{

// Constructor

this.Text = "Hello Form 1.4";

this.MinimumSize = new Size(200,200);

// Create and configure the Button btnLoad = new Button(); btnLoad.Text = "&Load"; btnLoad.Left = 10;

btnLoad.Top = 10;

btnLoad.Click += new System.EventHandler(this.OnLoadClick); btnLoad.Anchor = AnchorStyles.Top | AnchorStyles.Left;

// Create and configure the PictureBox pboxPhoto = new PictureBox();

pboxPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; pboxPhoto.Width = this.Width / 2;

pboxPhoto.Height = this.Height / 2;

pboxPhoto.Left = (this.Width - pboxPhoto.Width) / 2; pboxPhoto.Top = (this.Height - pboxPhoto.Height) / 2; pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;

pboxPhoto.Anchor = AnchorStyles.Top | AnchorStyles.Bottom

| AnchorStyles.Left | AnchorStyles.Right;

// Add our new controls to the Form this.Controls.Add(btnLoad); this.Controls.Add(pboxPhoto);

}

protected void OnLoadClick(object sender, System.EventArgs e)

{

OpenFileDialog dlg = new OpenFileDialog();

dlg.Title = "Open Photo";

dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;

RESIZING FORMS

27

if (dlg.ShowDialog() == DialogResult.OK)

{

pboxPhoto.Image = new Bitmap(dlg.OpenFile());

}

dlg.Dispose();

}

public static void Main()

{

Application.Run(new MyForm());

}

}

}

As an aside, figure 1.6 exposes a problem with our application that will need to be fixed. Since the image scales along with our PictureBox control, the aspect ratio changes as well. The aspect ratio is the ratio of the height of an image to its width. A standard 4-inch by 6-inch photograph, for example, has an aspect ratio of two-thirds (4 divided by 6). As the form is resized the image is distorted to fit the control, which affects the aspect ratio. We will fix this in chapter 7. In the meantime, keep in mind that our program exhibits what can only be called a bug.

While we have only added three lines here, they lead us to some interesting discussion points.

1.4.1Desktop layout properties

The first change in our program sets the MinimumSize property to define the minimum possible size for the form. This ensures that the form never becomes so small that the PictureBox disappears and our image cannot be seen.

The MinimumSize property is a Size structure representing the minimum width and height of the form. As you may recall, structures are value types and store their data directly, either on the stack or as part of the containing type. As we discussed in section 1.1.3, the new keyword is used to create a new value type. When one value type is assigned to another, as we do here, the contents of the original type are copied into the target type. As a result, the fact that the newly allocated Size structure is destroyed when the MyForm constructor is finished has no effect on the value stored within the MyForm class.

public MyForm()

{

. . .

this.MinimumSize = new Size(200,200);

. . .

}

28

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

Note that the System.Drawing namespace defines a number of structures that are used in a similar manner, including the Size, Point, and Rectangle structures. We will encounter these types repeatedly throughout the book.

The MinimumSize property is one of a number of properties that control how a form behaves on the Windows Desktop. While not directly related to our discussion, this is a good place to introduce these properties as a set. Figure 1.7 illustrates how these properties relate to the desktop.

A brief explanation of each property shown in figure 1.7 is provided in the following table:

Property

Type

Description

 

 

 

ControlBox

bool

Whether to include a control box (upper-left icon) on the

 

 

form.

DesktopBounds

Rectangle

The bounds (area) of the form on the desktop.

DesktopLocation

Point

The location of the upper left corner of the form on the

 

 

desktop.

FormBorderStyle

FormBorderStyle

This defines whether the form is a dialog box, whether it

 

 

is resizable, and what type of outer border is used.

Icon

Icon

The icon, or picture, used to represent the form. This

 

 

appears in the control box and on the taskbar.

MaximizedBounds

Rectangle

The bounds of the form when it is maximized. This

 

 

property is protected.

MaximizeBox

bool

Whether to include a maximize box on the form. Note

 

 

that this is only shown if the ControlBox property is

 

 

true.

MaximumSize

Size

The maximum size to which the form can be resized.

MinimizeBox

Size

Whether to include a minimize box on the form. Note that

 

 

this is only shown if the ControlBox property is true.

MinimumSize

Size

The minimum size to which the form can be resized.

ShowInTaskBar

Bool

Whether to show the form on the Windows taskbar.

 

 

 

1.4.2The Anchor property

The remaining two lines added to our program use the Anchor property to fix the control on the form in relation to the form’s edges.

//Create and configure the Button

. . .

btnLoad.Anchor = AnchorStyles.Top | AnchorStyles.Left;

//Create and configure the PictureBox

. . .

pboxPhoto.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

RESIZING FORMS

29

b ControlBox icon

e DesktopBounds

h MaximizedBounds

 

DesktopLocation

 

c FormBorderStyle

f MaximumSize

I MinimizeBox

d MinimumSize

g ShowInTaskbar

j MaximizeBox

 

 

b

Ij

 

c

 

 

d

e

h

f

g

Figure 1.7 The properties for the Form class that relate to the Windows desktop define the size, appearance, and position of the form on the desktop.

All controls in the .NET Framework support the Anchor property for this purpose. The property is set using the AnchorStyles enumeration, discussed in .NET Table 1.3..

.NET Table 1.3 AnchorStyles enumeration

The AnchorStyles enumeration specifies the settings available for the Anchor property in the Control class, and by inheritance all controls in the .NET Framework. The enumeration is part of the System.Windows.Forms namespace. An Anchor property is set for a control object using a bitwise or (with the vertical bar ‘|’ operator) of the desired values.

 

Bottom

Control is anchored to the bottom edge of its container.

 

Left

Control is anchored to the left edge of its container.

 

None

Control is not anchored to its container. When an

Enumeration values

 

Anchor property is set to None, the control moves half

 

 

the distance that its container is resized in all directions.

 

Right

Control is anchored to the right edge of its container.

 

Top

Control is anchored to the top edge of its container.

 

 

 

30

CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS

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