Dragdrop of WPF-image Resource Manager

Source: Internet
Author: User

[Problem]

In winform or WPF development, dragdrop is often used. If you drag a file to a program form, the path of the file is displayed on the text control, and other content controls display the file content, this saves the trouble of entering the file path or opening the file dialog box. In practical applications, we can also see that some audio and video players support playing drag and drop files, Office supports inserting drag and drop files, and so on. In most cases, we focus on dragging and dropping files from outside the program to the program, that isDrag it.

If we want to create a function, we need to drag and drop the content in the program to another program. For example, we need to drag the image control in the program to word, and then paste the image in the image control to word, that isDrag pastHow can this be achieved?

I have tried a variety of ideas, including calling the Windows API to get the temporary file object of the image with the mouse in the Click image control event, but coding is complicated, looking back at dragdrop, we found that it not only supports drag, but also supports drag. So we made a demo of these two functions, a simple image resource manager.

[Program architecture]

First, you need a list to display the image path. Then you need an image to display the preview image. Double-click the path in the list to display the preview image of the image.

When you drag a file from outside the program to the program, determine whether the file format is an image format. If so, copy the file to a folder in the working path of the Program for backup, the image path is displayed in the path list. It should be noted that, since the program must support dragging from itself to other programs, it is also allowed to drag from itself to itself. This needs to be done during design to prevent this situation from occurring, to avoid Operation conflicts.

Finally, when you drag the path of an image from the path list to another application, insert the image or open it in another program.

[Coding front-end interface]

The meeting is too simple and will not be described more.

<Groupbox header = "preview" horizontalalignment = "Left" margin = "402,10, 179" verticalignment = "TOP" Height = "190" width = ""> <image X: name = "imgshow" margin = "0" rendertransformorigin = "0.766, 0.482"/> </groupbox> <groupbox header = "path" horizontalalignment = "Left" margin = "10, 10, 179 "verticalalignment =" TOP "Height =" 387 "width =" "> <ListBox X: name = "lstimage" selectionmode = "single" allowdrop = "true" margin = "2" Drop = "lstimage_drop" selectionchanged = "lstimage_selectionchanged" mousedoubleclick = "lstimage_mousedoubleclick"/> </box>

[Drag the Code]

First, set the allowdrop attribute to true for The ListBox control that supports receiving and dragging, and then set the drop method as follows:

Private void lstimage_drop (Object sender, drageventargs e) {// only supports drag-and-drop if (! E. data. getdatapresent (dataformats. filedrop) {return;} // obtain the drag object string [] files = (string []) E. data. getdata (dataformats. filedrop); // note that, because the program supports both drag and drop, ListBox can also receive files dragged by itself. // to prevent conflicts between mouse clicks and drag, you need to block the files dragged by the program itself. // you need to determine whether the files are dragged from outside the program, that is, whether the image is in the working directory if (files. length> 0 &&! Files [0]. startswith (PATH) & (E. allowedeffects & dragdropeffects. copy) = dragdropeffects. copy) {e. effects = dragdropeffects. copy;} else {e. effects = dragdropeffects. none;} foreach (string file in files) {try {// if the image is dragged in from outside, copy the file to the working directory for backup string destfile = path + system. io. path. getfilename (File); Switch (E. effects) {Case dragdropeffects. copy: file. copy (file, destfile, false); BMI = new bi Tmapimage (New uri (destfile); imgshow. source = BMI; lstimage. items. add (destfile); break; default: break;} catch {MessageBox. show ("this file already exists or a non-image file has been imported! ");}}}

[Drag on encoding]

The selectionchanged method of ListBox is used to determine the selected image. You only need to call the dragdrop. dodragdrop method to complete the drag function, which is very simple and convenient.

Private void lstimage_selectionchanged (Object sender, selectionchangedeventargs e) {If (lstimage. selectedindex>-1) {// only use the ListBox radio function string [] files = new string [1]; files [0] = lstimage. selecteditem. tostring (); dragdrop. dodragdrop (lstimage, new dataobject (dataformats. filedrop, files), dragdropeffects. copy | dragdropeffects. move/* | dragdropeffects. link */);}}

The dodragdrop method parameters are described as follows:

Dragsource
Type: system. Windows. dependencyobject
References the dependency object of the dragged data source.
Data
Type: system.

Object
The data object that contains the data being dragged.

Allowedeffects
Type: system. Windows.

Dragdropeffects
Specifies the effect of a drag-and-drop operation.
Dragdropeffects
Value.

[Improvement]

The above are just the basic functions of drag-and-drop, and some other functions are not completed. Here we will only introduce the following features:

  • Click the clipboard on the right to copy the clipboard, paste it, and click "refresh" to open a file. You can use the assumer.exe command here.
  • In the dragover event, determine whether the file comes from outside the program in advance, set the effects of drageventargs based on the judgment result, and then perform copy, move, link, or none operations based on the effects in the drop event.
  • Drag the image control to insert an image or open it in another application.

[Source code]

Http://files.cnblogs.com/wurang/ImageExplorer.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.