Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delphi XE LoadFromResourceName resource not found

Posted on 2013-05-31
3
Medium Priority
?
2,116 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

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