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

.NET Table 18.4 DragEventArgs class

The DragEventArgs class represents the event arguments required for drag and drop events, namely the DragEnter, DragOver, and DragDrop events in the Control class. This class is part of the System.Windows.Forms namespace, and inherits from the System.EventArgs class.

 

AllowedEffect

Gets which drag and drop operations are permitted

 

 

by the source of the drag event.

 

Data

Gets the IDataObject interface that holds the data

 

 

and data formats associated with the event.

 

Effect

Gets or sets the DragDropEffects enumeration

 

 

values indicating which drag and drop operations are

Public Properties

 

permitted in the target of the drag event.

 

 

 

KeyState

Gets the current state of the Shift, Ctrl, and Alt

 

 

keyboard keys.

 

X

Gets the x-coordinate of the current mouse pointer

 

 

position.

 

Y

Gets the y-coordinate of the current mouse pointer

 

 

position.

 

 

 

For our example, we will recognize the FileDrop format in the MainForm window to receive files dragged from the file system or from other MainForm windows.

The steps required are detailed in the following table:

HANDLE DRAG AND DROP IN THE MAINFORM WINDOW

 

Action

Result

 

 

 

1

In the MainForm.cs [Design] window,

Drop operations are now permitted in the panel

 

set the AllowDrop property on the

control.

 

Panel control to true.

 

 

 

 

2

Add a DragEnter event handler for

private void pnlPhoto_DragEnter

 

the panel.

(object sender,

 

 

System.Windows.Forms.DragEventArgs e)

 

 

{

 

 

 

3

If the data associated with the event

if (e.Data.GetDataPresent(

 

supports the FileDrop data format,

DataFormats.FileDrop))

 

then indicate that this control will

e.Effect = DragDropEffects.Copy;

 

 

 

support the Copy drag and drop

 

 

effect.

 

 

How-to

 

 

Use the GetDataPresent method

 

 

from the IDataObject interface.

 

 

 

 

DRAG AND DROP

623

HANDLE DRAG AND DROP IN THE MAINFORM WINDOW (continued)

 

Action

Result

 

 

 

4

Otherwise, indicate that the current

else

 

drag and drop data is not accepted by

e.Effect = DragDropEffects.None;

 

this control.

}

 

 

 

 

 

5

Add a DragDrop event handler for the

private void pnlPhoto_DragDrop

 

panel.

(object sender,

 

 

System.Windows.Forms.DragEventArgs e)

 

 

{

 

 

 

6

In this handler:

object obj = e.Data.GetData(

 

a. Retrieve the data in FileDrop for-

DataFormats.FileDrop);

 

Array files = obj as Array;

 

mat associated with the event.

int index = -1;

 

b. Convert this data to an Array

 

foreach (object o in files)

 

instance.

 

{

 

c. For each object in the array, con-

string s = o as string;

 

vert the object to a string.

 

 

 

 

7

If a string is found, then:

if (s != null)

 

a. Create a new Photograph object

{

 

Photograph photo

 

using this string.

= new Photograph(s);

 

b. See if the Photograph is already in

// Add the file (if not present)

 

the current album.

 

index = _album.IndexOf(photo);

 

c. If not, then add the new photo to

if (index < 0)

 

the album.

{

 

index = _album.Add(photo);

 

Note: Recall that the Photograph

 

_bAlbumChanged = true;

 

object will simply display a bad

}

 

image bitmap if an invalid or non-

}

 

}

 

image file name is provided.

 

 

 

 

8

If a Photograph was found in the

if (index >= 0)

 

foreach loop, then

{

 

a. Adjust the current album position

// Show the last image added

 

_album.CurrentPosition = index;

 

to the discovered index.

Invalidate();

 

b. Invalidate the form to redraw the

}

 

}

 

window.

 

 

 

 

This completes our handling of drag and drop. Compile and run the program to see this in action. Display two different albums in separate MainForm windows. You should be able to perform the following drag and drop operations to obtain the described results:

Find a new image file in Windows Explorer. Drag this file into one of the album windows. The image is added to the album and displayed in the window.

Find an image file in Windows Explorer that is already in an album. Drag this file into the album. The existing Photograph object is displayed in the window.

624

CHAPTER 18 ODDS AND ENDS .NET

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