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

CREATE THE PIXELDLG CLASS (continued)

Action

Result

5Add a Button object to the base of the form and set its properties.

Settings

Property Value

(Name) btnClose

Text &Close

The code generated here is very similar to the code we saw earlier in this chapter for the CaptionDlg class, so we will not look at this code in detail. Instead, we will move on to the internal class members required by this new form.

8.4.2ADDING CLASS MEMBERS TO PIXELDLG

There really isn’t a lot of work to do here. We need to allow our main application to modify the display values, and make sure the dialog exits when the Close button is clicked. We will use properties for the display values, and handle the Click event to close the form.

You may recall that we did not handle any events for our CaptionDlg form. Since this was a modal dialog, we took advantage of the DialogResult property in the Button class. When the corresponding button is clicked and a modal dialog is displayed via the ShowDialog method, this property closes the form and returns the assigned result to the caller. Here, such a scheme is not possible since we are creating a modeless dialog. Thus our need for a Click event handler.

The steps here are similar to what we have done before, so let’s get to it.

ADD THE REQUIRED PIXELDLG CLASS MEMBERS

 

Action

Result

 

 

 

1

In the PixelDlg.cs [Design]

protected void btnClose_Click

 

window, add a Click handler

(object sender, System.EventArgs e)

 

for the Close button.

{

 

 

 

 

 

2

Implement this method to

Close();

 

close the form.

}

 

 

 

3

Add an XVal property to set

public int XVal

 

the value for the X label.

{

 

 

set { lblXVal.Text = value.ToString(); }

 

 

}

 

 

 

MODELESS DIALOGS

255

ADD THE REQUIRED PIXELDLG CLASS MEMBERS (continued)

 

Action

Result

 

 

 

4

Add a YVal property to set the

public int YVal

 

value for the Y label.

{

 

 

set { lblYVal.Text = value.ToString(); }

 

 

}

 

 

 

5

Add RedVal, GreenVal, and

public int RedVal

 

BlueVal properties for their

{

 

respective labels.

set { lblRedVal.Text = value.ToString(); }

 

}

 

 

 

 

public int GreenVal

 

 

{

 

 

set { lblGreenVal.Text = value.ToString(); }

 

 

}

 

 

public int BlueVal

 

 

{

 

 

set { lblBlueVal.Text = value.ToString(); }

 

 

}

 

 

 

This ensures our dialog can be closed, and provides the properties necessary to update the labels from our main form. Note how the Form.Close method is used to close the form, just like in the Exit menu handler for our main application window. The

.NET framework keeps track of which form is the top-level application window, so the Close method here closes just the PixelDlg window and not the entire application. As you’ll recall, this method disposes of any nonmemory resources allocated by the form as well.

One other change we should make is to allow the standard keyboard shortcuts to close the dialog. Since there is a single button on our form, we will support both the Enter and Esc keys for this purpose. Continuing the previous steps:

SUPPORT KEYBOARD SHORTCUTS TO CLOSE PIXELDLG FORM

Action

Result

6In the PixelDlg.cs [Design] window, display the properties for the PixelDlg form.

7Set both the AcceptButton The properties are set in the InitializeComponent property and the CancelButton method of the PixelDlg.cs source file.

property to btnClose.

Our PixelDlg form is ready to go. Next we need to invoke this form from the main window.

8.4.3DISPLAYING THE MODELESS PIXELDLG FORM

For our CaptionDlg form, we displayed it as a modal dialog box using the Form.ShowDialog method. This method displays the form and waits until it exits,

256

CHAPTER 8 DIALOG BOXES

preventing the parent form from accepting any external input until this occurs. For a modeless dialog a different method is required that will allow the parent form to continue execution.

The Form.Show method is used for this purpose. The Show method is inherited from the Control class and sets a control’s Visible property to true. For a Form, this means it displays in a modeless fashion. The Show method is a void method since no immediate result is returned.

As for our modal dialog, we will display the form from an item on the menu bar.

ADD PIXEL DATA MENU TO INVOKE THE PIXELDLG FORM

 

 

Action

Result

 

 

 

 

 

 

1

Display the View menu in the

 

 

MainForm.cs [Design] window.

 

 

 

 

 

 

 

2

Add a separator at the end of the menu.

 

 

 

 

 

 

 

3

Add a Pixel Data menu item.

 

 

 

Settings

 

 

 

 

 

 

 

 

 

Property

Value

 

 

 

(Name)

menuPixelData

 

 

 

Text

Pi&xel Data…

 

 

 

 

 

 

 

Before we use this to display the dialog, let’s ponder what support we need for our new dialog. Since this is a modeless dialog, it will display while the main form is displayed. So the user may change which photo is displayed, or modify the display mode used. Such changes will require that we modify what is displayed in the dialog.

To facilitate this, we will track whether the dialog is currently displayed, and which photo is currently represented by the dialog. Let’s continue the previous steps and add these class members.

ADD CLASS MEMBERS TO TRACK PIXELDLG SETTINGS

 

Action

Result

 

 

 

4

In the MainForm.cs

private PixelDlg _dlgPixel = null;

 

window, add a private

 

 

member to hold the

 

 

PixelDlg form object.

 

 

 

 

5

Also add an integer to hold

private int _nPixelDlgIndex;

 

the current photo

 

 

represented in this form.

 

 

 

 

MODELESS DIALOGS

257

_nPixelDlgIndex

These members will be used to update the dialog as the main window changes. In particular, we can use these members to create the dialog in the menu handler.

IMPLEMENT MENUPIXELDATA_CLICK EVENT HANDLER

 

Action

Result

 

 

 

6

Add a click handler for the

protected void menuPixelData_Click

 

Pixel Data menu.

(object sender, System.EventArgs e)

 

 

{

 

 

 

7

If the dialog has not been

if (_dlgPixel == null || _dlgPixel.IsDisposed)

 

created or the existing

{

 

dialog has been disposed,

_dlgPixel = new PixelDlg();

 

_dlgPixel.Owner = this;

 

create a new dialog.

 

}

 

 

Note: The Owner property used here ensures that the

 

 

PixelDlg form is minimized and maximized along with

 

 

the parent form.

 

 

 

8

Assign the initial data to

_nPixelDlgIndex = _album.CurrentPosition;

 

display in the dialog.

Point p = pnlPhoto.PointToClient(

 

 

Form.MousePosition);

 

 

UpdatePixelData(p.X, p.Y);

 

 

 

9

Finally, display the dialog.

_dlgPixel.Show();

 

 

}

 

 

 

The code to create and display the dialog should seem familiar, but what about that code in step 8 of our task. Let’s talk about it.

The first line in step 8 simply assigns the current photo index to the variable. No problem there.

_nPixelDlgIndex = _album.CurrentIndex;

The next line converts the current screen coordinates of the mouse pointer to its coordinates within in the main Panel object. This uses the static Form.MousePosition property to retrieve the screen coordinates of the mouse pointer as a Point instance. This point contains the current X and Y position of the pointer on the screen in pixels.

The location on the screen is not what we need. We need to know the location of the mouse pointer within the main Panel object. That is, in the pnlPhoto control. This can then be used to calculate what part of the image is at that location.

The PointToClient method does this conversion. It accepts a point in screen coordinates and returns the same point in client coordinates. If the given point happens to be outside the control, the returned Point will contain values outside the display area of the control.

Point p = pnlPhoto.PointToClient(Form.MousePosition);

The final line calls an as-yet-undefined UpdatePixelData method. We will write this method in the next section to accept the current position of the mouse pointer in Panel coordinates and fill in the appropriate values of the PixelDlg form.

UpdatePixelData(p.X, p.Y);

258

CHAPTER 8 DIALOG BOXES

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