home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue145 / Delphi / CopyDelp.exe / DragLoad / dgload1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-07-13  |  3.7 KB  |  122 lines

  1. unit Dgload1;
  2. { PC Plus sample Delphi program.
  3.   Illustrates a way of letting your application accept
  4.   files dragged from the Windows Explorer, File Manager
  5.   or Desktop.
  6.   Author: Huw Collingbourne
  7.  
  8.   Note: To locate the relevant lines of code, search for
  9.   comments beginning with these characters:
  10.            {!!
  11.  
  12. }
  13. interface
  14.  
  15. uses
  16.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  17.   Forms, Dialogs, StdCtrls,
  18.   ShellAPI, ExtCtrls; {!! add ShellAPI to the uses section }
  19.  
  20. type
  21.   TForm1 = class(TForm)
  22.     Memo1: TMemo;
  23.     Panel1: TPanel;
  24.     CheckBox1: TCheckBox;
  25.     procedure FormCreate(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.      procedure RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
  29.      procedure DisplayMultipleFileNames(var Msg: Tmsg; var Handled: Boolean);
  30.      procedure DisplaySingleFileName(var Msg: Tmsg; var Handled: Boolean);
  31.   public
  32.     { Public declarations }
  33.   end;
  34.  
  35. var
  36.   Form1: TForm1;
  37.  
  38. implementation
  39.  
  40. {$R *.DFM}
  41. procedure TForm1.DisplayMultipleFileNames(var Msg: Tmsg; var Handled: Boolean);
  42. {!! Iterate through all file names if a multi-file selecetion was dropped }
  43. const
  44.   BUFFLEN = 255;
  45.   FileIndex : Cardinal = Cardinal(-1);   { return a count of dropped files }
  46. var                                      { $FFFF 16-bit;  $FFFFFFFF 32-bit }
  47.   buffer : array[0..BUFFLEN] of char;
  48.   fname : string;
  49.   fnum  : word;
  50. begin
  51.    if Msg.Message = WM_DROPFILES then
  52.    begin
  53.       for fnum := 0 to DragQueryFile(Msg.WParam, FileIndex, NIL, BUFFLEN)-1 do
  54.       begin
  55.          DragQueryFile(Msg.WParam, fnum, buffer, BUFFLEN);
  56.          fname  := StrPas(buffer);
  57.          Memo1.Lines.Add(fname);
  58.       end;
  59.     DragFinish(Msg.WParam);
  60.     Handled := True;
  61.    end;
  62. end;
  63.  
  64.  
  65. procedure TForm1.DisplaySingleFileName(var Msg: Tmsg; var Handled: Boolean);
  66. {!! Display a single file name after 1 or more files have been dropped }
  67. const
  68.   BUFFLEN = 255;
  69. var
  70.   buffer : array[0..BUFFLEN] of char;
  71.   fname : string;
  72. begin
  73.    if Msg.Message = WM_DROPFILES then
  74.    begin
  75.       DragQueryFile(Msg.WParam, 0, buffer, BUFFLEN);
  76.       fname  := StrPas(buffer);
  77.       DragFinish(Msg.WParam);
  78.       Handled := True;
  79.       Memo1.Lines.Add(fname);
  80.    end;
  81. end;
  82.  
  83. procedure TForm1.RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
  84. {!! This is the method that intercepts Windows messages in this application }
  85. begin
  86.   if CheckBox1.Checked then
  87.      DisplayMultipleFileNames( Msg, Handled )
  88.   else
  89.      DisplaySingleFileName( Msg, Handled );
  90. end;
  91.  
  92. procedure TForm1.FormCreate(Sender: TObject);
  93. {!!
  94. When the form is created, make it able to respond to drag-drop operations
  95.  
  96. procedure DragAcceptFiles(Wnd: HWnd; Accept: Bool);
  97.    This registers whether a given window accepts dropped files.
  98.    * Wnd      Identifies the window registering whether it accepts dropped files.
  99.    * Accept   Specifies whether the window specified by the Wnd parameter
  100.               accepts dropped files.
  101.  
  102. Application.OnMessage := RespondToMessage;
  103.    * Application: TApplication;
  104.        The Application variable declares an instance of your application.
  105.        It has a number of useful methods, properties and events.
  106.    * OnMessage
  107.        An OnMessage event occurs when the application receives a Windows message.
  108.        By creating an OnMessage event handler in your application, you can call
  109.        other handlers that respond to the message. An OnMessage event handler
  110.        lets the application trap a Windows message before Windows itself
  111.        processes it.
  112.    * RespondToMessage
  113.        The name of the method that will be called when the Application
  114.        receives a message.
  115. }
  116. begin
  117.   DragAcceptFiles(Form1.Handle, true);
  118.   Application.OnMessage := RespondToMessage;
  119. end;
  120.  
  121. end.
  122.