4

I am writing a component (a button) which needs a bitmap to be displayed on it. I don't want to make an ImageList property and the user assigns an image. I want that button to have only the image chosen by me.

I tried to include the bitmap in a resource file but when I try to access it I get "Resource not found" error message. This is what I've done:

myres.rc

FIXED BMP "fixed.bmp"

I compiled the resource file with: brcc32 myres.rc

Then I included it in my component unit...

implementation
{$R .\resources\myres.res}

And access it with...

MyComponent.Glyph.LoadFromResourceName(HInstance,'FIXED');
// MyComponent = class(TSpeedButton)

Edit1: I deleted the {$R .\resources\myres.res} directive and I loaded the resource from menu Project -> Resources and it's working, both with HInstance or FindClassHInstance(MyComponent).

Using a resource editor I found that when I load the resource from the menu the resource appears with the name "FIXED" as it should, but when I load the resource compiled with brcc32 it appears with the name "0". It seems that brcc32 doesn't set the name correctly.

But I don't want to load it from menu, I want it to be loaded automatically with the component.

Edit2: Remy Lebeau is correct. I was using a wrong BMP format (the file starts with 'BM6' characters instead 'BM8' like Photoshop produce it, and it works).

Marus Gradinaru
  • 2,824
  • 1
  • 26
  • 55

1 Answers1

10

Change BMP to BITMAP in your RC file, and change HInstance to FindClassHInstance() in your code:

FIXED BITMAP "fixed.bmp"

Glyph.LoadFromResourceName(FindClassHInstance(MyComponent), 'FIXED');
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Works fine for me in every component I have ever written that uses custom bitmaps. And you need to use `FindClassHInstance()` instead of `HInstance` in order for your component's resources to be found correctly regardless of whether the project using your component has Runtime Packages enabled or not. `HInstance` will not always point at your component's package, but `FindClassHInstance()` will always return an appropriate instance handle for your component's package. – Remy Lebeau Jun 04 '15 at 16:46