home *** CD-ROM | disk | FTP | other *** search
- unit Dgload1;
- { PC Plus sample Delphi program.
- Illustrates a way of letting your application accept
- files dragged from the Windows Explorer, File Manager
- or Desktop.
- Author: Huw Collingbourne
-
- Note: To locate the relevant lines of code, search for
- comments beginning with these characters:
- {!!
-
- }
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls,
- ShellAPI, ExtCtrls; {!! add ShellAPI to the uses section }
-
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- Panel1: TPanel;
- CheckBox1: TCheckBox;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- procedure RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
- procedure DisplayMultipleFileNames(var Msg: Tmsg; var Handled: Boolean);
- procedure DisplaySingleFileName(var Msg: Tmsg; var Handled: Boolean);
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
- procedure TForm1.DisplayMultipleFileNames(var Msg: Tmsg; var Handled: Boolean);
- {!! Iterate through all file names if a multi-file selecetion was dropped }
- const
- BUFFLEN = 255;
- FileIndex : Cardinal = Cardinal(-1); { return a count of dropped files }
- var { $FFFF 16-bit; $FFFFFFFF 32-bit }
- buffer : array[0..BUFFLEN] of char;
- fname : string;
- fnum : word;
- begin
- if Msg.Message = WM_DROPFILES then
- begin
- for fnum := 0 to DragQueryFile(Msg.WParam, FileIndex, NIL, BUFFLEN)-1 do
- begin
- DragQueryFile(Msg.WParam, fnum, buffer, BUFFLEN);
- fname := StrPas(buffer);
- Memo1.Lines.Add(fname);
- end;
- DragFinish(Msg.WParam);
- Handled := True;
- end;
- end;
-
-
- procedure TForm1.DisplaySingleFileName(var Msg: Tmsg; var Handled: Boolean);
- {!! Display a single file name after 1 or more files have been dropped }
- const
- BUFFLEN = 255;
- var
- buffer : array[0..BUFFLEN] of char;
- fname : string;
- begin
- if Msg.Message = WM_DROPFILES then
- begin
- DragQueryFile(Msg.WParam, 0, buffer, BUFFLEN);
- fname := StrPas(buffer);
- DragFinish(Msg.WParam);
- Handled := True;
- Memo1.Lines.Add(fname);
- end;
- end;
-
- procedure TForm1.RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
- {!! This is the method that intercepts Windows messages in this application }
- begin
- if CheckBox1.Checked then
- DisplayMultipleFileNames( Msg, Handled )
- else
- DisplaySingleFileName( Msg, Handled );
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- {!!
- When the form is created, make it able to respond to drag-drop operations
-
- procedure DragAcceptFiles(Wnd: HWnd; Accept: Bool);
- This registers whether a given window accepts dropped files.
- * Wnd Identifies the window registering whether it accepts dropped files.
- * Accept Specifies whether the window specified by the Wnd parameter
- accepts dropped files.
-
- Application.OnMessage := RespondToMessage;
- * Application: TApplication;
- The Application variable declares an instance of your application.
- It has a number of useful methods, properties and events.
- * OnMessage
- An OnMessage event occurs when the application receives a Windows message.
- By creating an OnMessage event handler in your application, you can call
- other handlers that respond to the message. An OnMessage event handler
- lets the application trap a Windows message before Windows itself
- processes it.
- * RespondToMessage
- The name of the method that will be called when the Application
- receives a message.
- }
- begin
- DragAcceptFiles(Form1.Handle, true);
- Application.OnMessage := RespondToMessage;
- end;
-
- end.
-