home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Examples / Apps / RichEdit / REINIT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-01-26  |  3.6 KB  |  136 lines

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