1

Is it possible to load a resource at design time? I am making a speed button component, and I want to automatically load a new image from the resource whenever the button size changes. It already works properly at run time, but at design time, after I set the resource name property, it does not show any icon. I can draw a default rectangle in place of the icon if it is not possible, but it would have been nice to display my icons at design time as well.

function TPngSpeedButton.LoadIcon(ResName: String): Boolean;
var hI: HICON;
    Ico: TIcon;
    ISize: Integer;
    Png: TPngImage;
begin
 Result:= False;
 if ResName = '' then Exit;
 ISize:= Height - 7 - Round(Height * 0.15);
 Png:= TPngImage.Create; Ico:= TIcon.Create;
 try
  if LoadIconWithScaleDown(HInstance, PChar(ResName), ISize, ISize, hI) = S_OK then begin
   Ico.Handle:= hI;
   ConvertToPng(Ico, Png);
   SetPngImage(Png);
   Result:= True;
  end;
 finally
  Png.Free; Ico.Free;
 end;
end;
Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55
  • The interesting part would be the code that handles the resize of the button. – Uwe Raabe Jan 12 '22 at 16:49
  • Oh, and is the resource included in the package containing that component? – Uwe Raabe Jan 12 '22 at 16:51
  • `LoadIconWithScaleDown` it is a standard windows api. The resource cannot be included in the package, because the icons will be different for every button I drop on the form... – Marus Gradinaru Jan 12 '22 at 16:55
  • 2
    So how do you expect the image to be shown if is not even available during design time? – Uwe Raabe Jan 12 '22 at 21:35
  • It is... in the resources of my application. But, indeed, I cannot see how a component from a design time can access the resources of a compiled application... – Marus Gradinaru Jan 13 '22 at 01:28
  • 1
    Your application does not exist at design-time, so it's impossible to access resources from that non-existent application. – Ken White Jan 13 '22 at 02:30
  • On a side note, when components do want to load their own resources, they should not be using the `HInstance` variable for that, use the [`FindClassHInstance()`](https://docwiki.embarcadero.com/Libraries/en/System.FindClassHInstance) function instead. – Remy Lebeau Jan 13 '22 at 18:30

1 Answers1

1

You can not load icons at design-time from your application resource, since at that time the application executable doesn't even exist as you haven't compiled it yet.

Now, what you might be able to do is create a resource-based dynamic link library (resource DLL) which you compile separately. This way, you would be able to access the DLL resources even at design-time, similar to how the Delphi IDE is already accessing some system resources.

If you don't want to deal with additional DLLs, then put your icons into one or more ImageLists, since images from ImageLists are available both at run-time as well at design-time.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • A better approach would be to use imagelists for the icons. These can be used at runtime as well as designtime. This approach has been proved working smoothly since years now. – Uwe Raabe Jan 13 '22 at 09:07
  • @UweRaabe I agree with you but somehow I believe icons are not the only thing OP is trying to load from resources – SilverWarior Jan 14 '22 at 07:53