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

C H A P T E R

1 6

 

 

Multiple document interfaces

16.1

Interface styles 526

16.4

MDI children 543

16.2

MDI forms 530

16.5

MDI child window management 557

16.3

Merged menus 535

16.6

Recap 563

The ListView and TreeView classes discussed in chapters 14 and 15 present a collection of objects within a single list or tree control. These are especially useful when creating an explorer-style interface such as our MyAlbumExplorer application, or the common Windows Explorer application. Another kind of interface is the multiple document interface, also called an MDI (normally pronounced em-dee-eye).

An MDI application presents a collection of forms within a single application window. We will discuss MDI applications through the following discussion areas:

Understanding various interface styles.

Creating an MDI container window.

Converting an SDI application into an MDI application.

Using MDI-related class members of various controls.

Merging two menus into a single merged menu.

Managing menus and forms in an MDI application.

These topics will be covered as we progress through the chapter, beginning with the concept of interface styles.

525

16.1INTERFACE STYLES

Before we discuss exactly how multiple document interfaces are created, let’s take a step back and consider the various types of application interfaces used for Windows applications. Most Windows applications fall into one of three interface categories:

Single document interfaces.

Explorer interfaces.

Multiple document interfaces.

We will discuss each type of interface separately.

16.1.1SINGLE DOCUMENT INTERFACES

A single document interface, also called an SDI, is an interface that displays a single document or other encapsulated data within a single form. Our MyPhotos application, as shown in figure 16.1, is a good example of this style, in which a single photo album is displayed. The user can look at multiple photo albums only by examining one after another. The contents of two albums cannot be compared unless two copies of the program are running.

In the Windows operation system, the Notepad and WordPad applications provide additional examples of the SDI style.

Figure 16.1

Our single document interface displays one photo album at a time.

16.1.2EXPLORER INTERFACES

The MyAlbumExplorer application built in chapters 14 and 15 is an example of an explorer interface, and can be seen in figure 16.2. In this style, a hierarchy of information is presented to the user. Normally a TreeView control displays this hierarchy, typically on the left, with details on the selected node provided in a ListView control. Sometimes the TreeView control can be hidden, and sometimes it is always

526

CHAPTER 16 MULTIPLE DOCUMENT INTERFACES

present. Alternate information may appear on the list side of the window as well, such as the photographic image we displayed in chapter 15 for a selected photograph in the MyAlbumExplorer application.

In Windows, of course, the Windows Explorer application is another example of this style.

Figure 16.2 Our explorer interface presents the collection of photo albums in list form.

16.1.3MULTIPLE DOCUMENT INTERFACES

A multiple document interface (MDI) allows multiple views of one or more documents or other encapsulated data to be displayed at the same type. This permits alternate views of the same data, or separate presentations of the same style of data, within a single window. For example, a stock market MDI application might present different historical or graphical views of a single portfolio, each as a separate window. Alternately, such an application might present multiple portfolios, each as its own window within a containing application window.

In the original conception of this style, a single window acted as a container for other windows, where each contained window displayed a specific instance or view of a type of data. More recently, well-known MDI applications such as Microsoft Word and Excel have taken the approach of displaying all of their windows directly on the desktop, each within a separate application window, while still preserving an MDI look and feel from the menu bar and other parts of the interface. This relatively new style, the Multiple Single Document Interface, or MSDI, is consistent with the manner in which Web browsers have typically worked. While an MSDI interface can be created in Visual Studio.NET, it is not necessarily an easy task.

INTERFACE STYLES

527

Also note that Visual Studio .NET, while providing an MDI-like interface, uses more of a TabControl look and feel for the set of displayed windows, or what might be called a Multiple Tabbed Documents Interface, or MTDI. In this style, multiple sets of windows are displayed as horizontal or vertical groups of tabs. Both the MSDI and MTDI approaches can be created using the .NET Framework as an alternative to the traditional MDI interface, although there is not really any direct support for these newer interfaces. As a result, implementing such interfaces requires much more effort from the developer.

For our purposes, a traditional MDI application provides the means to discuss and demonstrate the manner in which the .NET Framework supports such applications. We will convert the existing MyPhotos application into the MDI application shown in figure 16.3. As you can see, this application will incorporate the Form classes we have created in part 2 of this book.

Figure 16.3 Our multiple document interface, created in this chapter, displays a selected set of photo albums within a single window.

The reuse of our existing classes is possible because of the manner in which the Form class in general and MDI support in particular is integrated into the Windows Forms hierarchy. As we discussed in chapter 7, a Form object is a Control instance that happens to display an application window. For MDI applications, Form controls are contained by a parent Form. Of course, the contained forms can be resized and moved within their container, and can still display menus, toolbars, status bars, and other controls. As we shall see, the relationship between MDI parent and child forms is different than the relationship between control containers and controls.

528

CHAPTER 16 MULTIPLE DOCUMENT INTERFACES

16.1.4SUPPORT IN WINDOWS FORMS

To provide some insight and perhaps some perspective on MDI applications, the following table lists a number of class members specific to the implementation of MDI applications in the .NET Framework. Of course, these members can be used for other purposes, and additional properties, methods, and events are certainly used in MDI applications. These events highlight many of the MDI-specific tasks that are often performed in this style interface. The table provides a short description of each member and a reference to the section in this chapter where more information on each item may be found.

Class members often used in MDI applications

Class

Member

Member name

Description

See

type

section

 

 

 

 

 

 

 

 

 

Properties

ActiveMdiChild

Gets the MDI child window that is currently

16.4.1

 

 

 

active.

 

 

 

IsMdiChild

Gets whether the form is an MDI child.

16.3.2

 

 

IsMdiContainer

Gets whether the form is an MDI container

16.2.1

 

 

 

form.

 

 

 

MdiChildren

Gets the set of MDI children contained by

16.4.3

 

 

 

this form as an array of Form objects.

 

 

 

MdiParent

Gets or sets the MDI container for this form.

16.2.2

Form

 

 

If set, then this form is an MDI child form.

 

 

 

 

 

 

 

MergedMenu

Gets the MainMenu object representing the

16.3

 

 

 

current merged menu for an MDI container

 

 

 

 

form.

 

 

Methods

LayoutMdi

Arranges the MDI children within this form

16.5.1

 

 

 

using a given layout style.

 

 

Events

MdiChildActivate

Occurs when an MDI child form is activated

16.4.4

 

 

 

or deactivated within an MDI application.

 

 

 

 

Note that MDI children do not receive the

 

 

 

 

Activated and Deactivate events.

 

 

 

 

 

 

 

Properties

MdiListItem

Gets the MenuItem object contained by this

16.5.2

 

 

 

menu that displays a list of MDI child forms

 

Menu

 

 

for the associated form object.

 

 

 

 

 

 

Methods

MergeMenu

Merges the MenuItem objects in a given

16.3

 

 

 

menu with those contained by this menu.

 

 

 

 

 

 

 

Properties

MdiList

Gets or sets whether this menu should be

16.5.2

 

 

 

populated with a list of MDI child forms

 

 

 

 

contained by the associated form.

 

MenuItem

 

MergeOrder

Gets or sets the relative position of this menu

16.3.2

 

 

item when it is merged with another menu.

 

 

 

 

 

 

 

MergeType

Gets or sets how this menu should be

16.3.1

 

 

 

merged with other menus. The default is

 

 

 

 

MergeType.Add.

 

 

 

 

 

 

INTERFACE STYLES

529

Also note that the behaviors of desktop-related actions within an MDI child form are modified. For example, the Minimize and Maximize buttons on the title bar work within the parent window, rather than on the desktop itself.

In the rest of this chapter we will enhance our MyPhotos application to support a multiple document interface. We begin with the MDI container form.

16.2MDI FORMS

So let’s convert our existing MyPhotos application into an MDI application. This initial work is not as difficult as you might think. Generally, we need one Form to act as the top-level container, and the ability to create other forms as children within this container. Here, we will do this via the following tasks:

1Create a new parent form for the application to act as the MDI container.

2Add a menu bar and New menu item to create MDI child forms.

3Define a new Main method in the parent as the entry point for the application.

Of course, there will be other work to perform to clean up the behavior of our application. These steps will get us going, and subsequent sections will deal with other required changes. Figure 16.4 shows how our application will look by the end of this section. Note in particular the two File menus. We will address this issue in the next section while discussing Merged Menus.

Figure 16.4 Note the two File menus for this window. The menus from both our ParentForm and MainForm classes appear separately on the menu bar. We will address this in section 16.3.

530

CHAPTER 16 MULTIPLE DOCUMENT INTERFACES

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