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

Compile and run this application to see our new context menu in action. The CloneMenu method provides a deep copy, in that it duplicates not only the Image menu item, but its child menu items and all event handlers associated with each menu. Because of our careful construction of the Popup and Click event handlers earlier in the chapter, these handlers work without any changes.

It is important to realize that the MenuItem objects within our context menu are not the same as those under the View menu. In particular, if you manually modify an item (such as the menuStretch menu), it will have no effect on the context menu. This may seem a bit strange to programmers used to managing memory in their application, since there are no pointers or other mechanisms required to track these new MenuItem objects. The references to these objects from the context menu are tracked internally as part of the garbage collection system, leaving us to concentrate on our next subject instead of worrying about memory management.

3.6RECAP

That’s it for menus in .NET. In this chapter we showed how both menu bars and context menus can be created, modified, and handled within the .NET Framework by adding these controls to our MyPhotos application. We looked at submenus, and showed how a single event handler can be used by multiple menu objects.

The shared event handlers we created supported both our menu bar as well as our context menu. The contents of our context menu were cloned, or copied, based on the contents of the top-level View menu so that the behavior and processing of both constructs were identical.

We also examined some C# keywords such as the is, in, and foreach keywords, as well as the bool type. We looked at the Properties window in Visual Studio

.NET in more detail, and used this window to add various events to our program. Future chapters will rely on our knowledge of menus and the C# and Visual Stu-

dio items we have learned here. The next chapter will take us to the bottom of the application window, where the status bar normally resides.

More .NET One resource for menus specifically and .NET in general is the GotDotNet web site at www.gotdotnet.com. This site is currently managed by Microsoft, and bills itself as the “.NET Framework Community Website.”

General information about the .NET Framework can also be found on the Microsoft Developer Network at msdn.microsoft.com. These and other Internet sites with information on .NET are listed in appendix D.

RECAP

101

C H A P T E R

4

 

 

Status bars

4.1The Control class 103

4.2The StatusBar class 105

4.3Status bar panels 110

4.4Owner-drawn panels 118

4.5Recap 125

Most applications stuff a lot of information and features into a single window. Most users do not use all of these features, but there is often a core subset that all users would appreciate having at their fingertips. A status bar is a good place for this type data, as it can provide quick feedback related to the current task or cursor position. My word processor, for example, indicates the current page number, total number of pages, column and line position of the cursor, whether the Insert key has been pressed (which I seem to hit constantly while aiming for the Page Down key), and other information I may want to know at a glance. This helps me keep track of how this book is shaping up, when the Insert key has been pressed, and where these words you are reading will appear on the page.

Status bars can also contain graphical information such as the status of the printer, whether the application is connected to the Internet, and pretty much anything else you can draw or animate.

In this chapter, we will look at status bars in Windows Forms by adding the status bar shown in figure 4.1. As you can see, this status bar contains three areas, called

102

panels. You can place any number of panels on a status bar, or you can use a status bar with no panels and simply display text.

Figure 4.1

Our status bar will include the optional sizing grip graphic at the lower right of the control. A user can click this graphic to resize the form.

4.1THE CONTROL CLASS

Before we venture into the StatusBar class, it is worth looking at the classes behind this and all other Windows Forms controls. In chapter 3 we saw how the Menu class derived from the Object, MarshalByRefObject, and Component classes. The hierarchy for the StatusBar class is shown in figure 4.2.

Figure 4.2 The StatusBar class hierarchy includes the base class for all Windows Forms controls: the Control class.

THE CONTROL CLASS

103

.NET Table 4.1 Control class

The Control class for Windows Forms is a component with a visual representation on the desktop. This class is part of the System.Windows.Forms namespace, and inherits from the System.ComponentModel.Component class. This class encapsulates the standard functionality used by all Windows Forms controls.

 

AllowDrop

Gets or sets whether to allow drag and drop operations in this

 

 

control. Drag and drop operations are discussed in chapter 18.

 

Anchor

Gets or sets the anchor setting for the control. The Dock

 

 

property gets or sets the dock setting for the control.

 

BackColor

Gets or sets the background color of the control.

 

ContextMenu

Gets or sets the context menu for the control.

 

Controls

Gets or sets the collection of controls contained by this

 

 

control.

 

ClientRectangle

Gets the client area of the control. The DisplayRectangle

 

 

property gets the display area.

Public

Cursor

Gets or sets the Cursor to display when the mouse is over

 

the control.

Properties

 

 

 

 

Enabled

Gets or sets whether the control is enabled.

 

Location

Gets or sets the location of the control. The edges are

 

 

available via the Top, Bottom, Left, and Right properties.

 

Parent

Gets or sets the parent of this control.

 

TabIndex

Gets or sets the tab index of the control.

 

TabStop

Gets or sets whether the user can use the Tab key to give the

 

 

focus to the control.

 

Text

Gets or sets the text associated with this control.

 

Visible

Gets or sets whether control is visible. This also affects any

 

 

controls contained by this control.

 

 

 

 

BringToFront

Brings the control to the front of the z-order. A similar

 

 

SendToBack method also exists.

Public

GetNextControl

Returns the next or previous control in the tab order.

 

 

Methods

Invalidate

Invalidates all or part of the control and forces a paint

 

 

 

message to be sent to it.

 

PointToClient

Converts a screen location to client coordinates.

 

 

 

 

Click

Occurs when the control is clicked.

Public

KeyPress

Occurs when a key is pressed while the control has focus.

Events

MouseUp

Occurs when a mouse button is released within the control.

 

 

Paint

Occurs when all or part of the control should be repainted.

 

 

 

104

CHAPTER 4 STATUS BARS

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