home *** CD-ROM | disk | FTP | other *** search
- unit Main;
-
- { Program copyright (c) 1995 by Charles Calvert }
- { Project Name: DSKFILE }
-
- { Use this program to view and edit INI files.
-
- The code shown here was originally meant to help you arrange DSK
- files. DSK files are used by Delphi to remember where the
- windows used at design time are located. For instance, the
- Editor, Objector Inspector, Watch window, etc, can all
- have their locations saved to this file if you choose the
- correct options from the Preferences page of the Options |
- Environment window. This program was originally meant to
- allow you to automatically arrange all these windows so that
- when you opened a project it would be orderly in appearance.
- The code shown here still performs that function, but I have
- found it to be only minimally useful.
-
- I decided to keep the program on the CD, however, because it
- can also be used to browse any existing INI file, include
- DELPHI.INI or SYSTEM.INI. As such it provides a useful
- function and an interesting example of how to use the
- INIFILES.PAS unit that ships with Delphi. }
-
- interface
-
- uses
- WinTypes, WinProcs, Classes,
- Graphics, Forms, Controls,
- IniFiles, StdCtrls, Menus,
- Dialogs, ExtCtrls;
-
- type
- TAry = array[1..7] of LongInt;
- TSize= (sSmall, sMedium, sLarge);
-
- const
- DefaultCaption = 'INI File Manager';
- pi800X600: TAry = (1, 1, 0, 0, 115, 221, 392);
- ww800X600: TAry = (1, 1, 0, 0, 507, 801, 91);
- ew800X600: TAry = (1, 1, 0, 220, 115, 581, 392);
- pi640X480: TAry = (1, 1, 0, 0, 97, 198, 308);
- ww640X480: TAry = (1, 1, 0, 0, 404, 636, 76);
- ew640X480: TAry = (1, 1, 0, 199, 97, 441, 308);
- pi1024x768: TAry = (1, 1, 0, 0, 97, 198, 308);
- ww1024x768: TAry = (1, 1, 0, 0, 404, 636, 76);
- ew1024x768: TAry = (1, 1, 0, 199, 97, 441, 308);
- CurScreen: TSize = sMedium;
-
- type
- tGetDem = (gdLeft, gdTop, gdWidth, gdHeight);
-
- TForm1 = class(TForm)
- ListBox1: TListBox;
- MainMenu1: TMainMenu;
- File1: TMenuItem;
- Select1: TMenuItem;
- OpenDialog1: TOpenDialog;
- N1: TMenuItem;
- Exit1: TMenuItem;
- ECurItem: TEdit;
- DSKFiles1: TMenuItem;
- SetWatch1: TMenuItem;
- SetPropInspector1: TMenuItem;
- SetEditWindow1: TMenuItem;
- SectionBox: TListBox;
- Label2: TLabel;
- Label3: TLabel;
- Label1: TLabel;
- procedure CurSectionClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure ListBox1Click(Sender: TObject);
- procedure Select1Click(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure BSetWatchClick(Sender: TObject);
- procedure bSetPropInspectClick(Sender: TObject);
- procedure bSetEditWindowClick(Sender: TObject);
- procedure OnExit(Sender: TObject);
- procedure ECurItemKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- private
- IniOpen: Boolean;
- FileName: String;
- IniFile: TIniFile;
- CurSection: string;
- CurItem: string;
- procedure GetSections;
- procedure ShowSection(S: string);
- function OkToSet: Boolean;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- StrBox, SysUtils;
-
- {$R *.DFM}
-
- procedure TForm1.ECurItemKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- var
- Value: string;
- begin
- if Key = vk_F2 then begin
- Value := ECurItem.Text;
- IniFile.WriteString(CurSection, CurItem, Value);
- ShowSection(CurSection);
- end;
- end;
-
- procedure TForm1.ShowSection(S: String);
- var
- S1, Value: string;
- i: Integer;
- SL: TStringList;
- begin
- ListBox1.Clear;
- IniFile.ReadSectionValues(S, ListBox1.Items);
- end;
-
- procedure TForm1.CurSectionClick(Sender: TObject);
- begin
- if not IniOpen then Exit;
- CurSection := SectionBox.Items.Strings[SectionBox.ItemIndex];
- ECurItem.Text := '';
- ShowSection(CurSection);
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- a, b: Integer;
- begin
- FileName := '';
- IniOpen := False;
- Caption := DefaultCaption;
- a := GetSystemMetrics(sm_cxScreen);
- b := GetSystemMetrics(sm_cyScreen);
- if (A = 800) and (B = 600) then
- CurScreen := sMedium
- else if (A = 1024) and (B = 768) then
- CurScreen := sLarge
- else
- CurScreen := sSmall;
- end;
-
- procedure TForm1.ListBox1Click(Sender: TObject);
- var
- Value: String;
- begin
- if not IniOpen then Exit;
- CurItem := ListBox1.Items.Strings[ListBox1.ItemIndex];
- CurItem := StripLastToken(CurItem, '=');
- Value := IniFile.ReadString(CurSection, CurItem, 'Foo');
- ECurItem.Text := Value;
- end;
-
- procedure TForm1.GetSections;
- var
- F: System.Text;
- S1, S: string;
- SL: TStringList;
- begin
- System.Assign(F, FileName);
- System.Reset(F);
- SL := TStringList.Create;
- while not EOF(F) do begin
- ReadLn(F, S);
- S1 := ReverseStr(S);
- if (S[1] = '[') and (S1[1] = ']') then begin
- S := Shorten(S1, 1);
- S := ReverseStr(S);
- S := Shorten(S, 1);
- SL.Add(S);
- end;
- end;
- SectionBox.Items := SL;
- SL.Free;
- System.Close(F);
- end;
-
- procedure TForm1.Select1Click(Sender: TObject);
- begin
- if OpenDialog1.Execute then begin
- IniOpen := True;
- ListBox1.Clear;
- SectionBox.Clear;
- ECurItem.Text := '';
- FileName := OpenDialog1.FileName;
- Caption := DefaultCaption + ' - ' + FileName;
- IniFile.Free;
- IniFile := TIniFile.Create(FileName);
- GetSections;
- end;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- IniFile.Free;
- end;
-
- function ww(gdType: tGetDem): LongInt;
- begin
- if CurScreen = sSmall then
- case gdType of
- gdLeft: ww := ww640x480[4];
- gdTop: ww := ww640x480[5];
- gdWidth: ww := ww640x480[6];
- gdHeight: ww := ww640x480[7];
- end
- else if CurScreen = sMedium then
- case gdType of
- gdLeft: ww := ww800x600[4];
- gdTop: ww := ww800x600[5];
- gdWidth: ww := ww800x600[6];
- gdHeight: ww := ww800x600[7];
- end
- else if CurScreen = sLarge then
- case gdType of
- gdLeft: ww := ww1024x768[4];
- gdTop: ww := ww1024x768[5];
- gdWidth: ww := ww1024x768[6];
- gdHeight: ww := ww1024x768[7];
- end;
- end;
-
- function pi(gdType: tGetDem): LongInt;
- begin
- if CurScreen = sSmall then
- case gdType of
- gdLeft: pi := pi640x480[4];
- gdTop: pi := pi640x480[5];
- gdWidth: pi := pi640x480[6];
- gdHeight: pi := pi640x480[7];
- end
- else if CurScreen = sMedium then
- case gdType of
- gdLeft: pi := pi800x600[4];
- gdTop: pi := pi800x600[5];
- gdWidth: pi := pi800x600[6];
- gdHeight: pi := pi800x600[7];
- end
- else if CurScreen = sLarge then
- case gdType of
- gdLeft: pi := pi1024x768[4];
- gdTop: pi := pi1024x768[5];
- gdWidth: pi := pi1024x768[6];
- gdHeight: pi := pi1024x768[7];
- end;
- end;
-
- function ew(gdType: tGetDem): LongInt;
- begin
- if CurScreen = sSmall then
- case gdType of
- gdLeft: ew := ew640x480[4];
- gdTop: ew := ew640x480[5];
- gdWidth: ew := ew640x480[6];
- gdHeight: ew := ew640x480[7];
- end
- else if CurScreen = sMedium then
- case gdType of
- gdLeft: ew := ew800x600[4];
- gdTop: ew := ew800x600[5];
- gdWidth: ew := ew800x600[6];
- gdHeight: ew := ew800x600[7];
- end
- else if CurScreen = sLarge then
- case gdType of
- gdLeft: ew := ew1024x768[4];
- gdTop: ew := ew1024x768[5];
- gdWidth: ew := ew1024x768[6];
- gdHeight: ew := ew1024x768[7];
- end;
- end;
-
- function TForm1.OkToSet: Boolean;
- var
- S: string;
- begin
- OkToSet := False;
- S := ExtractFileExt(FileName);
- if UpperCase(S) = '.DSK' then
- OkToSet := True
- else
- MessageDlg('DSK file not loaded', mtInformation, [mbOk], 0);
- end;
-
- procedure TForm1.BSetWatchClick(Sender: TObject);
- var
- Section: String;
- begin
- if not OKToSet then Exit;
- Section := 'WatchWindow';
- IniFile.WriteInteger(Section, 'Create', 1);
- IniFile.WriteInteger(Section, 'Visible', 1);
- IniFile.WriteInteger(Section, 'State', 0);
- IniFile.WriteInteger(Section, 'Left', 0);
- IniFile.WriteInteger(Section, 'Top', ww(gdTop));
- IniFile.WriteInteger(Section, 'Width', ww(gdWidth));
- IniFile.WriteInteger(Section, 'Height', ww(gdHeight));
- end;
-
- procedure TForm1.bSetPropInspectClick(Sender: TObject);
- var
- Section: String;
- begin
- if not OKToSet then Exit;
- Section := 'PropertyInspector';
- IniFile.WriteInteger(Section, 'Create', 1);
- IniFile.WriteInteger(Section, 'Visible', 1);
- IniFile.WriteInteger(Section, 'State', 0);
- IniFile.WriteInteger(Section, 'Left', pi(gdLeft));
- IniFile.WriteInteger(Section, 'Top', pi(gdTop));
- IniFile.WriteInteger(Section, 'Width', pi(gdWidth));
- IniFile.WriteInteger(Section, 'Height', pi(gdHeight));
- end;
-
- procedure TForm1.bSetEditWindowClick(Sender: TObject);
- var
- Section: String;
- TempName: String;
- begin
- if not OKToSet then Exit;
- Section := 'EditWindow0';
- IniFile.WriteInteger(Section, 'Create', 1);
- IniFile.WriteInteger(Section, 'Visible', 1);
- IniFile.WriteInteger(Section, 'State', 0);
- IniFile.WriteInteger(Section, 'Left', ew(gdLeft));
- IniFile.WriteInteger(Section, 'Top', ew(gdTop));
- IniFile.WriteInteger(Section, 'Width', ew(gdWidth));
- IniFile.WriteInteger(Section, 'Height', ew(gdHeight));
- IniFile.WriteInteger(Section, 'ViewCount', 1);
- IniFile.WriteInteger(Section, 'CurrentView', 1);
- IniFile.WriteInteger(Section, 'View0', 0);
- Section := 'View0';
- IniFile.WriteInteger(Section, 'Module', 0);
- IniFile.WriteInteger(Section, 'CursorX', 1);
- IniFile.WriteInteger(Section, 'CursorY', 1);
- IniFile.WriteInteger(Section, 'TopLine', 1);
- IniFile.WriteInteger(Section, 'LeftCol', 1);
- Section := 'Modules';
- IniFile.WriteInteger(Section, 'Count', 1);
- IniFile.WriteInteger(Section, 'EditWindowCount', 1);
- TempName := FileName;
- TempName[0] := Chr(Ord(TempName[0]) - 3);
- TempName := TempName + 'DPR';
- IniFile.WriteString(Section, 'Module0', TempName);
- end;
-
- procedure TForm1.OnExit(Sender: TObject);
- begin
- Close;
- end;
-
- end.
-