Solved

Delphi XE LoadFromResourceName resource not found

Posted on 2013-05-31
3
1,650 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 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Sinisa Vuk
Comment Utility
Add Windows in uses.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now