A simple example of using Delphi to enable window support for file drag and drop, with source code

Source: Internet
Author: User

A simple method to enable Windows Shell file drag-and-drop is to use Windows API: dragacceptfiles, and then use the VCL message function overload mechanism of Delphi to process wm_dropfiles messages and call dragqueryfile.

 

Dragacceptfiles Function

--------------------------------------------------------------------------------

Registers whether a window accepts dropped files.

Syntax

Void dragacceptfiles (hwnd,
Bool faccept
);
Parameters

Hwnd
Identifier of the window that is registering whether it will accept dropped files.
Faccept
Value that indicates if the window identified by the hwnd parameter accepts dropped files. This value is true to accept dropped files or false to discontinue accepting dropped files.
Return Value

No return value.

Remarks

An application that calldragacceptfiles with the faccept parameter set to true has identified itself as able to process the wm_dropfiles message from File Manager.

Function Information

Minimum dll version shell32.dll version 4.0 or later
Custom implementation no
Header shellapi. h
Import library shell32.lib
Minimum Operating Systems Windows NT 3.1, Windows 95

 

Dragqueryfile Function

--------------------------------------------------------------------------------

Retrieves the names of dropped files that result from a successful drag-and-drop operation.

Syntax

Uint dragqueryfile (hdrop,
Uint ifile,
Lptstr lpszfile,
Uint CCH
);
Parameters

Hdrop
Identifier of the structure containing the file names of the dropped files.
Ifile
Index of the file to query. if the value of the ifile parameter is 0 xffffffff, dragqueryfile returns a count of the files dropped. if the value of the ifile parameter is between zero and the total number of files dropped, dragqueryfile copies the file name with the corresponding value to the buffer pointed to by the lpszfile parameter.
Lpszfile
Address of a buffer to receive the file name of a dropped file when the function returns. this file name is a null-terminated string. if this parameter is null, dragqueryfile returns the required size, in characters, of the buffer.
CCH
Size, in characters, of the lpszfile buffer.
Return Value

When the function copies a file name to the buffer, the return value is a count of the characters copied, not including the terminating null character.

If the index value is 0 xffffffff, the return value is a count of the dropped files. Note that the index variable itself returns unchanged, and will therefore remain 0xffffffff.

If the index value is between zero and the total number of dropped files and the lpszfile buffer address is null, the return value is the required size, in characters, of the buffer, not including the terminating null character.

Windows 95/98/me: dragqueryfile is supported by the Microsoft layer for Unicode. to use this, you must add certain files to your application, as outlined in Microsoft layer for Unicode on Windows ME/98/95 systems.

 

Function Information

Minimum dll version shell32.dll version 4.0 or later
Custom implementation no
Header shellapi. h
Import library shell32.lib
Minimum Operating Systems Windows NT 3.1, Windows 95
Unicode implemented as ANSI and Unicode versions.

 

 Unit Fm_dropfiles;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, shellapi, shlobj, extctrls, stdctrls, ActiveX;

Type
Tfmdropfiles = Class (Tform)
Dropfilelist: tlistbox;
Procedure Formcreate (Sender: tobject );
Private
Procedure Wmdropfiles ( VaR MSG: twmdropfiles ); Message Wm_dropfiles;
Public
{ Public declarations }
End ;

VaR
Fmdropfiles: tfmdropfiles;

Implementation

{ $ R *. DFM }

Procedure Tfmdropfiles. formcreate (Sender: tobject );
Begin
Dragacceptfiles (handle, true );
End ;

Procedure Tfmdropfiles. wmdropfiles ( VaR MSG: twmdropfiles );
VaR
Dropfilename: String ;
Dropcount: integer;
I: integer;
Begin
Inherited ;
Setlength (dropfilename, max_path );
Dropcount: = dragqueryfile (msg. Drop, $ ffffffff, Nil , 0 );
For I: = 0 To Dropcount-1 Do
Begin
Dragqueryfile (msg. drop, I, pchar (dropfilename), max_path );
Dropfilelist. Items. Add (dropfilename );
End ;
Dragfinish (msg. Drop );
End ;

End .

 

Download:Http://www.ctdisk.com/file/5490633

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.