Homemade support file drag and drop VCL components

zhaozj2021-02-11  186

Friends who have used Winamp know that WINAMP interface can support file drag and drop. When you want to appreciate a MP3 file, just drag the file to Winamp's window, then let go of the mouse. Then how do we make your own procedure to achieve such functions? We can achieve it by improving the standard components provided by the development tool. The following is taken as an example of the ListBox component in the Delphi environment, let the 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 DragQueryFile () Query DragQueryFile () Query Dragfinish () The basic principle of the resource implementation used when dragging and dropping files is as follows : First call the DragAcceptFiles () function initialization component window so that it allows you to accept file drag and drop, then wait for the WM_DropFiles message (once the user is dragging and dropping the file operation, the component window gets this message), after obtaining the message, DragQueryFile can be used. ) The function query is dragged and dropped, and finally calls the Dragfinish () release source. Because in the VCL class library, the ListBox component, the class name is: TListbox, so we can inherit an built component from TListBox. The new component name is: TDROPFILELISTBOX, which adds an onDropfiles event than the standard TListbox and a DROPENABLED attribute. When DROPENABLED is TRUE, you can accept file drag and drop. After the file drag and drop, the OndropFiles event is thrown, which provides a FileNames parameter to get the user to get the file name. Code components as follows: {TDropFileListBox V1.00 Component} {Copyright (c) 2000.5 by Shen Min, Sunisoft} {Email: sunisoft@21cn.com} {Web: http://www.sunistudio.com} unit DropFileListBox; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ShellApi; // increase ShellApi unit, because the unit files used in the three API function declaration for this type TMyNotifyEvent = procedure (Sender: TObject; Filenames: tstringlist) of object; // The custom event type.

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

New Post(0)