Solved

Delphi XE LoadFromResourceName resource not found

Posted on 2013-05-31
3
1,732 Views
Last Modified: 2013-08-19
I have created a component inherited from an image button. I have added the up and down images to the resource image list using the 'Project|Resources and Images' menu option .
the bitmaps appear in the Project Manager and it all compiles an installs just fine.
When I use the button in my project there are no errors and the button image is used when viewed in the design window.
My project compiles just fine but when I try to open the window when running the program it give an error:


error message
the create code for the button is:

constructor TSMfcImageBtn.create(aowner:tcomponent);
var
buttonup,buttondown : Tbitmap;
begin
inherited;
try
  buttonup := Tbitmap.Create;
  buttonup.LoadFromResourceName(hInstance,'ButtonUp');
  image.loadfrombitmap(buttonup);
  buttondown := Tbitmap.Create;
  buttondown.LoadFromResourceName(hInstance,'ButtonDown');
  imagedown.loadfrombitmap(buttondown);
finally
  buttonup.Free;
  buttondown.Free;
end;
offsets.GlyphX := 0;
offsets.GlyphY := 0;
offsets.ImageDownX := 0;
offsets.ImageDownY := 0;
offsets.TextDownX := 0;
offsets.TextDownY := 0;
offsets.TextX := 0;
offsets.Texty := 0;
end;


Any ideas
0
Comment
Question by:RipBucks
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39211630
This way your component Hinstance is not equal to Application Hinstance. So, your code need little reworking. Also you need to check if resource exists first. Second thing is that if exception comes on first bitmap because of resource - try..finally block forces to free second image which is not created at this time ... great mistake.

var
  buttonup,buttondown : Tbitmap;
  hInst, MyRes: Cardinal;
begin
  buttonup := Tbitmap.Create;
  buttondown := Tbitmap.Create;
  try
    hInst := FindClassHInstance(Self.ClassType);
    if hInst>0 then
    begin
      MyRes := FindResource(hInst, 'ButtonUp', RT_Bitmap);
      if MyRes>0 then
      begin
        buttonup.LoadFromResourceName(hInst,'ButtonUp');
        image.loadfrombitmap(buttonup);
      end;

      MyRes := FindResource(hInst, 'ButtonDown', RT_Bitmap);
      if MyRes>0 then
      begin
        buttondown.LoadFromResourceName(hInst,'ButtonDown');
        imagedown.loadfrombitmap(buttondown);
      end;
    end;
  finally
    buttonup.Free;
    buttondown.Free;
  end;

Open in new window

0
 

Author Comment

by:RipBucks
ID: 39218449
Thanks for the code but FindResource won't compile. I can find it in the help but it does not appear in my system unit. Nor does the program know what RT_Bitmap is.
So I can't test if this is a solution
0
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39220109
Add Windows in uses.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question