Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2014, 08:34 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default Resource Manager in Delphi 2010

Hi and thanks in advanced for the Help.

Using Delphi 2010.



Delphi 2010 is equipped with a resource manager where you can include Icons images etc. I never had the need to use this until now. I have read about the TResourceStream class and how to use it, however, no success.

I have a bit button in wish as for example purposes I would like to get the image from the resource manager and stick it into the bit button glyph property.

I would like to know if some of you have use it, and allow me to know the code on how to accomplish this.

Thanks so much
Attached Images
File Type: jpg ResourceWindow.JPG (36.4 KB, 12 views)
Reply With Quote
  #2  
Old 06-11-2014, 06:49 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You cannot assign a png image to TBitBtn Glyph property(only bmp).
With that said do the following:
1. Forget about Project/Resources
2. Create a new directory in your project folder, MyResources(for example)
3. Go to the bin directory $(BDS)\bin, locate the Brcc32.exe, copy to MyResources folder
4. Copy all the images you want to include to the resource file, to MyResources directory
5. Create a new file: MyResources.rc, add the following lines:
Code:
Bitmap1 BITMAP Man_Sluet.bmp
Bitmap2 BITMAP police_woman_Sluet.bmp
Bitmap3 BITMAP PoliceMan_Sluet.bmp
5. Run the Brc32.exe with the following parameter MyResources.rc. You can do this with cmd or Total Commander:
Brc32.exe MyResources.rc
If the operation was successful you will find a MyResources.res file, copy to your project folder
6. Include the resource file to your main form
Code:
implementation
{$R *.dfm}
{$R MyResources.res}
...
7. Load Glyph from resource
Code:
BitBtn1.Glyph.LoadFromResourceName(hInstance, 'Bitmap1');
or
Code:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
 RS: TResourceStream;
begin
  RS := TResourceStream.Create(hInstance, 'Bitmap1', 'BITMAP' );
  try
    RS.Position := 0;
    //do whatever you want to do with the stream
  finally
    RS.Free;
  end;
Reply With Quote
  #3  
Old 06-11-2014, 01:24 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

kolbasz

I just saw this response. I would like to thank you not just on my behalf but on everyone behalf on this forum for all your help. I haven't try your detailed solution but I am sure is going to work.

Thank you so much again.

Best regards
Reply With Quote
  #4  
Old 06-11-2014, 03:33 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi and Thanks for the help.

I went step by step, take a look at the screenshots, the strange thing is that the .RES file gets create it, Delphi Compiles fine but I get the following error (See Resource Error)

Thanks

Best regards
Attached Images
File Type: jpg Brc32Error.JPG (47.0 KB, 9 views)
File Type: jpg MyResorces.JPG (32.7 KB, 5 views)
File Type: jpg ResourceError.JPG (15.7 KB, 4 views)
Reply With Quote
  #5  
Old 06-11-2014, 05:06 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

I see in the screenshots that you changed the bitmap to png. This is perfectly fine, however as I mentioned in my previous post this line:
BitBtn1.Glyph.LoadFromResourceName(hInstance, 'Bitmap1');
won't work because you cannot assign a png to BitBtn1.Glyph. You wil get a resource error, although the resource is there. To test your .res file, do the following:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
 RS: TResourceStream;
begin
 RS := TResourceStream.Create(hInstance, 'Bitmap1', 'PNG' );//<--don't 
//forget to change BITMAP to PNG
 try
   RS.Position := 0;
   RS.SaveToFile('c:\test.png');
 finally
   RS.Free;
 end;
end;
If the "test.png" is created, there is no error and test is a valid png file, then your resource is working as it should.
But don't try to stick it into the bitbutton glyph property, use bitmaps for that purpose.
If you still get a resource error, please attach the .res file.
Reply With Quote
  #6  
Old 06-11-2014, 08:29 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi and thanks for the reply.

Yes the test.png gets created and no errors are given, Now I know the approach.

Thanks you so much for all your help.

Best regards
Reply With Quote
  #7  
Old 06-11-2014, 08:42 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You're welcome delphimpd!

Download PngComponents from http://cc.embarcadero.com/item/26127, all you need is a free membership. Install the component, drop a TPngBitBtn to your form.

Code:
procedure TForm1.PngBitBtn1Click(Sender: TObject);
var
 RS: TResourceStream;
begin
  RS := TResourceStream.Create(hInstance, 'Bitmap1', 'PNG');
  try
    RS.Position := 0;   
    PngBitBtn1.PngImage.LoadFromStream(RS);
    PngBitBtn1.Invalidate;
  finally
    RS.Free;
  end;
end;
I just tested this, and it's working fine.
Reply With Quote
  #8  
Old 06-11-2014, 08:55 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Here is a demo application, but first install the PngComponents.
Attached Files
File Type: zip Resources.zip (93.4 KB, 1 views)
Reply With Quote
  #9  
Old 06-12-2014, 01:47 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Good Morning Kolbasz.

Thank you so much for the help and the zip file. I was able to get everything going. I am pretty sure that your help will help a lot of people, experienced and newcomers.

Thank you again for your support.

Best regards
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 06:02 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.