home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / resource / Resources.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-19  |  2.5 KB  |  93 lines

  1. unit Resources;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Graphics, Classes;
  7.   
  8. type
  9.   TResourceLibrary = class
  10.   private
  11.     Handle: THandle;
  12.     ResStream: TResourceStream;
  13.     function GetStrings (ResourceID: Cardinal): String;
  14.     function GetResourceStream (ResourceID: Cardinal): TResourceStream;
  15.   public
  16.     constructor Create;
  17.     constructor CreateForDLL (DLLFileName: String);
  18.     destructor Destroy; override;
  19.     // access to known datatypes
  20.     procedure LoadIcon (ResourceID: Cardinal; Icon: TIcon);
  21.     procedure LoadBitmap (ResourceID: Cardinal; Bitmap: TBitmap);
  22.     // access to strings
  23.     property Strings[ResourceID: Cardinal]: String read GetStrings; default;
  24.     // access to resources in stream format
  25.     property ResourceStream [ResourceID: Cardinal]: TResourceStream read GetResourceStream;
  26.   end;
  27.  
  28. implementation
  29.  
  30. uses
  31.   Forms, SysUtils, Dialogs;
  32.  
  33. type
  34.   EResourceLibrary = class (Exception);
  35.  
  36. constructor TResourceLibrary.Create;
  37. begin
  38.   inherited;
  39.   Handle := HInstance;
  40. end;
  41.  
  42. constructor TResourceLibrary.CreateForDLL (DLLFileName: String);
  43. begin
  44.   inherited;
  45.   Handle := LoadLibrary (PChar (DLLFileName));
  46.   if Handle = 0 then MessageDlg (Format ('Filename "%s" not found!', [DLLFileName]), mtError, [mbOK], 0);
  47. end;
  48.  
  49. destructor TResourceLibrary.Destroy;
  50. begin
  51.   if (Handle <> 0) and (Handle <> HInstance) then begin
  52.     FreeLibrary (Handle);
  53.   end;
  54.   ResStream.Free;
  55.   inherited;
  56. end;
  57.  
  58. procedure TResourceLibrary.LoadIcon (ResourceID: Cardinal; Icon: TIcon);
  59. begin
  60.   if Handle = 0 then Exit;
  61.   Icon.Handle := Windows.LoadIcon (Handle, Pointer (ResourceID));
  62.   if Icon.Handle = 0 then RaiseLastWin32Error;
  63. end;
  64.  
  65. procedure TResourceLibrary.LoadBitmap (ResourceID: Cardinal; Bitmap: TBitmap);
  66. begin
  67.   if Handle = 0 then Exit;
  68.   Bitmap.Handle := Windows.LoadBitmap (Handle, Pointer (ResourceID));
  69.   if Bitmap.Handle = 0 then RaiseLastWin32Error;
  70. end;
  71.  
  72. function TResourceLibrary.GetStrings (ResourceID: Cardinal): String;
  73. begin
  74.   if Handle = 0 then begin
  75.     Result := '';
  76.   end else begin
  77.     // set our buffer to be large enough to hold a valid string table entry
  78.     SetLength (Result, 1024);
  79.     // now load the string into our correctly sized buffer
  80.     SetLength (Result, LoadString (Handle, ResourceID, PChar (Result), Length (Result)));
  81.   end;
  82. end;
  83.  
  84. function TResourceLibrary.GetResourceStream (ResourceID: Cardinal): TResourceStream;
  85. begin
  86.   ResStream.Free;
  87.   ResStream := TResourceStream.CreateFromID (Handle, ResourceID, RT_RCDATA);
  88.   Result := ResStream;
  89. end;
  90.  
  91. end.
  92.  
  93.