📄 jvapputils.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvAppUtils.PAS, released on 2002-07-04.
The Initial Developers of the Original Code are: Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 1997, 1998 Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 2001,2002 SGB Software
All Rights Reserved.
Last Modified: 2002-07-04
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
{$I JVCL.INC}
unit JvAppUtils;
interface
uses
Windows, Registry, Classes, Controls, Forms, Grids, IniFiles,
{$IFDEF COMPILER6_UP}
RTLConsts, { Variants, }
{$ENDIF}
JvVCLUtils; {, JvComponent}
function GetDefaultSection(Component: TComponent): string;
{$IFDEF WIN32}
procedure GetDefaultIniData(Control: TControl; var IniFileName,
Section: string; UseRegistry: Boolean);
{$ELSE}
procedure GetDefaultIniData(Control: TControl; var IniFileName,
Section: string);
{$ENDIF}
function GetDefaultIniName: string;
type
TOnGetDefaultIniName = function: string;
const
OnGetDefaultIniName: TOnGetDefaultIniName = nil;
{$IFDEF WIN32}
var
DefCompanyName: string = '';
RegUseAppTitle: Boolean = False;
function GetDefaultIniRegKey: string;
{$ENDIF}
function FindForm(FormClass: TFormClass): TForm;
function FindShowForm(FormClass: TFormClass; const Caption: string): TForm;
function ShowDialog(FormClass: TFormClass): Boolean;
function InstantiateForm(FormClass: TFormClass; var Reference): TForm;
{$IFDEF WIN32}
procedure SaveFormPlacement(Form: TForm; const IniFileName: string;
UseRegistry: Boolean);
procedure RestoreFormPlacement(Form: TForm; const IniFileName: string;
UseRegistry: Boolean);
procedure WriteFormPlacementReg(Form: TForm; IniFile: TRegIniFile;
const Section: string);
procedure ReadFormPlacementReg(Form: TForm; IniFile: TRegIniFile;
const Section: string; LoadState, LoadPosition: Boolean);
procedure SaveMDIChildrenReg(MainForm: TForm; IniFile: TRegIniFile);
procedure RestoreMDIChildrenReg(MainForm: TForm; IniFile: TRegIniFile);
procedure RestoreGridLayoutReg(Grid: TCustomGrid; IniFile: TRegIniFile);
procedure SaveGridLayoutReg(Grid: TCustomGrid; IniFile: TRegIniFile);
{$ELSE}
procedure SaveFormPlacement(Form: TForm; const IniFileName: string);
procedure RestoreFormPlacement(Form: TForm; const IniFileName: string);
{$ENDIF WIN32}
procedure WriteFormPlacement(Form: TForm; IniFile: TCustomIniFile;
const Section: string);
procedure ReadFormPlacement(Form: TForm; IniFile: TCustomIniFile;
const Section: string; LoadState, LoadPosition: Boolean);
procedure SaveMDIChildren(MainForm: TForm; IniFile: TCustomIniFile);
procedure RestoreMDIChildren(MainForm: TForm; IniFile: TCustomIniFile);
procedure RestoreGridLayout(Grid: TCustomGrid; IniFile: TCustomIniFile);
procedure SaveGridLayout(Grid: TCustomGrid; IniFile: TCustomIniFile);
function GetUniqueFileNameInDir(const Path, FileNameMask: string): string;
function StrToIniStr(const Str: string): string;
function IniStrToStr(const Str: string): string;
function IniReadString(IniFile: TObject; const Section, Ident,
Default: string): string;
procedure IniWriteString(IniFile: TObject; const Section, Ident,
Value: string);
function IniReadInteger(IniFile: TObject; const Section, Ident: string;
Default: Longint): Longint;
procedure IniWriteInteger(IniFile: TObject; const Section, Ident: string;
Value: Longint);
function IniReadBool(IniFile: TObject; const Section, Ident: string;
Default: Boolean): Boolean;
procedure IniWriteBool(IniFile: TObject; const Section, Ident: string;
Value: Boolean);
procedure IniReadSections(IniFile: TObject; Strings: TStrings);
procedure IniEraseSection(IniFile: TObject; const Section: string);
procedure IniDeleteKey(IniFile: TObject; const Section, Ident: string);
{$IFDEF WIN32}
procedure AppBroadcast(Msg, wParam: Longint; lParam: Longint);
{$ELSE}
procedure AppBroadcast(Msg, wParam: Word; lParam: Longint);
{$ENDIF WIN32}
procedure AppTaskbarIcons(AppOnly: Boolean);
{ Internal using utilities }
procedure InternalSaveGridLayout(Grid: TCustomGrid; IniFile: TObject;
const Section: string);
procedure InternalRestoreGridLayout(Grid: TCustomGrid; IniFile: TObject;
const Section: string);
procedure InternalSaveMDIChildren(MainForm: TForm; IniFile: TObject);
procedure InternalRestoreMDIChildren(MainForm: TForm; IniFile: TObject);
implementation
uses
SysUtils, Messages, Consts,
JvTypes, JvStrUtils, JvFileUtil, JvPlacemnt;
const
CrLf = #13#10;
function GetDefaultSection(Component: TComponent): string;
var
F: TCustomForm;
Owner: TComponent;
begin
if Component <> nil then
begin
if Component is TCustomForm then
Result := Component.ClassName
else
begin
Result := Component.Name;
if Component is TControl then
begin
F := GetParentForm(TControl(Component));
if F <> nil then
Result := F.ClassName + Result
else
begin
if TControl(Component).Parent <> nil then
Result := TControl(Component).Parent.Name + Result;
end;
end
else
begin
Owner := Component.Owner;
if Owner is TForm then
Result := Format('%s.%s', [Owner.ClassName, Result]);
end;
end;
end
else
Result := '';
end;
function GetDefaultIniName: string;
begin
if Assigned(OnGetDefaultIniName) then
Result:= OnGetDefaultIniName
else
Result := ExtractFileName(ChangeFileExt(Application.ExeName, '.INI'));
end;
{$IFDEF WIN32}
function GetDefaultIniRegKey: string;
begin
if RegUseAppTitle and (Application.Title <> '') then
Result := Application.Title
else
Result := ExtractFileName(ChangeFileExt(Application.ExeName, ''));
if DefCompanyName <> '' then
Result := DefCompanyName + '\' + Result;
Result := 'Software\' + Result;
end;
{$ENDIF}
{$IFDEF WIN32}
procedure GetDefaultIniData(Control: TControl; var IniFileName,
Section: string; UseRegistry: Boolean);
{$ELSE}
procedure GetDefaultIniData(Control: TControl; var IniFileName,
Section: string);
{$ENDIF}
var
I: Integer;
begin
IniFileName := '';
with Control do
if Owner is TCustomForm then
for I := 0 to Owner.ComponentCount - 1 do
if Owner.Components[I] is TJvFormPlacement then
begin
IniFileName := TJvFormPlacement(Owner.Components[I]).IniFileName;
Break;
end;
Section := GetDefaultSection(Control);
if IniFileName = '' then
{$IFDEF WIN32}
if UseRegistry then
IniFileName := GetDefaultIniRegKey
else
IniFileName := GetDefaultIniName;
{$ELSE}
IniFileName := GetDefaultIniName;
{$ENDIF}
end;
function FindForm(FormClass: TFormClass): TForm;
var
I: Integer;
begin
Result := nil;
for I := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[I] is FormClass then
begin
Result := Screen.Forms[I];
Break;
end;
end;
end;
function InternalFindShowForm(FormClass: TFormClass;
const Caption: string; Restore: Boolean): TForm;
var
I: Integer;
begin
Result := nil;
for I := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[I] is FormClass then
if (Caption = '') or (Caption = Screen.Forms[I].Caption) then
begin
Result := Screen.Forms[I];
Break;
end;
end;
if Result = nil then
begin
Application.CreateForm(FormClass, Result);
if Caption <> '' then
Result.Caption := Caption;
end;
with Result do
begin
if Restore and (WindowState = wsMinimized) then
WindowState := wsNormal;
Show;
end;
end;
function FindShowForm(FormClass: TFormClass; const Caption: string): TForm;
begin
Result := InternalFindShowForm(FormClass, Caption, True);
end;
function ShowDialog(FormClass: TFormClass): Boolean;
var
Dlg: TForm;
begin
Application.CreateForm(FormClass, Dlg);
try
Result := Dlg.ShowModal in [mrOk, mrYes];
finally
Dlg.Free;
end;
end;
function InstantiateForm(FormClass: TFormClass; var Reference): TForm;
begin
if TForm(Reference) = nil then
Application.CreateForm(FormClass, Reference);
Result := TForm(Reference);
end;
// (rom) use StrStringToEscaped, StrEscapedToString from JclStrings.pas
function StrToIniStr(const Str: string): string;
var
N: Integer;
begin
Result := Str;
repeat
N := Pos(CrLf, Result);
if N > 0 then
Result := Copy(Result, 1, N-1) + '\n' + Copy(Result, N+2, Length(Result));
until N = 0;
repeat
N := Pos(#10#13, Result);
if N > 0 then
Result := Copy(Result, 1, N-1) + '\n' + Copy(Result, N+2, Length(Result));
until N = 0;
end;
function IniStrToStr(const Str: string): string;
var
N: Integer;
begin
Result := Str;
repeat
N := Pos('\n', Result);
if N > 0 then
Result := Copy(Result, 1, N-1) + CrLf + Copy(Result, N+2, Length(Result));
until N = 0;
end;
{ The following strings should not be localized }
const
siFlags = 'Flags';
siShowCmd = 'ShowCmd';
siMinMaxPos = 'MinMaxPos';
siNormPos = 'NormPos';
siPixels = 'PixelsPerInch';
siMDIChild = 'MDI Children';
siListCount = 'Count';
siItem = 'Item%d';
function IniReadString(IniFile: TObject; const Section, Ident,
Default: string): string;
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
Result := TRegIniFile(IniFile).ReadString(Section, Ident, Default)
else
{$ENDIF}
if IniFile is TCustomIniFile then
Result := TCustomIniFile(IniFile).ReadString(Section, Ident, Default)
else
Result := Default;
end;
procedure IniWriteString(IniFile: TObject; const Section, Ident,
Value: string);
var
S: string;
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
TRegIniFile(IniFile).WriteString(Section, Ident, Value)
else
{$ENDIF}
begin
S := Value;
if S <> '' then
begin
if ((S[1] = '"') and (S[Length(S)] = '"')) or
((S[1] = '''') and (S[Length(S)] = '''')) then
S := '"' + S + '"';
end;
if IniFile is TCustomIniFile then
TCustomIniFile(IniFile).WriteString(Section, Ident, S);
end;
end;
function IniReadInteger(IniFile: TObject; const Section, Ident: string;
Default: Longint): Longint;
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
Result := TRegIniFile(IniFile).ReadInteger(Section, Ident, Default)
else
{$ENDIF}
if IniFile is TCustomIniFile then
Result := TCustomIniFile(IniFile).ReadInteger(Section, Ident, Default)
else
Result := Default;
end;
procedure IniWriteInteger(IniFile: TObject; const Section, Ident: string;
Value: Longint);
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
TRegIniFile(IniFile).WriteInteger(Section, Ident, Value)
else
{$ENDIF}
if IniFile is TCustomIniFile then
TCustomIniFile(IniFile).WriteInteger(Section, Ident, Value);
end;
function IniReadBool(IniFile: TObject; const Section, Ident: string;
Default: Boolean): Boolean;
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
Result := TRegIniFile(IniFile).ReadBool(Section, Ident, Default)
else
{$ENDIF}
if IniFile is TCustomIniFile then
Result := TCustomIniFile(IniFile).ReadBool(Section, Ident, Default)
else
Result := Default;
end;
procedure IniWriteBool(IniFile: TObject; const Section, Ident: string;
Value: Boolean);
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
TRegIniFile(IniFile).WriteBool(Section, Ident, Value)
else
{$ENDIF}
if IniFile is TCustomIniFile then
TCustomIniFile(IniFile).WriteBool(Section, Ident, Value);
end;
procedure IniEraseSection(IniFile: TObject; const Section: string);
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
TRegIniFile(IniFile).EraseSection(Section)
else
{$ENDIF}
if IniFile is TCustomIniFile then
TCustomIniFile(IniFile).EraseSection(Section);
end;
procedure IniDeleteKey(IniFile: TObject; const Section, Ident: string);
{$IFNDEF WIN32}
var
CSection: array [0..127] of Char;
CIdent: array [0..127] of Char;
CFileName: array [0..127] of Char;
{$ENDIF}
begin
{$IFDEF WIN32}
if IniFile is TRegIniFile then
TRegIniFile(IniFile).DeleteKey(Section, Ident)
else
if IniFile is TCustomIniFile then
TCustomIniFile(IniFile).DeleteKey(Section, Ident);
{$ELSE}
if IniFile is TCustomIniFile then begin
WritePrivateProfileString(StrPLCopy(CSection, Section, SizeOf(CSection) - 1),
StrPLCopy(CIdent, Ident, SizeOf(CIdent) - 1), nil,
StrPLCopy(CFileName, TCustomIniFile(IniFile).FileName, SizeOf(CFileName) - 1));
end;
{$ENDIF}
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -