home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / ctenari / Hadraba / Source / MainForm.pas < prev    next >
Pascal/Delphi Source File  |  2002-09-02  |  12KB  |  412 lines

  1. unit MainForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ExtCtrls, ComCtrls, StdCtrls, Buttons, ActnList, Menus, ImgList,
  8.   IniFiles;
  9.  
  10. type
  11.   TMainForm1 = class(TForm)
  12.     MainPanel1: TPanel;
  13.     Image1: TImage;
  14.     ItemsListBox1: TListBox;
  15.     ExecModeGroupBox1: TGroupBox;
  16.     Label1: TLabel;
  17.     AssocSpeedButton1: TSpeedButton;
  18.     UserSpeedButton1: TSpeedButton;
  19.     ExitSpeedButton1: TSpeedButton;
  20.     SortedCheckBox1: TCheckBox;
  21.     Bevel1: TBevel;
  22.     DescriptionStaticText1: TStaticText;
  23.     ActionList1: TActionList;
  24.     ExitAction1: TAction;
  25.     AssocAction1: TAction;
  26.     UserAppAction1: TAction;
  27.     SortAction1: TAction;
  28.     MinimizeAction1: TAction;
  29.     SystemPopupMenu1: TPopupMenu;
  30.     Iconify1: TMenuItem;
  31.     N1: TMenuItem;
  32.     Exit1: TMenuItem;
  33.     ExecIconCheckBox1: TCheckBox;
  34.     ExecIconiAction1: TAction;
  35.     ExecCloseCheckBox1: TCheckBox;
  36.     ExecCloseAction1: TAction;
  37.     ViewPopupMenu1: TPopupMenu;
  38.     Standardassociation1: TMenuItem;
  39.     Userapplication1: TMenuItem;
  40.     N2: TMenuItem;
  41.     Sorted1: TMenuItem;
  42.     N3: TMenuItem;
  43.     Iconifyafterexecute1: TMenuItem;
  44.     Closeafterexecute1: TMenuItem;
  45.     AboutAction1: TAction;
  46.     AboutSpeedButton1: TSpeedButton;
  47.     InitItemsListBox1: TListBox;
  48.     ImageList1: TImageList;
  49.     DisplayImage1: TImage;
  50.     Timer1: TTimer;
  51.     About1: TMenuItem;
  52.     SpeedButton1: TSpeedButton;
  53.     ExploreAction1: TAction;
  54.     Exploreselecteditem1: TMenuItem;
  55.     SpeedButton2: TSpeedButton;
  56.     ExploreCDAction1: TAction;
  57.     N4: TMenuItem;
  58.     ExploreCD1: TMenuItem;
  59.     ShortcutsAction1: TAction;
  60.     N5: TMenuItem;
  61.     Keyboardshortcuts1: TMenuItem;
  62.     Keyboardshortcuts2: TMenuItem;
  63.     N6: TMenuItem;
  64.     SystemMenu1: TMenuItem;
  65.     ExploreCD2: TMenuItem;
  66.     N7: TMenuItem;
  67.     Keyboardshortcuts3: TMenuItem;
  68.     About2: TMenuItem;
  69.     N8: TMenuItem;
  70.     Iconify2: TMenuItem;
  71.     Exit2: TMenuItem;
  72.     OpenDialog1: TOpenDialog;
  73.     CmdListBox1: TListBox;
  74.     Bevel2: TBevel;
  75.     CDNamePanel1: TPanel;
  76.     CDNameLabel1: TLabel;
  77.     SpeedButton3: TSpeedButton;
  78.     procedure OnDeviceChange(var Msg : TMsg); message WM_DeviceChange;
  79.     procedure OnMoveOnScreen(Sender : TObject; Button : TMouseButton;
  80.       Shift : TShiftState; X, Y : Integer);
  81.     procedure ExitAction1Execute(Sender: TObject);
  82.     procedure MinimizeAction1Execute(Sender: TObject);
  83.     procedure AboutAction1Execute(Sender: TObject);
  84.     procedure SortAction1Execute(Sender: TObject);
  85.     procedure Timer1Timer(Sender: TObject);
  86.     procedure FormCreate(Sender: TObject);
  87.     procedure ShortcutsAction1Execute(Sender: TObject);
  88.     procedure ExploreCDAction1Execute(Sender: TObject);
  89.     procedure FormDestroy(Sender: TObject);
  90.     procedure ExecIconiAction1Execute(Sender: TObject);
  91.     procedure ExecCloseAction1Execute(Sender: TObject);
  92.     procedure AssocAction1Execute(Sender: TObject);
  93.     procedure UserAppAction1Execute(Sender: TObject);
  94.     procedure ExploreAction1Execute(Sender: TObject);
  95.     procedure FormShow(Sender: TObject);
  96.     procedure ItemsListBox1Click(Sender: TObject);
  97.     procedure ItemsListBox1KeyPress(Sender: TObject; var Key: Char);
  98.   private
  99.     DisplayedIcon : Byte;
  100.     IniFile       : TIniFile;
  101.   public
  102.     { Public declarations }
  103.   end;
  104.  
  105. var
  106.   MainForm1   : TMainForm1;
  107.   CDDrive     : String;
  108.   IniFileName : String;
  109.   CDVName     : String;
  110.   CDVSerial   : Cardinal;
  111.  
  112. procedure FillInfo(var AVolumeName : String; var AVolumeSerial : Cardinal);
  113.  
  114. implementation
  115.  
  116. uses ShellAPI, AboutForm, ShortCutForm, ErrorForm, EventForm;
  117.  
  118. {$R *.DFM}
  119.  
  120. var
  121.   RootPathName       : PChar    = nil;
  122.   VolumeNameBuffer   : PChar    = nil;
  123.   VolumeNameSize     : Cardinal = 0;
  124.   VolumeSerialNumber : Cardinal = 0;
  125.   MaxComponentLength : Cardinal = 0;
  126.   FileSystemFlags    : Cardinal = 0;
  127.   FileSystemName     : PChar    = nil;
  128.   FileSystemNameSize : Cardinal = 0;
  129.  
  130. procedure FillInfo(var AVolumeName : String; var AVolumeSerial : Cardinal);
  131. begin
  132.   GetMem(RootPathName, 512);
  133.   StrPCopy(RootPathName, CDDrive + '\');
  134.   GetMem(VolumeNameBuffer, 512);
  135.   VolumeNameSize     := 512;
  136.   GetMem(FileSystemName, 512);
  137.   FileSystemNameSize := 512;
  138.   GetVolumeInformation(RootPathName, VolumeNameBuffer,
  139.     VolumeNameSize, @VolumeSerialNumber,
  140.     MaxComponentLength, FileSystemFlags,
  141.     FileSystemName, FileSystemNameSize);
  142.   AVolumeName        := VolumeNameBuffer;
  143.   AVolumeSerial      := VolumeSerialNumber;
  144.   FreeMem(FileSystemName, 512);
  145.   FreeMem(VolumeNameBuffer, 512);
  146.   FreeMem(RootPathName, 512);
  147. end;
  148.  
  149. procedure TMainForm1.OnDeviceChange(var Msg : TMsg);
  150. var
  151.   NewVolumeName   : String;
  152.   NewVolumeSerial : Cardinal;
  153. begin
  154.   FillInfo(NewVolumeName, NewVolumeSerial);
  155.   If (NewVolumeName <> CDVName) or (NewVolumeSerial <> CDVSerial) then
  156.     Application.Terminate;
  157. end;
  158.  
  159. procedure TMainForm1.OnMoveOnScreen(Sender : TObject; Button : TMouseButton;
  160.   Shift : TShiftState; X, Y : Integer);
  161. var
  162.   Point : TPoint;
  163. begin
  164.   If Button = mbLeft then
  165.     begin
  166.     Point.x := X;
  167.     Point.y := Y;
  168.     ReleaseCapture;
  169.     SendMessage(Handle, wm_NCLButtonDown, htCaption, Integer(@Point));
  170.     end;
  171. end;
  172.  
  173. procedure TMainForm1.ExitAction1Execute(Sender: TObject);
  174. begin
  175.   Close;
  176. end;
  177.  
  178. procedure TMainForm1.MinimizeAction1Execute(Sender: TObject);
  179. begin
  180.   Application.Minimize;
  181. end;
  182.  
  183. procedure TMainForm1.AboutAction1Execute(Sender: TObject);
  184. begin
  185.   AboutForm1.ShowModal;
  186. end;
  187.  
  188. procedure TMainForm1.SortAction1Execute(Sender: TObject);
  189. begin
  190.   If not ItemsListBox1.Sorted then
  191.     begin
  192.     ItemsListBox1.Sorted := True;
  193.     SortAction1.Checked  := True;
  194.     end
  195.   else
  196.     begin
  197.     ItemsListBox1.Sorted := False;
  198.     SortAction1.Checked  := False;
  199.     ItemsListBox1.Clear;
  200.     ItemsListBox1.Items.AddStrings(InitItemsListBox1.Items);
  201.     end;
  202.   If ItemsListBox1.Items.Count > 0 then
  203.     ItemsListBox1.ItemIndex := 0;
  204. end;
  205.  
  206. procedure TMainForm1.Timer1Timer(Sender: TObject);
  207. begin
  208.   case DisplayedIcon of
  209.     0:
  210.       begin
  211.       ImageList1.GetIcon(1, DisplayImage1.Picture.Icon);
  212.       DisplayedIcon := 1;
  213.       end;
  214.     1:
  215.       begin
  216.       ImageList1.GetIcon(2, DisplayImage1.Picture.Icon);
  217.       DisplayedIcon := 2;
  218.       end;
  219.     2:
  220.       begin
  221.       ImageList1.GetIcon(0, DisplayImage1.Picture.Icon);
  222.       DisplayedIcon := 0;
  223.       end;
  224.   end;
  225. end;
  226.  
  227. procedure TMainForm1.FormCreate(Sender: TObject);
  228. var
  229.   i : Cardinal;
  230.   S : String;
  231. begin
  232.   DisplayedIcon        := 2;
  233.   IniFile              := TIniFile.Create(IniFileName);
  234.   CDNameLabel1.Caption := IniFile.ReadString('global',
  235.     'CDName', 'AutoRun Version 1.0');
  236.   Application.Title    := CDNameLabel1.Caption;
  237.   Caption              := CDNameLabel1.Caption;
  238.   If CDNameLabel1.Width > (CDNamePanel1.ClientWidth - 10) then
  239.     begin
  240.     repeat
  241.       CDNameLabel1.Font.Size := CDNameLabel1.Font.Size - 1;
  242.     until CDNameLabel1.Width < (CDNamePanel1.ClientWidth - 10);
  243.     CDNameLabel1.Align := alClient;
  244.     end
  245.   else
  246.     begin
  247.     repeat
  248.       CDNameLabel1.Font.Size := CDNameLabel1.Font.Size + 1;
  249.     until CDNameLabel1.Height > CDNamePanel1.ClientHeight;
  250.     CDNameLabel1.Align := alClient;
  251.     end;
  252.   i := 1;
  253.   while IniFile.ReadString('items', 'Item' + IntToStr(i), '') <> '' do
  254.     begin
  255.     If (IniFile.ReadString('items',
  256.       'Item' + IntToStr(i) + 'Command', '') <> '') and
  257.       (InitItemsListBox1.Items.IndexOf(IniFile.ReadString('items',
  258.       'Item' + IntToStr(i), '')) = -1) then
  259.       begin
  260.       InitItemsListBox1.Items.Add(IniFile.ReadString('items',
  261.         'Item' + IntToStr(i), ''));
  262.       S := IniFile.ReadString('items', 'Item' + IntToStr(i) + 'Command', '');
  263.       If ExtractFileDrive(S) <> '' then
  264.         Delete(S, 1, 2);
  265.       If S[1] <> '\' then
  266.         S := '\' + S;
  267.       CmdListBox1.Items.Add(S);
  268.       end;
  269.     Inc(i);
  270.     end;
  271.   ItemsListBox1.Items.AddStrings(InitItemsListBox1.Items);
  272.   If IniFile.ReadInteger('options', 'SortItems', 0) = 1 then
  273.     begin
  274.     SortAction1.Checked := True;
  275.     SortAction1Execute(Sender);
  276.     end;
  277.   If IniFile.ReadInteger('options', 'IconifyAfterExec', 0) = 1 then
  278.     ExecIconiAction1.Checked := True;
  279.   If IniFile.ReadInteger('options', 'CloseAfterExec', 0) = 1 then
  280.     ExecCloseAction1.Checked := True;
  281. end;
  282.  
  283. procedure TMainForm1.ShortcutsAction1Execute(Sender: TObject);
  284. begin
  285.   ShortCutForm1.ShowModal;
  286. end;
  287.  
  288. procedure TMainForm1.ExploreCDAction1Execute(Sender: TObject);
  289. begin
  290.   ShellExecute(0, 'explore', PChar(CDDrive + '\'), '', '', SW_Normal);
  291. end;
  292.  
  293. procedure TMainForm1.FormDestroy(Sender: TObject);
  294. begin
  295.   IniFile.Free;
  296. end;
  297.  
  298. procedure TMainForm1.ExecIconiAction1Execute(Sender: TObject);
  299. begin
  300.   ExecIconiAction1.Checked := not ExecIconiAction1.Checked;
  301. end;
  302.  
  303. procedure TMainForm1.ExecCloseAction1Execute(Sender: TObject);
  304. begin
  305.   ExecCloseAction1.Checked := not ExecCloseAction1.Checked;
  306. end;
  307.  
  308. procedure TMainForm1.AssocAction1Execute(Sender: TObject);
  309. var
  310.   S : String;
  311. begin
  312.   S := ItemsListBox1.Items[ItemsListBox1.ItemIndex];
  313.   If InitItemsListBox1.Items.IndexOf(S) = -1 then
  314.     begin
  315.     ErrorForm1.ShowModal;
  316.     Close;
  317.     Exit;
  318.     end;
  319.   If ShellExecute(0, 'open', PChar(CDDrive +
  320.     CmdListBox1.Items[InitItemsListBox1.Items.IndexOf(S)]),
  321.     '', '', SW_Normal) <= 32 then
  322.     begin
  323.     EventForm1.Execute(ARE_CantExec);
  324.     Exit;
  325.     end;
  326.   If ExecCloseAction1.Checked then
  327.     begin
  328.     Close;
  329.     Exit;
  330.     end;
  331.   If ExecIconiAction1.Checked then
  332.     Application.Minimize;
  333. end;
  334.  
  335. procedure TMainForm1.UserAppAction1Execute(Sender: TObject);
  336. var
  337.   S : String;
  338. begin
  339.   S := ItemsListBox1.Items[ItemsListBox1.ItemIndex];
  340.   If InitItemsListBox1.Items.IndexOf(S) = -1 then
  341.     begin
  342.     ErrorForm1.ShowModal;
  343.     Close;
  344.     Exit;
  345.     end;
  346.   If not OpenDialog1.Execute then
  347.     Exit;
  348.   If ShellExecute(0, 'open', PChar(OpenDialog1.FileName), PChar(CDDrive +
  349.     CmdListBox1.Items[InitItemsListBox1.Items.IndexOf(S)]),
  350.     '', SW_Normal) <= 32 then
  351.     begin
  352.     EventForm1.Execute(ARE_CantExec);
  353.     Exit;
  354.     end;
  355.   If ExecCloseAction1.Checked then
  356.     begin
  357.     Close;
  358.     Exit;
  359.     end;
  360.   If ExecIconiAction1.Checked then
  361.     Application.Minimize;
  362. end;
  363.  
  364. procedure TMainForm1.ExploreAction1Execute(Sender: TObject);
  365. var
  366.   S : String;
  367. begin
  368.   S := ItemsListBox1.Items[ItemsListBox1.ItemIndex];
  369.   If InitItemsListBox1.Items.IndexOf(S) = -1 then
  370.     begin
  371.     ErrorForm1.ShowModal;
  372.     Close;
  373.     Exit;
  374.     end;
  375.   If ShellExecute(0, 'explore', PChar(ExtractFilePath(CDDrive +
  376.     CmdListBox1.Items[InitItemsListBox1.Items.IndexOf(S)])),
  377.     '', '', SW_Normal) <= 32 then
  378.     begin
  379.     EventForm1.Execute(ARE_CantExec);
  380.     end;
  381. end;
  382.  
  383. procedure TMainForm1.FormShow(Sender: TObject);
  384. begin
  385.   If ItemsListBox1.Items.Count > 0 then
  386.     ItemsListBox1.ItemIndex := 0;
  387.   ItemsListBox1Click(Sender);
  388. end;
  389.  
  390. procedure TMainForm1.ItemsListBox1Click(Sender: TObject);
  391. var
  392.   S : String;
  393. begin
  394.   S := ItemsListBox1.Items[ItemsListBox1.ItemIndex];
  395.   If InitItemsListBox1.Items.IndexOf(S) = -1 then
  396.     begin
  397.     ErrorForm1.ShowModal;
  398.     Close;
  399.     Exit;
  400.     end;
  401.   DescriptionStaticText1.Caption := CDDrive + CmdListBox1.Items[
  402.     InitItemsListBox1.Items.IndexOf(S)];
  403. end;
  404.  
  405. procedure TMainForm1.ItemsListBox1KeyPress(Sender: TObject; var Key: Char);
  406. begin
  407.   ItemsListBox1Click(Sender);
  408. end;
  409.  
  410. end.
  411.  
  412.