home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / Bonus / Plasmatech / ptscp_examples.exe / %MAINDIR% / Examples / NewFileEdit / Delphi / FMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-08-31  |  3.1 KB  |  108 lines

  1. unit FMain; // Copyright ⌐ 1998 Plasmatech Software Design.
  2. {
  3.   Shell Control Pack Examples
  4.   NewFileEdit
  5.  
  6.   Creates a new file, makes the new list item visible and starts editing the name.
  7.  
  8.   The MakeNewFile sets things up so the work can be done in the OnInsert event handler.
  9. }
  10.  
  11. interface
  12.  
  13. uses
  14.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  15.   ComCtrls, UPTTreeList, UPTShellControls, StdCtrls;
  16.                                  
  17. type
  18.   TfrmMain = class(TForm)
  19.     PTShellList1: TPTShellList;
  20.     btnCreate: TButton;
  21.     Memo1: TMemo;
  22.     procedure PTShellList1Insert(Sender: TObject; Item: TListItem);
  23.     procedure btnCreateClick(Sender: TObject);
  24.   private
  25.     FCreatingFile: Boolean;
  26.     FNewFileName: String;
  27.     procedure MakeNewFile;
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   frmMain: TfrmMain;
  34.  
  35. implementation
  36. uses
  37.   UPTShellUtils;
  38.  
  39. {$R *.DFM}
  40. var
  41.   FileNumber: Integer;
  42.  
  43. {
  44.  This procedure creates the file and sets up state information for the OnInsert event handler.
  45. }
  46. procedure TfrmMain.MakeNewFile;
  47. begin
  48.   try
  49.     PTShellList1.Options := PTShellList1.Options - [ptsloDynamicRefresh];
  50.       {We will handle the refreshing ourselves, so turn off automatic mode for now.}
  51.  
  52.     FCreatingFile := true;
  53.       {This flag is checked in the OnInsert event handler.}
  54.  
  55.     Inc(FileNumber);
  56.       {Give the file a unique number.}
  57.  
  58.     FNewFileName := UPTShellUtils.EnsureTrailingCharDB(PTShellList1.Folder.PathName,'\') +
  59.                     'NewFile'+IntToStr(FileNumber);
  60.       {EnsureTrailingCharDB is a double-byte safe function that makes sure the last character
  61.        in the string is the passed character '\' in this case. Most folders don't end with a '\',
  62.        but root drive folders (c:\) do. Using this function we can get the path name into a
  63.        consistent format to which we can append the file name.}
  64.  
  65.     TFileStream.Create(FNewFileName, fmCreate or fmOpenWrite or fmShareDenyNone).Free;
  66.       {Create the new file stream, free it straight away. This will create an empty file.}
  67.  
  68.     if IsWin95 and (PTShellList1.Folder.IdList = nil) then
  69.       Sleep(2000);
  70.       {Under Windows 95 (and variants) the desktop virtual folder is very slow in registering the
  71.        presence of the new file.}
  72.  
  73.     PTShellList1.RefreshItems;
  74.       {Before this returns, the OnInsert item will be called 1..n times.}
  75.   finally
  76.     FCreatingFile := false;
  77.     PTShellList1.Options := PTShellList1.Options + [ptsloDynamicRefresh];
  78.   end;
  79. end;
  80.  
  81. {
  82.  This event handler makes the example work. It checks that the new item is the one we created,
  83.  makes it visible and starts editing.
  84. }
  85. procedure TfrmMain.PTShellList1Insert(Sender: TObject; Item: TListItem);
  86. begin
  87.   if FCreatingFile and
  88.     (AnsiCompareFileName(PTShellList1.GetDataFromItem(Item).PathName, FNewFileName)=0) then
  89.   begin
  90.     Item.MakeVisible(false);
  91.       {Make the item fully visible.}
  92.  
  93.     Item.EditCaption;
  94.  
  95.     FCreatingFile := false;
  96.       {Since we only added one file, we don't need to check any more.}
  97.   end;
  98. end;
  99.  
  100. procedure TfrmMain.btnCreateClick(Sender: TObject);
  101. begin
  102.   MakeNewFile;
  103. end;
  104.  
  105. end.
  106.  
  107.  
  108.