home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue146 / Delphi / CopyDlph.exe / lpunit.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-08-06  |  5.6 KB  |  152 lines

  1. unit Lpunit;
  2. {
  3.   PC Plus sample Delphi application.
  4.  
  5.   A simple drag-and-drop-enabled application 'launch pad'.
  6.  
  7.   Usage: Select 1 or more files in the Windows Explorer and drag/drop
  8.   them onto the launch pad. Those that contain an icon will be shown as
  9.   an iconic button, those without an icon will be shown as a plain labelled
  10.   button. Once installed, a button can be clicked to launch the
  11.   associated application.
  12.  
  13.   Limitations: No error checking, does not handle a case when more buttons
  14.   are added than will fit into the panel, no ability to save and restore
  15.   the panel state, cannot load document files with the icons of their associated
  16.   application.
  17.  
  18.   Compatibility: Delphi 1 and above
  19.   Author: Huw Collingbourne
  20. }
  21.  
  22. interface
  23.  
  24. uses
  25.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  26.   Forms, Dialogs,
  27.   ShellAPI, StdCtrls, Buttons, ExtCtrls; {!! must use the ShellAPI unit    }
  28.                                          {!! BitBtn needs the Buttons unit }
  29.  
  30. type
  31.   TForm1 = class(TForm)
  32.     Memo1: TMemo;
  33.     ButtonPanel: TPanel;
  34.     procedure FormCreate(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.     ButtonList : TStringList;    {!! This StringList holds our BitBtns      }
  38.     procedure RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
  39.     procedure BtnClick(Sender: TObject);
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. const
  45.   BUFFLEN = 255;
  46. type
  47.   CHARARRAY = array[0..BUFFLEN] of char;
  48. var
  49.   Form1: TForm1;
  50.   Icon : TIcon; {!! An icon to put onto a button } 
  51.  
  52. implementation
  53. {$R *.DFM}
  54.  
  55. function ExecuteFile(const FileName, Dir : string ) : THandle;
  56. {!! A simple interface to the API's ShellExecute function that opens
  57.     the application specified by FileName }
  58. var
  59.   ntFileName, ntDir : CHARARRAY;
  60. begin
  61.   Result := ShellExecute(Application.MainForm.Handle,
  62.   nil,      { nil here equates to the default command,'Open' }
  63.   StrPCopy(ntFileName, FileName),
  64.   nil,
  65.   StrPCopy(ntDir,Dir),
  66.   SW_SHOW);
  67. end;
  68.  
  69. procedure TForm1.RespondToMessage(var Msg: Tmsg; var Handled: Boolean);
  70. { Iterate through all file names if a multi-file selection was dropped }
  71. const
  72.   FileIndex : Cardinal = Cardinal(-1);   { return a count of dropped files }
  73. var                                      { $FFFF 16-bit;  $FFFFFFFF 32-bit }
  74.   buffer : CHARARRAY;
  75.   fname : string;
  76.   fnum  : word;
  77. begin
  78.    if Msg.Message = WM_DROPFILES then
  79.    begin
  80.       for fnum := 0 to DragQueryFile(Msg.WParam, FileIndex, NIL, BUFFLEN)-1 do
  81.       begin
  82.          DragQueryFile(Msg.WParam, fnum, buffer, BUFFLEN);
  83.          fname  := StrPas(buffer);
  84.          Memo1.Lines.Add(fname);
  85.       {!!===================================================================== }
  86.       {!!=== This is the code that installs BitBtn(s) for a dropped filname(s) }
  87.       {!!===================================================================== }
  88.          Icon.Handle := ExtractIcon(HInstance, buffer, 0);  {!! get icon       }
  89.          ButtonList.AddObject(fname, TBitBtn.Create(Self));{!! add Btn to list}
  90.          with TBitBtn(ButtonList.Objects[ButtonList.Count-1]) do
  91.          begin
  92.               with Glyph do     { size the Glyph (picture area) of the BitBtn  }
  93.               begin
  94.                 width := 32;
  95.                 height := 32;
  96.                if Icon.Handle <> 0 then { if we've found an icon put it on Btn }
  97.                    Canvas.Draw(0,0,Icon);
  98.               end;
  99.                                { set the size, font etc. of the BitBtn itself  }
  100.               width := 100;
  101.               layout := blGlyphTop;
  102.               font.name := 'Arial';
  103.               font.size := 8;
  104.               caption := ExtractFileName(fname);
  105.               Align := alLeft;
  106.               Parent := ButtonPanel;    {!! put the BitBtn onto the panel      }
  107.               OnClick := BtnClick;      {!! set BitBtn's OnClick event-handler }
  108.          end;
  109.       {!!===================================================================== }
  110.       end;
  111.     DragFinish(Msg.WParam);
  112.     Handled := True;
  113.    end;
  114. end;
  115.  
  116. procedure TForm1.FormCreate(Sender: TObject);
  117. begin
  118.   {!! make this form drag-friendly }
  119.   DragAcceptFiles(Form1.Handle, true);
  120.   Application.OnMessage := RespondToMessage;
  121.   ButtonList := TStringList.Create; {!! Create StringList to store our BitBtns }
  122. end;
  123.  
  124. procedure TForm1.BtnClick(Sender: TObject);
  125. {!! The BitBtns' OnClick event-handler                                         }
  126. {!! The Sender parameter indicates which button on the form was clicked.
  127.     We locate the position of the button in our ButtonList.
  128.     ButtonList is a StringList containing a String (the executable file's path)
  129.     alongside each button. We are able to use pass this string (and also to
  130.     parse out the directory part of the string) to a function that launches the
  131.     application.
  132. }
  133. var
  134.    obindex : integer;
  135.    filename, filepath : string;
  136. begin
  137.    obindex := ButtonList.IndexOfObject(Sender);{ get pos of Btn in ButtonList  }
  138.    filename := ButtonList.Strings[obindex];    { get string in list at same pos}
  139.    filepath := ExtractFilePath(filename);      { extract the path from string  }
  140.    Memo1.Lines.Add('Object at index: ' + IntToStr(obindex) +
  141.                                      ', LAUNCHING:  ' + filename);
  142.    Memo1.Lines.Add('Default directory is: ' + filepath );
  143.    ExecuteFile(filename, filepath);           { execute the file specified     }
  144.  
  145. (* --- this is the more terse version of the code ----
  146.    filename := ButtonList.Strings[ButtonList.IndexOfObject(Sender)];
  147.    ExecuteFile(filename, ExtractFilePath(filename));
  148.    -------------------------------------------------- *)
  149. end;
  150.  
  151. end.
  152.