📄 formutilities.pas
字号:
unit FormUtilities;
interface
uses
Windows, Forms, IniFiles, SysUtils, StdCtrls, ExtCtrls, Dialogs,
comctrls, Utilities32;
procedure SaveFormValuesToFile(Form : TForm; FileName : string);
procedure SaveFormStateToFile(Form : TForm; FileName : string);
procedure SaveFormValues(Form : TForm);
procedure SaveFormState(Form : TForm);
procedure LoadFormValuesFromFile(Form : TForm; FileName : string);
procedure LoadFormStateFromFile(Form : TForm; FileName : string);
procedure LoadFormState(Form : TForm);
procedure LoadFormPos(Form : TForm);
procedure LoadFormValues(Form : TForm);
procedure ShowWarning(s : string);
function InvokeHelpMacro(const StrMacro: String; const ForceFile: Boolean): Boolean;
const
SetFile : string = '';
implementation
function InvokeHelpMacro(const StrMacro: String; const ForceFile: Boolean): Boolean;
Begin
if ForceFile then
Application.HelpCommand(HELP_FORCEFILE, 0);
Result := Application.HelpCommand(HELP_COMMAND, Longint(@StrMacro[1]));
// Forces the help file to open, and shows the 'Contents' tab:
// InvokeHelpMacro('Contents()', TRUE);
// Forces the help file to open, and shows the 'Index' tab:
// InvokeHelpMacro('Search()', True);
// Forces the help file to open, and shows the 'Find' tab (WinHelp 4 only):
// InvokeHelpMacro('Find()', True);
End;
procedure ShowWarning(s : string);
begin
MessageDlg(s,mtWarning,[mbOk],0);
end;
procedure LoadFormStateFromFile(Form : TForm; FileName : string);
var
SectionName : string;
L : integer;
begin
with TForm(Form) do begin
SectionName := ClassName + 'State';
if FileName = '' then
FileName := ChangeFileExt(ParamStr(0),'.set');
with TIniFile.Create(FileName) do begin
if ReadInteger(SectionName, Name + '.Left', -5000) > -5000 then
Left := ReadInteger(SectionName, Name + '.Left', Left);
if ReadInteger(SectionName, Name + '.Left', -5000) > -5000 then
L := ReadInteger(SectionName, Name + '.Left', Left);
if ReadInteger(SectionName, Name + '.Top', -5000) > -5000 then
Top := ReadInteger(SectionName, Name + '.Top', Top);
if ReadInteger(SectionName, Name + '.Width', -5000) > -5000 then
Width := ReadInteger(SectionName, Name + '.Width', Width);
if ReadInteger(SectionName, Name + '.Height', -5000) > -5000 then
Height := ReadInteger(SectionName, Name + '.Height', Height);
if ReadInteger(SectionName, Name + '.State', -5000) > -5000 then
WindowState := TWindowState(ReadInteger(SectionName, Name + '.WindowState', Integer(WindowState)));
end;
end
end;
procedure LoadFormPos(Form : TForm);
var
FileName,SectionName : string;
begin
FileName := '';
with TForm(Form) do begin
SectionName := ClassName + 'State';
if FileName = '' then
FileName := ChangeFileExt(ParamStr(0),'.set');
with TIniFile.Create(FileName) do begin
if (ReadInteger(SectionName, Name + '.Left', -5000) > -5000) AND (ReadInteger(SectionName, Name + '.Top', -5000) > -5000) then
Position := poDesigned;
if ReadInteger(SectionName, Name + '.Left', -5000) > -5000 then
Left := ReadInteger(SectionName, Name + '.Left', Left);
if ReadInteger(SectionName, Name + '.Top', -5000) > -5000 then
Top := ReadInteger(SectionName, Name + '.Top', Top);
end;
end
end;
procedure SaveFormStateToFile(Form : TForm; FileName : string);
var
SectionName : string;
begin
if GetDriveType(PChar(LOwerCase(application.exename))) = DRIVE_CDROM then
Exit;
with TForm(Form) do begin
SectionName := ClassName + 'State';
if FileName = '' then
FileName := ChangeFileExt(ParamStr(0),'.set');
with TIniFile.Create(FileName) do begin
EraseSection(SectionName);
if (Left < 0) or (Left > 5000) then
Left := 0;
WriteInteger(SectionName, Name + '.Left', Left);
if (Top < 0) or (Top > 5000) then
Top := 0;
WriteInteger(SectionName, Name + '.Top', Top);
WriteInteger(SectionName, Name + '.Width', Width);
WriteInteger(SectionName, Name + '.Height', Height);
WriteInteger(SectionName, Name + '.WindowState', Integer(WindowState));
end;
end
end;
procedure SaveFormState(Form : TForm);
begin
SaveFormStateToFile(Form, '');
end;
procedure LoadFormState(Form : TForm);
begin
LoadFormStateFromFile(Form, '');
end;
procedure SaveFormValuesToFile(Form : TForm; FileName : string);
var
i,j,NoOfMemoLines,MemoLine,cbCount : integer;
SectionName,MemoName,s,cbName : string;
TabSheet : TTabSheet;
begin
if GetDriveType(PChar(LOwerCase(application.exename))) = DRIVE_CDROM then
Exit;
with TForm(Form) do begin
SectionName := ClassName;
if FileName = '' then
FileName := ChangeFileExt(ParamStr(0),'.set');
SetFile := FileName;
with TIniFile.Create(FileName) do begin
EraseSection(SectionName);
for i := 0 to (ComponentCount-1) do begin
// Check for TMemo
if Components[i].Tag < 0 then
Continue;
if Components[i] is TMemo then begin
NoOfMemoLines := TMemo(Components[i]).Lines.Count;
MemoName := TMemo(Components[i]).Name;
WriteInteger(SectionName,MemoName+'0',NoOfMemoLines);
for MemoLine := 0 to NoOfMemoLines-1 do
WriteString(SectionName,MemoName+IntToStr(MemoLine+1),TMemo(Components[i]).Lines[MemoLine]);
end
else begin
// Check for TPageControl
if Components[i] is TPageControl then begin
s := TPageControl(Components[i]).ActivePage.Name;
WriteString(SectionName,TPageControl(Components[i]).Name,s);
end;
// Check for TOpenDialog (includes TSaveDialog as well)
if Components[i] is TOpenDialog then begin
if TOpenDialog(Components[i]).FileName <> '' then begin
s := ExtractFilePath(TOpenDialog(Components[i]).FileName);
WriteString(SectionName,TOpenDialog(Components[i]).Name+'_InitialDir',s);
end
else begin
s := TOpenDialog(Components[i]).InitialDir;
if DirExists(s) then
WriteString(SectionName,TOpenDialog(Components[i]).Name+'_InitialDir',s);
end;
end;
// Check for TCustomEdit
if Components[i] is TCustomEdit then begin
WriteString(SectionName,TCustomEdit(Components[i]).Name,TCustomEdit(Components[i]).Text);
end;
// Check for TCheckBox
if Components[i] is TCheckBox then begin
WriteBool(SectionName,TCheckBox(Components[i]).Name,TCheckBox(Components[i]).Checked);
end;
// Check for TRadioButton
if Components[i] is TRadioButton then begin
WriteBool(SectionName,TRadioButton(Components[i]).Name,TRadioButton(Components[i]).Checked);
end;
// Check for TRadioGroup
if Components[i] is TRadioGroup then begin
WriteInteger(SectionName,TRadioGroup(Components[i]).Name,TRadioGroup(Components[i]).ItemIndex);
end;
// Check for TComboBox
if Components[i] is TComboBox then begin
cbName := TComboBox(Components[i]).Name;
cbCount := TComboBox(Components[i]).Items.Count;
if TComboBox(Components[i]).Style <> csDropDownList then begin
WriteInteger(SectionName,TComboBox(Components[i]).Name+'0',TComboBox(Components[i]).Items.Count);
WriteString(SectionName,cbName+'Text',TComboBox(Components[i]).Text);
for j := 0 to cbCount-1 do begin
WriteString(SectionName,cbName+IntToStr(j+1),TComboBox(Components[i]).Items[j]);
end;
end
else
WriteInteger(SectionName,TComboBox(Components[i]).Name,TComboBox(Components[i]).ItemIndex);
end;
// Check for TSplitter
if Components[i] is TSplitter then begin
WriteInteger(SectionName,TSplitter(Components[i]).Name+'.Left',TSplitter(Components[i]).Left);
WriteInteger(SectionName,TSplitter(Components[i]).Name+'.Top',TSplitter(Components[i]).Top);
end;
end;
end;
Free;
end;
end;
end;
procedure LoadFormValues(Form : TForm);
begin
LoadFormValuesFromFile(Form, '');
end;
procedure LoadFormValuesFromFile(Form : TForm; FileName : string);
var
cbCount,i,j,NoOfMemoLines,MemoLine,n : integer;
cbName,SectionName,MemoName,s : string;
begin
with TForm(Form) do begin
SectionName := ClassName;
if FileName = '' then
FileName := ChangeFileExt(ParamStr(0),'.set');
SetFile := FileName;
with TIniFile.Create(FileName) do begin
for i := 0 to (ComponentCount-1) do begin
if Components[i].Tag < 0 then
Continue;
if Components[i] is TMemo then begin
MemoName := TMemo(Components[i]).Name;
NoOfMemoLines := ReadInteger(SectionName,MemoName+'0',0);
if NoOfMemoLines <> 0 then begin
TMemo(Components[i]).Lines.Clear;
for MemoLine := 0 to NoOfMemoLines-1 do
TMemo(Components[i]).Lines.Add(ReadString(SectionName,MemoName+IntToStr(MemoLine+1),''));
end;
end
else begin
// Check for TOpenDialog (includes TSaveDialog as well)
if Components[i] is TOpenDialog then begin
if TOpenDialog(Components[i]).InitialDir = '' then
TOpenDialog(Components[i]).InitialDir := ExtractFilePath(Application.ExeName);
TOpenDialog(Components[i]).InitialDir := ReadString(SectionName,TOpenDialog(Components[i]).Name+'_InitialDir',TOpenDialog(Components[i]).InitialDir);
end;
// Check for TCustomEdit
if Components[i] is TCustomEdit then begin
TCustomEdit(Components[i]).Text := ReadString(SectionName,
TCustomEdit(Components[i]).Name,
TCustomEdit(Components[i]).Text);
end;
// Check for TPageControl
if Components[i] is TPageControl then begin
s := ReadString(SectionName,TPageControl(Components[i]).Name,'');
for n := 0 to TPageControl(Components[i]).PageCount-1 do begin
if s = TPageControl(Components[i]).Pages[n].Name then begin
TPageControl(Components[i]).ActivePage := TPageControl(Components[i]).Pages[n];
Break;
end;
end;
end;
// Check for TCheckBox
if Components[i] is TCheckBox then begin
TCheckBox(Components[i]).Checked := ReadBool(SectionName,TCheckBox(Components[i]).Name,TCheckBox(Components[i]).Checked);
end;
// Check for TRadioButton
if Components[i] is TRadioButton then begin
TRadioButton(Components[i]).Checked := ReadBool(SectionName,TRadioButton(Components[i]).Name,TRadioButton(Components[i]).Checked);
end;
// Check for TRadioGroup
if Components[i] is TRadioGroup then begin
TRadioGroup(Components[i]).ItemIndex := ReadInteger(SectionName,TRadioGroup(Components[i]).Name,0);
end;
// Check for TComboBox
if Components[i] is TComboBox then begin
cbName := TComboBox(Components[i]).Name;
if TComboBox(Components[i]).Style <> csDropDownList then begin
cbCount := ReadInteger(SectionName,cbName+'0',0);
if cbCount <> 0 then begin
TComboBox(Components[i]).Items.Clear;
for j := 0 to cbCount-1 do
TComboBox(Components[i]).Items.Add(ReadString(SectionName,cbName+IntToStr(j+1),''));
end;
TComboBox(Components[i]).Text := ReadString(SectionName,cbName+'Text','');
end
else
TComboBox(Components[i]).ItemIndex := ReadInteger(SectionName,TComboBox(Components[i]).Name,0);
end;
// Check for TSplitter
if Components[i] is TSplitter then begin
TSplitter(Components[i]).Left := ReadInteger(SectionName,TSplitter(Components[i]).Name+'.Left',TSplitter(Components[i]).Left);
TSplitter(Components[i]).Top := ReadInteger(SectionName,TSplitter(Components[i]).Name+'.Top',TSplitter(Components[i]).Top);
end;
end;
end;
Free;
end;
end;
end;
procedure SaveFormValues(Form : TForm);
begin
SaveFormValuesToFile(Form, '');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -