How cand I include a bitmap in my custom component (if is possible)? - delphi

I am writing a component (a button) which needs a bitmap to be displayed on it. I don't want to make an ImageList property and the user assigns an image. I want that button to have only the image chosen by me.
I tried to include the bitmap in a resource file but when I try to access it I get "Resource not found" error message. This is what I've done:
myres.rc
FIXED BMP "fixed.bmp"
I compiled the resource file with: brcc32 myres.rc
Then I included it in my component unit...
implementation
{$R .\resources\myres.res}
And access it with...
MyComponent.Glyph.LoadFromResourceName(HInstance,'FIXED');
// MyComponent = class(TSpeedButton)
Edit1:
I deleted the {$R .\resources\myres.res} directive and I loaded the resource from menu Project -> Resources and it's working, both with HInstance or FindClassHInstance(MyComponent).
Using a resource editor I found that when I load the resource from the menu the resource appears with the name "FIXED" as it should, but when I load the resource compiled with brcc32 it appears with the name "0". It seems that brcc32 doesn't set the name correctly.
But I don't want to load it from menu, I want it to be loaded automatically with the component.
Edit2:
Remy Lebeau is correct. I was using a wrong BMP format (the file starts with 'BM6' characters instead 'BM8' like Photoshop produce it, and it works).

Change BMP to BITMAP in your RC file, and change HInstance to FindClassHInstance() in your code:
FIXED BITMAP "fixed.bmp"
Glyph.LoadFromResourceName(FindClassHInstance(MyComponent), 'FIXED');

Related

How to Put Icones in RadStudio XE8 IDE palette for new component?

I wrote a set of few components named :
- TUser
- TRESTAccess
- TServerAccess
Then i create 3 PNG image 100x100 with same name than component (in the same directory than the BPL and the .PAS files)
I wrote a .RC file include in my package.
But can't compile : invalid format ?
does someone have any ideas how can i make the icones appear
in the component palette ?
No need to compile BRCC32
I used image 128x128 BMP format.
I click on Project / Ressource and Image
I added the image and rename identifier as the name of my component
Then uninstall package (then closed Rad Studio XE8, if not i get and access violation)
Then install my package again.
IT WORKS ! (except transparency : in the palette no transparency applied, but if i drop the component in my form, the transparency is applied !)

Adding a .res file to project replaces the default icon.How to prevent it?

I needed to add some icons to my project as a resource (I can't use a TImageList in this case, because of a bug in TCoolTrayIcon, and I can't replace the component quickly).
I've created an icons.rc script to add the two ico files to a Delphi resource file:
redicon ICON "c:\icon\red.ico"
greenicon ICON "c:\icon\green.ico"
it compiles fine to icons.res, so I add it to the first unit of my Delphi 7 project:
{$R icons.res}
then I store the handles in OnCreate() of the MainForm:
hRedIcon := LoadIcon(hInstance,'redicon');
hGreenIcon := LoadIcon(hInstance,'greenicon');
and then use the handles just fine.
Now to the problem - after doing that the project icon that was added in the project options (in sizes of 16x16 to 48x48) is replaced by the first icon (16x16 redicon) I've added in {$R icons.res}.
How to prevent this? How to add additional icons to a project as a resource without replacing the icon that is added in Project Options -> Application -> Load Icon?
The VCL hard codes the name 'MAINICON' for your application's icon. This can be seen in the code in TApplication.Create:
FIcon.Handle := LoadIcon(MainInstance, 'MAINICON');
On the other hand, the shell assumes that the first icon in your executable is the main application icon. The order that the shell uses is alphabetical by icon name.
The consequence of this is that all your icons should have names that appear after MAINICON in the alphabet.

How do I use a custom AVI with TAnimate?

I have an AVI file that I pulled from shell32 using Resources Extract. I would like to use this with TAnimate but I can't figure out how to load this file.
I succesfully loaded the AVI into a .RES file using DelphiDabbler's rcdatacreator program (you have to download the "worked example" to get rcdatacreator. However, my issue now is figuring out how to extract the AVI file from the .RES and supplying it to TAnimate.
I am using Delphi 2010:
Any help is appreciated.
As Andreas mentioned (in his now deleted answer), you don't need to use an external tool to add the resource in recent versions of Delphi.
Use Project/Resources and Images... from the IDE menu. Add a new resource by browsing to the folder your .AVI file is in, give it a name, and type in AVI for the Resource Type. (It's not in the list, but you can add it.)
At runtime, use the following code:
// I used CoolAVI as the resource name in my image above,
// so that's the name I need to use here.
Animate1.ResName := 'COOLAVI';
Animate1.Active := True;

Resource png not found

i make a png resource file named glyfs.rc
GLYF_CONFEDITOR RCDATA confeditor.png GLYF_EXTRAFE RCDATA extrafe.png .......
i add it in my project.
Put a TsBitBtn (it is an alphaskin component class) and try to load the glyf from resource file
sbitbtn1.Glyph.LoadFromResourceName(HInstance,'GLYF_CONFEDITOR');
when i run i get the error resource file with name 'GLYF_CONFEDITOR' not found.
What am i doing wrong?
The 3 party tool i use for png is pngdelphi downloaded from here
But the sbitbtn loads native the png files...
There are multiple problems with your code:
TBitBtn.Glyph is of type TBitmap and TBitmap.LoadFromResourceName assumes RT_BITMAP resource type, not RT_RCDATA, hence "resource not found" error.
Even if you use RT_BITMAP it will throw EInvalidGraphic or similar¹ because again - TBitBtn.Glyph is TBitmap and naturally TBitmap wont load PNG data.
¹ there is WinAPI function LoadImage behind LoadFromResourceName, actual error message may vary.

How I Compile Resources into my Application and Access them?

How can I make a single executable package that contains DLL and Image Resource Files?
Then how do I extract them from my Executable at Runtime?
Option 1 using the IDE (Delphi 2007 or Higher):
You can click the Project menu, then select Resources..., which you can load any file into. For your purpose this would be RC_DATA.
Option 2 without the IDE
If you do not have the above option, you will need to use the BRCC32 (Borland Resource Compiler) to create a .RES file from RC file, which you then link to your Application. To link Resource files without using the IDE, try the following:
Lets say for example we want to add a a couple of DLL files, and the name of the DLL files are MyLib1.dll and MyLib2.dll, to add this open Notepad, and type the following:
MYLIB1 RCDATA "..\MyLib1.dll"
MYLIB2 RCDATA "..\MyLib2.dll"
Make sure the ..\xxx.dll paths are correct, so obviously you need to edit that.
Now you need to save this as a .rc file, so File>Save As..(make sure the dropdown filter is All Files .) and name it MyResources.rc. Now you need to use the Resource Compiler to generate the Res file, using this console command:
BRCC32 MyResources.RC
You can write that command by using the Command Prompt, Start Menu > Run > cmd.exe, alternatively you can find the BRCC32.exe inside the bin folder of your Delphi setup and drag the MyResource.RC file onto.
This will create a Res file named MyResources.RES which you can include inside the Main Delphi form of your Application, like so:
{$R *.dfm}
{$R MyResources.res}
you can extract the resources by using something like this:
procedure ExtractResource(ResName: String; Filename: String);
var
ResStream: TResourceStream;
begin
ResStream:= TResourceStream.Create(HInstance, ResName, RT_RCDATA);
try
ResStream.Position:= 0;
ResStream.SaveToFile(Filename);
finally
ResStream.Free;
end;
end;
What I've found out to be convenient, is to use a .zip container.
Then you'll have two implementations:
Append some .zip content to an existing .exe, and the .exe code will retrieve the .zip content on request;
Embed the .zip content as a resource, then extract on request each content.
Solution 1 will add the .zip content after compilation. Whereas 2 will add the .zip content at compilation. For a setup program, I think solution 1 makes sense to me. For a way of retrieving some needed files (libraries, and even bitmaps or text) which are linked to a particular exe release, solution 2 could be envisaged.
Using .zip as format make it easy to parse the content, and allow compression. Using a tool like TotalCommander, you can even read the .zip file content with Ctrl+PgDown over the .exe. Very convenient.
You'll find in this link how you implement solution 1, and in this link (same page, but another post) how to use the TZipRead.Create() constructor to directly access to a .zip bundled as resource. You'll find in our repository how it works with working applications: e.g. how we embedded icons, textual content and graphviz + spell-checker libraries in the SynProject executable.
About performance, there is no difference between the two solutions, at least with our code. Both use memory mapped files to access the content, so it will be more or less identical: very fast.

Resources