home *** CD-ROM | disk | FTP | other *** search
- unit Resources;
-
- interface
-
- uses
- Windows, Graphics, Classes;
-
- type
- TResourceLibrary = class
- private
- Handle: THandle;
- ResStream: TResourceStream;
- function GetStrings (ResourceID: Cardinal): String;
- function GetResourceStream (ResourceID: Cardinal): TResourceStream;
- public
- constructor Create;
- constructor CreateForDLL (DLLFileName: String);
- destructor Destroy; override;
- // access to known datatypes
- procedure LoadIcon (ResourceID: Cardinal; Icon: TIcon);
- procedure LoadBitmap (ResourceID: Cardinal; Bitmap: TBitmap);
- // access to strings
- property Strings[ResourceID: Cardinal]: String read GetStrings; default;
- // access to resources in stream format
- property ResourceStream [ResourceID: Cardinal]: TResourceStream read GetResourceStream;
- end;
-
- implementation
-
- uses
- Forms, SysUtils, Dialogs;
-
- type
- EResourceLibrary = class (Exception);
-
- constructor TResourceLibrary.Create;
- begin
- inherited;
- Handle := HInstance;
- end;
-
- constructor TResourceLibrary.CreateForDLL (DLLFileName: String);
- begin
- inherited;
- Handle := LoadLibrary (PChar (DLLFileName));
- if Handle = 0 then MessageDlg (Format ('Filename "%s" not found!', [DLLFileName]), mtError, [mbOK], 0);
- end;
-
- destructor TResourceLibrary.Destroy;
- begin
- if (Handle <> 0) and (Handle <> HInstance) then begin
- FreeLibrary (Handle);
- end;
- ResStream.Free;
- inherited;
- end;
-
- procedure TResourceLibrary.LoadIcon (ResourceID: Cardinal; Icon: TIcon);
- begin
- if Handle = 0 then Exit;
- Icon.Handle := Windows.LoadIcon (Handle, Pointer (ResourceID));
- if Icon.Handle = 0 then RaiseLastWin32Error;
- end;
-
- procedure TResourceLibrary.LoadBitmap (ResourceID: Cardinal; Bitmap: TBitmap);
- begin
- if Handle = 0 then Exit;
- Bitmap.Handle := Windows.LoadBitmap (Handle, Pointer (ResourceID));
- if Bitmap.Handle = 0 then RaiseLastWin32Error;
- end;
-
- function TResourceLibrary.GetStrings (ResourceID: Cardinal): String;
- begin
- if Handle = 0 then begin
- Result := '';
- end else begin
- // set our buffer to be large enough to hold a valid string table entry
- SetLength (Result, 1024);
- // now load the string into our correctly sized buffer
- SetLength (Result, LoadString (Handle, ResourceID, PChar (Result), Length (Result)));
- end;
- end;
-
- function TResourceLibrary.GetResourceStream (ResourceID: Cardinal): TResourceStream;
- begin
- ResStream.Free;
- ResStream := TResourceStream.CreateFromID (Handle, ResourceID, RT_RCDATA);
- Result := ResStream;
- end;
-
- end.
-
-