Solved

Delphi XE LoadFromResourceName resource not found

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy Tmemory stream non latin charcter issue 2 98
FMX enumerated colours 2 84
How to debug For loops? 3 45
Tviruailstringtree sort multi columns on header click 1 54
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

17 Experts available now in Live!

Get 1:1 Help Now