TZQDragfilesListBox component implementation

xiaoxiao2021-03-06  67

TZQDragfilesListBox component implementation

Here is the source code of the TzqDragfilesListbox component. If you are interested, you can see how I implements the component. The TzqDragfilesListBox component provides the Dragenable property and the overdragfiles event to support the file, and the programmer can control whether the Dragenable property is allowed to allow the file to be dragged. If the Dragenable property is true, the programmer can drag in the onDragfiles event. The file is processed. Such as:

// The following statement is to simply add the file name dragged into the zqdragfileslistbox1 list.

Void __fastcall tform1 :: zqdragfileslistbox1dragfiles (TOBJECT * SENDER,

Tstringlist * filenames)

{

ZqdragfilesListBox1-> items-> addstrings (filenames);

}

Ok, the following is the source program of the component. I have a comment on the more important statements, mainly to make readers easier to understand these code!

// Head file zqdragfileslistbox.h

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ifndef zqdragfileslistboxh

#define zqdragfileslistboxh

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#include

#include

#include

/ / This statement defines the function prototype TDRAGFILESNOTIEEVENT for the onDragfiles event.

TypedEf void __fastcall (__closure * tdragfilesnotifyeevent) (System :: Tobject * Sender, TstringList * filenames);

/ / -------------------------------------------------------------------------------------------- ---------------------------

Class Package TzqdragfilesListbox: Public TListBox

{

Private:

/ / Define the properties that controls if the file is allowed to be dragged

Bool FDRAGENABED;

TDRAGFILESNOTIFYEVENT FDRAGFILES;

Void __fastcall setdragenabled (bool value);

BOOL __FASTCALL getDragenabled ();

Void __fastcall setdragfiles (TDRAGFILESNOTIFYEVENT VALUE);

TDRAGFILESNOTIFYEVENT __FASTCALL GETDRAGFILES ();

Void __fastcall dragfiles (twmdropfiles & msg);

protected:

// Used to handle messages related to file drag

Begin_MESSAGE_MAP

VCL_MESSAGE_HANDLER (WM_Dropfiles, Tw Mdropfiles, Dragfiles)

END_MESSAGE_MAP (TListBox)

PUBLIC:

__fastcall tzqdragfileslistbox (tComponent * Owner);

__publish:

// Define the DrageNabled property and the onDragfiles event __property Bool Dragenabled = {Read = getDragenabled, Write = setdragenabled};

__property tdragfilesNotifyEvent OnDragfiles = {read = getdragfiles, Write = setdragfiles}

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ENDIF

// zqdragfileslistbox component implementation file zqdragfileslistbox.cpp

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#pragma HDRSTOP

#include "zqdragfileslistbox.h"

#pragma package (smart_init)

/ / -------------------------------------------------------------------------------------------- ---------------------------

// ValidcTrcheck IS Used To Assure That The Components Created Do Not Have

// ann of Pure Virtual functions.

//

Static Inline Void ValidcTrcheck (TzqdragfilesListbox *)

{

New tzqdragfileslistbox (null);

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

__fastcall tzqdragfileslistbox :: tzqdragfileslistbox (tcomponent * oowner)

: TListBox (Owner)

{

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Namespace ZqdragfilesListBox

{

/ / Register this component

Void __fastcall package register ()

{

TcomponentClass Classes [1] = {__ClassId (TzqdragfilesListbox)}

RegisterComponents ("ZQComponents", Classes, 0);

}

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

Void __fastcall tzqdragfileslistbox :: setdragenabled (bool value)

{

IF (FDRAGENABLED! = Value) {

FDRAGENABLED = VALUE;

DragacceptFiles (Handle, FDRAGENABLED); // Enter the status of accepting file drag

}

}

Bool __fastcall tzqdragfileslistbox :: getDragenabled ()

{

Return FDRAGENABLED;

}

Void __fastcall tzqdragfileslistbox :: setdragfiles (tdragfilesnotifyevent value)

{

IF (fdragfiles! = value) {fdragfiles = value;

}

}

TDRAGFILESNOTIFYEVENT __FASTCALL TZQDRAGFILSLISTBOX :: getDragfiles ()

{

Return fdragfiles;

}

Void __fastcall tzqdragfileslistbox :: Dragfiles (TWMDROPFILES & MSG)

{

// Todo: Add your source code here

// Number of dragged files

INT Num = DragqueryFile ((HDROP) msg.drop, 0xfffffff, null, 0);

Tstringlist * filenames = new TSTRINGLIST;

For (int i = 0; i

Char buffer [256];

DragQueryFile ((HDROP) msg.drop, i, buffer, sizeof (buffer);

Filenames-> Add (ANSISTRING (BUFFER));

}

// End file drag operation

Dragfinish ((HDROP) msg.drop);

// Call user event code

FDRAGFILES

{

FDRAGFILES (this, filenames);

}

Delete filenames;

}

The above code realizes the drag of the file, and the user can use the component directly! At the same time, there is another two components, TzqdragfilescheckListbox and TzqdragfilePanel components together, because their implementation is the same as that of the component, and I will not introduce if these components are interested in http: // www. Zccfamily.com/zqget/ Website Download the source code for these components!

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

New Post(0)