Statistiques
| Révision:

root / ReInit.pas

Historique | Voir | Annoter | Télécharger (3,553 ko)

1 3 avalancogn
unit reinit;
2
3
interface
4
5
uses
6
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
7
8
procedure ReinitializeForms;
9
function LoadNewResourceModule(Locale: LCID): Longint;
10
11
implementation
12
13
type
14
  TAsInheritedReader = class(TReader)
15
  public
16
    procedure ReadPrefix(var Flags: TFilerFlags; var AChildPos: Integer); override;
17
  end;
18
19
procedure TAsInheritedReader.ReadPrefix(var Flags: TFilerFlags; var AChildPos: Integer);
20
begin
21
  inherited ReadPrefix(Flags, AChildPos);
22
  Include(Flags, ffInherited);
23
end;
24
25
function SetResourceHInstance(NewInstance: Longint): Longint;
26
var
27
  CurModule: PLibModule;
28
begin
29
  CurModule := LibModuleList;
30
  Result := 0;
31
  while CurModule <> nil do
32
  begin
33
    if CurModule.Instance = HInstance then
34
    begin
35
      if CurModule.ResInstance <> CurModule.Instance then
36
        FreeLibrary(CurModule.ResInstance);
37
      CurModule.ResInstance := NewInstance;
38
      Result := NewInstance;
39
      Exit;
40
    end;
41
    CurModule := CurModule.Next;
42
  end;
43
end;
44
45
function LoadNewResourceModule(Locale: LCID): Longint;
46
var
47
  FileName: array [0..260] of char;
48
  P: PChar;
49
  LocaleName: array[0..4] of Char;
50
  NewInst: Longint;
51
begin
52
  GetModuleFileName(HInstance, FileName, SizeOf(FileName));
53
  GetLocaleInfo(Locale, LOCALE_SABBREVLANGNAME, LocaleName, SizeOf(LocaleName));
54
  P := PChar(@FileName) + lstrlen(FileName);
55
  while (P^ <> '.') and (P <> @FileName) do Dec(P);
56
  NewInst := 0;
57
  Result := 0;
58
  if P <> @FileName then
59
  begin
60
    Inc(P);
61
    if LocaleName[0] <> #0 then
62
    begin
63
      // Then look for a potential language/country translation
64
      lstrcpy(P, LocaleName);
65
      NewInst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
66
      if NewInst = 0 then
67
      begin
68
        // Finally look for a language only translation
69
        LocaleName[2] := #0;
70
        lstrcpy(P, LocaleName);
71
        NewInst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
72
      end;
73
    end;
74
  end;
75
  if NewInst <> 0 then
76
    Result := SetResourceHInstance(NewInst)
77
end;
78
79
function InternalReloadComponentRes(const ResName: string; HInst: THandle; var Instance: TComponent): Boolean;
80
var
81
  HRsrc: THandle;
82
  ResStream: TResourceStream;
83
  AsInheritedReader: TAsInheritedReader;
84
begin                   { avoid possible EResNotFound exception }
85
  if HInst = 0 then HInst := HInstance;
86
  HRsrc := FindResource(HInst, PChar(ResName), RT_RCDATA);
87
  Result := HRsrc <> 0;
88
  if not Result then Exit;
89
  ResStream := TResourceStream.Create(HInst, ResName, RT_RCDATA);
90
  try
91
    AsInheritedReader := TAsInheritedReader.Create(ResStream, 4096);
92
    try
93
      Instance := AsInheritedReader.ReadRootComponent(Instance);
94
    finally
95
      AsInheritedReader.Free;
96
    end;
97
  finally
98
    ResStream.Free;
99
  end;
100
  Result := True;
101
end;
102
103
function ReloadInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
104
105
  function InitComponent(ClassType: TClass): Boolean;
106
  begin
107
    Result := False;
108
    if (ClassType = TComponent) or (ClassType = RootAncestor) then Exit;
109
    Result := InitComponent(ClassType.ClassParent);
110
    Result := InternalReloadComponentRes(ClassType.ClassName, FindResourceHInstance(
111
      FindClassHInstance(ClassType)), Instance) or Result;
112
  end;
113
114
begin
115
  Result := InitComponent(Instance.ClassType);
116
end;
117
118
procedure ReinitializeForms;
119
var
120
  Count: Integer;
121
  I: Integer;
122
  Form: TTntForm;
123
begin
124
  Count := Screen.FormCount;
125
  for I := 0 to Count - 1 do
126
  begin
127
    Form := Screen.Forms[I];
128
    ReloadInheritedComponent(Form, TForm);
129
  end;
130
end;
131
132
end.