How to interact with the resource manager to cut/copy/paste files

Source: Internet
Author: User
I. This article will introduce the solution to the following two problems:
1. Cut/copy files in the Resource Manager (Windows Explorer), and Program Paste (paste;
2. You can cut or copy files in your own applications and paste the files in resource management.

Ii. In this articleCodeCompile tools and test environment:
1, vc6.0, platform SDK (no need for MFC );
2. Windows 2000.

Iii. Overview
We know that in windows, we can share and transmit data through the clipboard (Clipboard). For example, in Windows Explorer, we can cut, copy, and paste files. Similarly, we can use clipboard in our own applications to improve the interoperability between our own applications and Windows operating systems. But how can we share and transmit data with applications such as resource manager? The method provided in this article is to use some data structures and APIs provided by windows to share and transmit data through the clipboard.

IV. Implementation Method
First, Windows does not write the file name to the clipboard when cutting/copying a file, but adds a draganddrop file object to the clipboard, A status value is written to identify the operation type (moving/copying, cutting is actually moving. If you do not paste it after cutting, the file will still exist and will not be deleted ). Based on this knowledge, we will first look at how the application recognizes the cut/copy actions of Windows Resource Manager.

Before using the clipboard, open it first:

Bool openclipboard (hwnd); The hwnd parameter is the window handle for opening the clipboard. True is returned for success, and false is returned for failure.

Then, you can use getclipboarddata to obtain the data in the clipboard:

 
Handle getclipboarddata (uint uformat );

Uformat is the format of the required data. For example, the format of the drag-and-drop object in this document is cf_hddrop. It indicates that the data format of this drag-and-drop object type (move/copy) is not a Windows Standard Clipboard data structure, but a simple DWORD pointer. We can use the following statement to register the Data Type:

 
Uint udropeffect = registerclipboardformat ("preferred dropeffect ");

The returned udropeffect is the code that will be substituted into the data structure of the getclipboarddata function,
The getclipboarddata function returns a handle, which is only a work of windows for uniformity. We can convert it into a corresponding data form as needed. For example, our udropeffect is a DWORD pointer.
As I have mentioned before, a drag-and-drop object is placed in the clipboard, so we can get this object through the following statement:

 
Hdrop = hdrop (getclipboarddata (cf_hdrop ));

If an hdrop object exists, we should obtain the udropeffect data so that we can process the following files:

 
DWORD dweffect = * (DWORD *) (getclipboarddata (udropeffcet )));

For the meaning of this value, we only need to include the "oleidl. H" header file. In this header file, five States are defined. This article only focuses on:

# Definedropeffect_copy (1) # definedropeffect_move (2)

Therefore, we can

 
If (dweffect & dropeffect_copy) copyfile (...); else (dweffect & dropeffect_move) movefile (...);

To complete the cut/copy operation.
After obtaining the udropeffect state, we need to get the file list. To get the list of files in the drag-and-drop object, we can use dragqueryfile to achieve this:

 
Uint dragqueryfile (hdrop, uint ifile, lptstr lpszfile, uint CCH );

The second parameter is the file serial number. You can set ifile to-1 to get the number of files.
Finally, we provide a complete example:

 # include 
  
    # include 
   
     .... uint udropeffect = forward ("preferred dropeffect"); If (openclipboard (hwnd) {hdrop = hdrop (getclipboarddata (cf_hdrop); If (hdrop) {DWORD dweffect, * dw; DW = (DWORD *) (getclipboarddata (udropeffect); If (DW = NULL) dweffect = dropeffect_copy; elsedweffect = * dw; char Buf [4096]; buf [0] = 0; uint cfiles = dragqueryfile (hdrop, (uint)-1, null, 0); point; char szfile [max_path]; for (uint COUNT = 0; count 
    
   
  

In this example, I did not perform file operations, but simply displayed a message box. In actual use, the movefile and copyfile functions are required. This article will not discuss them.
After we know how to identify the cut/copy actions of other programs, we know the data structure of this operation very well, to enable other programs to recognize our cut/copy operations is actually the process of putting the above data structure into the clipboard.
In our example, the data placed in the clipboard must be a memory object: hglobal. This object can be generated through globalalloc. Then, use globallock to get the memory address of the object and write data into it. In fact, in Win32, because the process has independent memory space, globallock is no longer required for regular memory allocation. In msdn, we can see that this function is mainly used for DDE and clipboard services.
Based on the previous knowledge, to let other programs identify our cut/copy actions, we must put two pieces of data in the clipboard. Now let's prepare data for dropeffect, we also need to register the data format first:

 
Udropeffect = registerclipboardformat ("preferred dropeffect ");

Then allocate the memory object and get the pointer:

 
Hgbleffect = globalalloc (gmem_zeroinit | gmem_moveable | gmem_ddeshare, sizeof (DWORD); dwdropeffect = (DWORD *) globallock (hgbleffect );

Note that the gmem_moveable flag must be used for the data to be placed in the clipboard. Finally, we can set the data and unlock it:

 
If (copy) * dwdropeffect = dropeffect_copy; else * dwdropeffect = dropeffect_move; globalunlock (hgbleffect );

In this way, I have prepared the data for dropeffect. After a while, we will put the data to the clipboard together with the drag-and-drop object of the file. The method for creating a file drag-and-drop object is basically the same as that of dropeffect, but the file drag-and-drop object has a special data structure, which is not as simple as dropeffect. The data structure of this object is as follows:

 
+ -------------------------- + | Dropfiles | files list | + ---------------------------- +

Dropfiles is the header data of the drag-and-drop object. This structure is defined in shlobj. h:

 
Typedef struct _ dropfiles {DWORD pfiles; point pt; bool FNC; bool fwide;} dropfiles, far * lpdropfiles;

The pfiles pointer is the offset of the file list (the files list item in) referenced by the object's first address. Generally, this value is equal to the length of the dropfiles structure (I have not seen any exceptions). PT indicates the coordinate of the drag and drop position of the file. In this example, we ignore it as 0; FNC indicates whether the PT value is the client zone coordinate (false indicates the screen coordinate); fwide indicates whether the files list contains Unicode. as Chinese, we must set it to true. The dropfiles structure is followed by the files list. The files list is a set of wide strings separated by 0. For example, "File 1 \ 0 file 2 \ 0 ..."
We can use the multibytetowidechar function to convert a regular string to a wide string. The following code generates a drag-and-drop object:

Udropfileslen = sizeof (dropfiles); dropfiles. pfiles = udropfileslen; dropfiles.pt. X = 0; dropfiles.pt. y = 0; dropfiles. FNC = false; dropfiles. fwide = true; ugbllen = udropfileslen + ubuflen <1 + 8; // ubuflen is the length of the character Transfer Group of the file name. to convert it to a wide character, therefore, the length must be 2 hgblfiles = globalalloc (gmem_zeroinit | gmem_moveable | gmem_ddeshare, ugbllen); szdata = (char *) globallock (hgblfiles); memcpy (szdata, (lpvoid) (& dropfiles), udropfileslen); // copy dropfiles to the header szfilelist = szdata + udropfileslen; // obtain the first address of the List of stored files (cp_acp, mb_composite, lpbuffer, ubuflen, (wchar *) szfilelist, ubuflen); globalunlock (hgblfiles );

Now we can put the above two groups of data into the clipboard. Note that the clipboard should be cleared before writing data. For ease of use, the following is an independent function that implements this function:

 
Void cutorcopyfiles (char * lpbuffer, uint ubuflen, bool bcopy)

Lpbuffer is a buffer that includes the names of all files to be cut/copied; ubuflen is the length of lpbuffer; bcopy determines whether the operation is copy or cut, true indicates copy, and false indicates cut. For example, we can call this function as follows:

 
Char szfiles [] = "C: \ 1.txt \ 0C: \ 2.txt \ 0"; cutorcopyfiles (szfiles, sizeof (szfiles), false );

To cut the file, or use:

 
Cutorcopyfiles (szfiles, sizeof (szfiles), true );

To copy files.

 
# Include <shellapi. h> # include <shlobj. h> # include <oleidl. h>
...... Void cutorcopyfiles (char * lpbuffer, uint ubuflen, bool bcopy) {uint udropeffect; dropfiles; uint ugbllen, batch; hglobal hgblfiles, hgbleffect; char * szdata, * szfilelist; DWORD * dwdropeffect; udropeffect = regular ("preferred dropeffect"); hgbleffect = globalalloc (gmem_zeroinit | large, sizeof (DWORD); dwdropeffect = (DWORD *) globallock (hgbleffect); If (bcopy) * dwdropeffect = dropeffect_copy; else * dwdropeffect = dropeffect_move; globalunlock (hgbleffect); expiration = sizeof (dropfiles); dropfiles. pfiles = udropfileslen; dropfiles.pt. X = 0; dropfiles.pt. y = 0; dropfiles. FNC = false; dropfiles. fwide = true; ugbllen = udropfileslen + ubuflen * 2 + 8; hgblfiles = globalalloc (gmem_zeroinit | gmem_moveable | disable, ugbllen); szdata = (char *) globallock (hgblfiles ); memcpy (szdata, (lpvoid) (& dropfiles), records); szfilelist = szdata + records; average (cp_acp, mb_composite, lpbuffer, ubuflen, (wchar *) szfilelist, ubuflen ); globalunlock (hgblfiles); If (openclipboard (null) {values (); values (cf_hdrop, hgblfiles); setclipboarddata (udropeffect, hgbleffect); closeclipboard ();}}

I hope the above content will help you.
This document is attached with a demo project. After compilation, the cutcopy.exe program is generated. The program is used as follows:
after the program is started, you can use a program such as Windows Resource Manager to cut/copy files, and click [checkclipboard] in the program. the demo program will analyze the content in the clipboard, and pop up a message box to tell whether it is copy files or cut files, and provide a list of files. after you click [OK] to close the message box, the file list will be placed in the text box. You can click [CUT]/[copy] to change the attributes of the clipboard.
at the same time, you can use [browser] to select several files to the text box, and then click [CUT]/[copy] to perform the operation. Then, you can use [checkclipboard] to check the content in the clipboard or paste (paste) in programs such as Windows Resource Manager to check whether the clipboard is correct.

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.