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

Automate scrolling when the image is larger than the form.

Examine the Panel class, and draw our image in a Panel object instead of directly onto the form.

As we have done in previous chapters, we begin by looking at the class hierarchy for the Form object in more detail.

7.1FORM CLASS HIERARCHY

If you have actually read this book from the beginning, you will recall that we looked at the Menu class hierarchy in chapter 3 and the StatusBar hierarchy in chapter 4. Chapter 3 introduced some of the low-level classes used by Windows Forms, and chapter 4 extended this hierarchy to include the Control class.

The Form class is based on additional extensions to enable scrolling and containment. The complete hierarchy is shown in figure 7.1.

Figure 7.1 The Form class hierarchy extends the Control class discussed in chapter 4 with the functionality required for various kinds of application windows.

As you can see in the figure, the ScrollableControl and ContainerControl classes extend the Control class discussed in chapter 4 to support functionality required by the Form class. The ScrollableControl class adds auto-scrolling capabilities, while the ContainerControl adds focus management on the contained set of controls, even when the container itself does not have the focus.

FORM CLASS HIERARCHY

195

7.1.1THE SCROLLABLECONTROL CLASS

You might think that all classes with scrolling support inherit from the ScrollableControl object. In fact, this class is only for objects which support automated scrolling for a contained set of Control objects. Controls such as ListBox, TextBox, and other controls that provide scrolling of their drawing area do so indepen-

dently of this object using the ScrollBar class, which is unrelated to the

ScrollableControl class.

The TextBox and ListBox classes are discussed in chapters 9 and 10, respectively. The point here is that the ScrollableControl class is specifically designed for the scrolling support required by container objects such as forms and panels. A summary of this class is provided in the .NET Table 7.1.

We will see the members of this class in action later in the chapter when we enable scrolling within our application.

.NET Table 7.1 ScrollableControl class

The ScrollableControl class represents a control that supports automated scrolling. This class is part of the System.Windows.Forms namespace and inherits from the System.Windows.Forms.Control class. This class is not typically used directly. Instead, derived classes such as Form and Panel are used. See .NET Table 4.1 on page 104 for a list of members inherited from the Control class.

 

AutoScroll

Gets or sets whether the user can scroll the container

 

 

to any contents placed outside of its visible boundaries.

 

AutoScrollMargin

Gets or sets the extra margin to add to the container’s

 

 

contents for scrolling purposes. This ensures that the

 

 

scrollable area goes slightly beyond the edge of any

 

 

contained controls.

Public

AutoScrollMinSize

Gets or sets the Size object representing the minimum

Properties

 

height and width of the scrollbars in pixels.

 

 

 

AutoScrollPosition

Gets or sets the Point within the virtual display area to

 

 

appear in the upper left corner of the visible portion of

 

 

the control.

 

DockPadding

Gets or sets the extra padding for the inside border of

 

 

this control when it is docked.

 

 

 

Public

SetAutoScrollMargin

Sets the AutoScrollMargin property.

Methods

 

 

 

 

 

Protected

HScroll

Gets or sets whether the horizontal scroll bar is visible.

Properties

VScroll

Gets or sets whether the vertical scroll bar is visible.

 

 

 

 

7.1.2THE FORM CLASS

We will skip over the ContainerControl class from figure 7.1 and jump straight to the Form class. The ContainerControl class simply adds functionality for focus management on the contained controls. For example, an ActiveControl property

196

CHAPTER 7 DRAWING AND SCROLLING

sets or gets the Control object that has the focus; while the ActivateControl method sets the focus to a specific control. The functionality is useful, but not as glamorous as scroll bars or forms, so we skip it.

.NET Table 7.2 Form class

The Form class represents any window that can be displayed by an application, including standard windows as well as modal or modeless dialog boxes and multiple document interface (MDI) windows. This class is part of the System.Windows.Forms namespace and inherits from the ContainerControl class. The contents of a form can be drawn directly by a program, consist of a collection of controls, or some combination of the two. These contents can also be larger than the visible area, with scrolling supported by the ScrollableControl class (see .NET Table 7.1).

Public Static

ActiveForm

Gets the Form currently active in the application,

Properties

 

or null if no Form is active.

 

 

 

 

AcceptButton

Gets or sets the button to invoke when the Enter

 

 

key is pressed.

 

ControlBox

Gets or sets whether a control box appears at

 

 

the left of the title bar.

 

DialogResult

Gets or sets the dialog result to return when the

 

 

form is a modal dialog box.

 

Icon

Gets or sets the icon for the form.

 

IsMdiChild

Gets whether this form is an MDI child form.

 

 

MDI forms are discussed in chapter 16.

Public Properties

MaximizeBox

Gets or sets whether a Maximize button appears

 

in the title bar of the form.

 

MaximumSize

Gets or sets the maximum size for a form.

 

Menu

Gets or sets the MainMenu object for this form.

 

Modal

Gets whether this form is displayed modally.

 

ShowInTaskBar

Gets or sets whether the form is displayed in the

 

 

Windows task bar.

 

StartPosition

Gets or sets the initial position of the form when

 

 

it is displayed.

 

WindowState

Gets or sets how the form is displayed on the

 

 

desktop (normal, maximized, or minimized).

 

 

 

 

Activate

Activates the form and gives it focus.

Public Methods

Close

Closes the form.

 

ShowDialog

Displays this form as a modal dialog box.

 

 

 

 

Closing

Occurs when the form is about to close.

Public Events

Deactivate

Occurs when the form has lost the focus.

 

Load

Occurs before a form is initially displayed.

 

 

 

FORM CLASS HIERARCHY

197

On to our friend the Form class. This class can be used for just about any application window, including borderless, floating, and dialog box windows. The Control class we discussed in chapter 4 provides a number of useful members for dealing with forms. For example, the Width and Height properties determine the size of the form, the DisplayRectangle property holds the drawable area of the form, and the Cursor property gets or sets the current cursor to display. A number of common events such as Click, KeyDown, MouseUp, and Paint are also inherited from this class. Scrolling, of course, is provided by the ScrollableControl class. An overview of the members specific to the Form class is shown in .NET Table 7.2.

Once you have perused the Form overview and memorized its contents, go on to the next section where we will make some practical use of some of these members.

7.2IMAGE DRAWING

Well, we are ready to utilize the Form class members, but where to begin? We will avoid dialog boxes and other new windows for the time being, and stick with the single form in our application. A good first topic we can cover here is drawing on a form.

As we have seen, drawing in .NET is performed using the System.Drawing namespace. We used classes from this namespace in chapter 4 when we created an owner-drawn status bar panel. Here, we continue to use the Graphics class for drawing, but will make use of some alternate members. Before we can do this, we need a place to draw. To generate such a place, we will remove the PictureBox object from our application.

7.2.1DELETING THE PICTUREBOX CONTROL

Beside the need for a place to draw, the PictureBox control just isn’t working out here. Like the Load button before it, it is just the wrong control for the task at hand. In chapter 2 we saw how this control stretched and distorted our image, and in chap-

ter 3 we saw that scrolling was not supported when the displayed image exceeded the size of the control. So, it was nice while it lasted, but off it goes.1

Let’s get this task out of the way so we can draw with a somewhat freer hand.

1 It is, in fact, possible to extend the PictureBox control to provide this support. You can override the Paint event to draw an image with the proper aspect ratio much like we do in this chapter, and scroll bars can be added using the ScrollBar class. We do not take this approach here since we want to discuss forms and panels, so the PictureBox control is no longer needed. Chapter 15 displays a properly proportioned image within a PictureBox control, and chapter 18 provides a short discussion on how to create a custom control incorporating this functionality.

198

CHAPTER 7 DRAWING AND SCROLLING

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