Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delphi XE LoadFromResourceName resource not found

Posted on 2013-05-31
3
Medium Priority
?
2,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 1500 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 28

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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