Homemade support file drag and drop VCL components

zhaozj2021-02-08  205

Friends who have used Winamp know that Winamp's interface can support file drag and drop. When you want to appreciate a MP3 file, you only need

Drag the file onto the Winamp window, then let go of the mouse. Then how do we make your own procedures to achieve such functions

? We can achieve it by improving the standard components provided by the development tool. The following is the ListBox component in the Delphi environment.

For example, let Listbox support file drag and drop.

First introduce the API function you want to use:

DragAcceptFiles () initializes a window to allow / forbidden to accept file drag and drop

DragQueryFile () query the file name drag and drop

Dragfinish () uses resources when dragging and dropping files

The basic principles of implementation are as follows: First call the DragAcceptFiles () function initialization component window so that it allows you to accept files.

Drag and drop, wait WM_DROPFILES messages (once the user is dragging and dropping file operation, the component window can get this message),

After getting the message, you can use the DragQueryFile () function to query the file name dragged and dropped, and finally call Dragfinish () release.

source.

Because in the VCL class library, the ListBox component, the class name is: TListBox, so we can inherit from TListBox

Your own components. The new component is named: TDROPFILELISTBOX, which adds an onDropfiles event than the standard TListbox and

A DROPENABLED attribute. When the DROPENABLED is TRUE, you can accept file drag and drop, and the file is thrown after the file is dropped.

OnDropfiles event, the event provides a filenames parameter to get the user get the file name.

The code of the component is as follows:

{TDROPFILASTBOX V1.00 Component}

{CopyRight (C) 2000.5 by Shen Min, SuniSoft}

{Email: suneisoft@21cn.com}

{Web: http://www.sunistudio.com}

Unit DropfileListbox;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs,

STDCTRLS, Shellapi; // Increase the shellapi unit, as the three API functions used are declared in this unit file

Type

TMYNOTIFYEVENT = Procedure (Sender: TOBJECT; FileNames: Tstringlist) of object; //

Righteous event type.

TDROPFILASTBOX = Class (TListBox) // New class inherits from TListBox

Private

{Private Declarations}

Fenabled: Boolean; // Internal variable of DROPENABLED

protected

FDROPFILE: TMYNOTIFYEVENT; / / Event pointer

Procedure Dropfiles (VAR MES: TMESSAGE); Message WM_Dropfiles;

Procedure FDROPENABED (Enabled: Boolean); / / Set the process of setting the dropenabled property

{Protected Declarations}

public

Constructor Create (Aowner: Tcomponent); OVERRIDE;

DESTRUCTOR DESTROY; OVERRIDE; {public Declarations}

Published

Property OnDropfiles: TMYNOTIFYEVENT READ FDROPFILE WRITE FDROPFILE

Property Dropenabled: Boolean Read Fenabled Write FDROPENABLED;

{Published Declarations}

END;

PROCEDURE register;

IMPLEMENTATION

PROCEDURE register;

Begin

Registercomponents ('SuniSoft', [TDROPFILELISTBOX]); // Registering components to the component board

END;

Constructor TDropfileListBox.create (Aowner: Tcomponent);

Begin

Inherited Create (Aowner);

Fenabled: = true; // When the class is constructed, the default value of DROPENABELD is True

END;

Destructor TDROPFILISTBOX.DESTROY;

Begin

Inherited destroy;

END;

/ / Change the calling process of the attribute DROPENABLED

Procedure TDROPFILANELISTBOX.FDROPENABED (Enabled: boolean);

Begin

Fenabled: = enabled;

DragacceptFiles (Self.handle, Enabled); / / Set whether the component window accepts the file drag and drop

END;

// Process of receiving WM_Dropfiles messages

Procedure TDROPFILASTBOX.DROPFILES (VAR MES: TMESSAGE);

VAR FN: TSTRINGLIST;

Filename: array [1..256] of char;

SFN: STRING;

I, count, p: integer;

Begin

Fn: = tstringlist.create;

Count: = DragQueryFile (Mes.WParam, $ fffffff, @ filename, 256); // Get the number of drag and drop files

For i: = 0 to count-1 do

Begin

Dragqueryfile (Mes.WParam, I, @ filename, 256); // Query file name

SFN: = filename;

P: = POS (chr (0), sfn); / / Remove the ASCII code at the end of the file name 0 characters

SFN: = COPY (SFN, 1, P-1);

Fn.Add (SFN);

END;

Dragfinish (Mes.wParam); // Release the resources used

IF assigned (fdropfile) THEN

FDROPFILE (SELF, FN); // Call the event and return the file name list parameters

Fn.free;

END;

End.

After the component is installed, we can create a new project, place the component on the Form, then

Write a specific processing drag and drop operation in the onDropfiles event of the TDROPFILISTBOX component.

For example, add all drag and drop files to the list, add code as follows:

Procedure TFORM1.DROPFILELISTBOX1DROPFILES (Sender: Tobject; FileNames: Tstringlist);

Begin

DROPFILELISTBOX1.ITEMS.Addstrings (filenames);

END;

Run a look, is it interesting? This article only has an extension for the ListBox component, you can also make similar extensions to other components, and implement the drag and drop of support files.

转载请注明原文地址:https://www.9cbs.com/read-2069.html

New Post(0)