how to add resources and to use them
Asked Answered
P

1

11

In my application i want to add 2 images as resources

I want to use those images ,when i click yes button in my application first image will be set as wallpaper and when i click on No button in my application second image will be set as desktop wallpaper

thanks in advance

regards

Pellicle answered 18/12, 2009 at 14:44 Comment(0)
S
25

The easiest way is to create a text file and name it resources.rc or something (as long as it isn't the same name as your project file as that already has a resource file of its own).

If you're adding images, you'll need to add lines such as:

IMG_1 BITMAP "c:\my files\image1.bmp"
IMG_2 RCDATA "c:\my files\image2.jpg"

Note that the first parameter is an unique identifying resource name. The second parameter is the resource type. Some constants are available such as BITMAP and AVI. For others, use RCDATA. The third parameter is the full path and file name of the resource.

Now, in Delphi, you can add this .rc file to your project in the project manager.

To use the resources, you need different methods according to the resource type.

To load a bitmap, you can use:

imgWallpaper1.Picture.Bitmap.LoadFromResourceName(HInstance, 'IMG_1');

To load a JPEG, you need to convert it like this:

var
   jpgLogo: TJpegImage;
   RStream: TResourceStream;
begin
     RStream := TResourceStream.Create(HInstance, 'IMG_2', RT_RCDATA);
     Try
        jpgLogo := TJpegImage.Create;
        Try
           jpgLogo.LoadFromStream(RStream);
           imgLogo.Picture.Graphic := jpgLogo;
        Finally
           jpgLogo.Free;
        End;
     Finally
        RStream.Free;
     End; {Try..Finally}
Sommersommers answered 18/12, 2009 at 15:38 Comment(4)
The content of the res file getting included into the exe file, how to avoid this?Indiscerptible
@grinner, that is the whole point of resource files, to add them to the executable so you do not have to distribute extra files.Sommersommers
@Sommersommers ok but i dont like fat exe files, so another point is to detach and separate static data from the exe and load them only when needed. Finally i used dlls for this purpose.Indiscerptible
@Indiscerptible the res of the exe-file isn't loaded into memory until needed. That's one of the features of exe over the ancient .com-filesMicropaleontology

© 2022 - 2024 — McMap. All rights reserved.