Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Manning - Windows Forms Programming With CSharp.pdf
Скачиваний:
72
Добавлен:
24.05.2014
Размер:
14.98 Mб
Скачать
Picture-

The slide show form is now fully integrated into our main application. Compile and run to see this window. Load an album and select the Slide Show menu to display the new dialog.

TRY IT! Throughout the book we have used the photo album and photograph abstractions we constructed in chapter 5 to represent and display images. In the MyPhotos application we display photographs in a Panel control, while in our other applications we use a PictureBox control. In both cases we were forced to override the Paint event in order to draw a photograph with the proper aspect ratio. It would be nice to have a control that provided this functionality directly.

Try creating a new PhotoBox class based on the Windows Forms PictureBox control that adds a new SizeMode setting called ScaleImage to the control. When set to this value, this new control should display the entire image with the proper aspect ratio within the control, just as we have done throughout the book. You can replace the existing Box.SizeMode property using a new set of enumeration values by defining the property in the following manner. You will also need to override the OnPaint and OnResize methods to properly draw an image within the new control.

private PhotoBoxSizeMode _sizeMode; public new PhotoBoxSizeMode SizeMode

{

get { return _sizeMode; } set { _sizeMode = value; }

}

Use your new control in place of the PictureBox control in the SlideShowForm window. My implementation of this control is available on the book’s web site. Also included on the site are the instructions for making this new control, referred to as a custom control, available in the Toolbox window of Visual Studio .NET.

18.3DRAG AND DROP

Continuing with our whirlwind tour of topics, let’s take a quick look at drag and drop. This refers to dragging an object from one location to another, and can occur within an application or between applications. Typically, a drag and drop operation is begun by clicking an object with the mouse pointer, holding down the mouse button while moving, or dragging, the object to a new location; and dropping the object at the new location by releasing the mouse button.

This topic can get fairly complicated, so we will show a rather basic example supporting the following types of drag and drop operations.

Dragging a file from the Windows file system into a PhotoAlbum in a MainForm window.

618

CHAPTER 18 ODDS AND ENDS .NET

Dragging a photograph file from the MainForm window to an external Windows location.

Dragging the photograph caption from the MainForm window to a text editor.

Dragging a photograph file from one PhotoAlbum to another within the MyPhotos MDI application.

The Windows Forms Control class provides direct support for drag and drop operations. The following table summarizes these members.

Members of the Control class related to drag and drop

Public Properties

AllowDrop

Gets or sets whether the control will permit drag and drop

 

operations within its boundaries. The default is false.

 

 

 

 

 

Public Methods

DoDragDrop

Initiates a drag and drop operation from within this control.

 

Typically, this is called from a MouseDown event handler.

 

 

 

 

 

 

DragDrop

Occurs when the user completes a drag and drop operation

 

 

within this control.

 

DragEnter

Occurs when an object is dragged into the control’s

Public Events

 

boundaries.

DragLeave

Occurs when an object formerly dragged into the control is

 

 

 

dragged out of the control’s boundaries.

 

DragOver

Occurs when an object within the control is moved within

 

 

the control’s boundaries.

 

 

 

At a high level, a drag and drop operation performs the following steps. These steps are illustrated by the code in the subsequent sections. Note that the source and target of the operation may be within the same application or in separate applications.

1A source control initiates drag and drop, typically within a MouseDown event handler, using the DoDragDrop method. One or more data objects and associated formats are provided as part of invoking this method.

2The user drags the object to a target control that has its AllowDrop property set to true.

3As the mouse enters the target control, the DragEnter event occurs to permit the target to identify whether the data can be recognized by this control. This permits the operating system to display an appropriate mouse cursor for the user.

4If so, then the DragOver event occurs as the user moves the drag and drop object within the control.

5If the object is dragged out of the control, the DragLeave event occurs.

6If the user releases the object within the target control, then the DragDrop event occurs to permit the control to receive the data.

7The result of the operation is returned by the DoDragDrop method in the original source control.

DRAG AND DROP

619

DragDrop-

We will divide our example into two sections. First, we will begin a drag and drop operation from within the PictureBox control of our MainForm class. Next, we will receive external drag and drop operations within this same control.

18.3.1INITIATING DRAG AND DROP

The key to beginning a drag and drop operation is the DoDragDrop method. This method defines the data for the operation and the kind of operation permitted.

public DragDropEffects DoDragDrop(object data,

DragDropEffects allowedEffects);

While the data parameter can be any data, the DataObject class provides a standard mechanism for safely transferring data between applications. The

Effects enumeration permits different types of drag and drop operations to be supported. For example, the Move, Copy, and Link values permit an object to be moved, copied, or linked from the original data source to the drop target.

The DoDragDrop method does not return until the drag and drop operation is completed. The return value indicates what effect was performed by the operation. The QueryContinueDrag event in the Control class can be used to keep tabs on the operation. This event occurs periodically during drag and drop and can be used to cancel the operation or to modify the application window as required.

In our application, we will simply begin the operation and let the .NET Framework take care of the rest. We will provide two types of data formats using the DataObject class. The first will be the FileDrop format recognized by the Windows file system and applications such as Microsoft Paint. The second will be the Text format recognized by most word processors.

The following table details the changes required.

Set the version number of the MyPhotos application to 18.3.

BEGIN A DRAG AND DROP OPERATION

 

Action

Result

 

 

 

1

Locate the MouseDown event

private void pnlPhoto_MouseDown

 

handler for the Panel control in

(object sender,

 

the MainForm.cs code window.

System.Windows.Forms.MouseEventArgs e)

 

{

 

 

 

 

 

2

If the Ctrl key is not held down,

if (ctrlKeyHeld)

 

then retrieve the current

{

 

photograph for the album.

. . .

 

}

 

 

 

 

else

 

 

{

 

 

// Initiate drag and drop for this image

 

 

Photograph photo = _album.CurrentPhoto;

 

 

 

620

CHAPTER 18 ODDS AND ENDS .NET

BEGIN A DRAG AND DROP OPERATION (continued)

 

Action

Result

 

 

 

3

If this Photograph is found,

if (photo != null)

 

create a FileDrop data format for

{

 

dragging the photograph to a new

// Create object for encapsulating data

 

DataObject data = new DataObject();

 

location.

 

 

 

How-to

// Construct string array for FileDrop

 

string[] fileArray = new string[1];

 

a. Construct a DataObject

 

fileArray[0] = photo.FileName;

 

instance to hold the data for-

data.SetData(DataFormats.FileDrop,

 

mats.

fileArray);

 

b. Construct a string array to

Note: The DataFormats class encapsulates vari-

 

hold the associated file.

ous data formats that can be used by drag and

 

c. Associate the string array with

drop operations. The FileDrop format used here

 

requires a string array as the data type. This per-

 

the FileDrop format for the

 

mits multiple files to be provided at once.

 

data.

 

 

 

 

 

4

Also assign a Text format using

// Use the caption for the text format

 

the Caption property of the

data.SetData(DataFormats.Text,

 

photograph as the associated

photo.Caption);

 

 

 

data.

 

 

 

 

5

Call the DoDragDrop method with

// Initiate drag and drop

 

the constructed data object to

pnlPhoto.DoDragDrop(data,

 

initiate a drag and drop Copy

DragDropEffects.Copy);

 

}

 

operation.

 

}

 

 

}

 

 

 

This code begins a drag and drop operation that can be received by any other application running on the computer. Other applications look at the provided data formats to identify whether they can accept the dragged data. We will look at how to do this in Windows Forms in a moment.

Of course, for applications that can receive multiple formats, the result they receive depends on which format they prefer. Most word processing applications look for the Text format first, and will therefore receive the Caption property of our photo, rather than the associated file object.

Compile and run the application. Display an album and click on the image. Hold the mouse and drag it to a new location to perform a drag and drop operation. Figure 18.3 shows the result of dragging one of our favorite images from the MyPhotos application into a Microsoft Paint application. The Paint application opens the given file and displays a copy of the image in its main window. Also try dragging an image into WordPad or some other word processor to see how the caption string appears.

DRAG AND DROP

621

Figure 18.3 The FileDrop format used here to drag an image into Microsoft Paint is a common method for transferring files between applications.

This completes our example for initiating a drag and drop operation. The next topic is to handle drag and drop operations within the MainForm window.

18.3.2RECEIVING DRAG AND DROP

Regardless of where a drag and drop operation originates, an application can elect to handle the incoming data. The DragEnter and DragDrop events are used to receive such operations. Event handlers for both of these events receive a DragEventArgs object as their event parameter. A summary of this object appears in .NET Table 18.4.

622

CHAPTER 18 ODDS AND ENDS .NET