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

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.

Related

How can I use ALL system registered themes/styles in my Delphi App?

With Vcl.Themes.TStyleManager.StyleNames, I have access to just the style names enabled in the project options (Application/Appearance).
How can I list all of the style names registered in the system, and make it active in Delphi 11?
This is how I fill a TComboBox with the available styles:
procedure FillComboboxWithStyles;
var
stylename: string;
begin
for stylename in TStyleManager.StyleNames do
CBVclStyles.Items.Add(stylename);
end;
To activate a certain style you fill TStyleManager.SetStyle(const Name: string) with the stylename. For example the selected one from the TComboBox.
if (CBVclStyles.ItemIndex >= 0) then
TStyleManager.SetStyle(CBVclStyles.Items[CBVclStyles.ItemIndex])
else
;// handle a non-selected style
Don't forget Vcl.Themes in your uses.
You can include the style files (*.vsf from C:\Program Files (x86)\Embarcadero\Studio\22.0\Redist\styles\vcl with 22.0 marking your RAD Studio version) along with (externally from) your .EXE and then iterate over them when you fill your ComboBox. Then manually load the style from the file with TStyleManager.LoadFromFile when selected.
This will also allow your user to add his own style files to the setup.
This, I believe, is what the IDE does (except that it reads the list directly from the folder, since there's no "installation" of a style other than making the file available in a recognized location).

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

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');

How to get the shell folder icon location for a specific folder?

In Delphi XE7, I need to get the LOCATION of the icon used by Windows Explorer to display a folder, especially in thumbnail view. So I use this code:
var
Shell32DllFilePath: string;
// Get shell32.dll FilePath:
Shell32DllFilePath := IncludeTrailingPathDelimiter(JclSysInfo.GetWindowsSystemFolder) + 'shell32.dll';
if not FileExists(Shell32DllFilePath) then
begin
Shell32DllFilePath := '';
// Todo: log this error
end;
Then I ASSUME that the default folder icon is the icon with IconIndex 3 in shell32.dll:
However, this gives me a static image for every folder, while Windows Explorer uses different folder icons according to the folder location and/or the folder content.
So how can I get the exact folder icon location (icon dll file and icon index) Windows Explorer uses for any SPECIFIC folder?
There may not be a file path to an icon with the way Windows works. You mention "especially in thumbnail view", which means images and icons (within the folder) are stacked together in the folder icon. This does not save a file anywhere, so you can't load from any file.
I'm assuming this is the type of icon you're talking about:

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.

Including resource file in a project by .RC file rather than .RES file

I remember reading an article or post somewhere years ago that suggested including a resource file in a project by referencing the .rc file instead of an already compiled .res file so that the resource is built as part of the project's build process.
I have a glyphs.rc file that I currently compile using the command brcc32 glyphs.rc. In my project file I then have the statement {$R Glyphs.res}.
I'd like to simplify this by changing it to something like
{$R Glyphs.rc}
but am unsure of the syntax. When I try using {$R Glyphs.rc} I get an error `
[DCC Error] E2161 Error: RLINK32: Unsupported 16bit resource in file "Glyphs.rc".
Is this approach possible with Delphi 2007?
Just add the rc file to your project via the "Project > Add to project" menu item. This creates the {$R 'myres.res' 'myres.rc'} line from the posting that TOndrej links to.
The linker can only handle res files, but you can direct the compiler to invoke the resource compiler and compile an rc script to produce a res file and link that, using a variation of the $R/$RESOURCE directive.
In your case (Delphi 2007) you should need only change:
{$r glyphs.res}
to
{$r glyphs.res glyphs.rc}
If this doesn't work on its own, try adding the RC to the project. In different versions of Delphi you may need single quotes around the filenames:
{$r 'glyphs.res' 'glyphs.rc'}
NOTE: You do still need to identify a res file, the difference is in being able to additionally identify the rc file to be compiled in order to produce the required res file in the first place.
Support for this appears to have been subject to some tinkering and in more recent versions adding the RC to the project does not always seem to be "detected" by the project until after you have then saved, closed and re-opened the project (e.g. I found this to be the case in XE4 but may also apply to other versions).
Also in some more recent versions, simply adding such a $R 'file.res' 'file.rc' declaration to the DPR causes the Project Manager to identify the referenced RC file as part of the project, but this does not seem to be the case in older versions. Again, part of the tinkering in this area it seems.
I would also note the XE4 is usually rock solid in terms of stability, but adding/removing RC files seemed to trigger an access violation when closing the IDE, though normal stability seemed to be restored when re-opening the IDE and project. i.e. it is adding/removing RC files that seems to cause a problem, not simply the fact of having the RC file in the project.
UPDATE: In recent versions of Delphi (Delphi 10.2 Berlin) you should include custom resources before {$R *.res} line, otherwise they will not be automatically compiled.
See an example here: "How do I make a PNG resource?".
I had the same problem and found out something new.
{$R glyphs.res glyphs.rc}
this is the code for compiling glyphs.rc to glyphs.res in the pre-build. (Works with Delphi XE4)
But this code ONLY works if it is in the *.dpr file! If you place this code, in a *.pas file as I did the first time, it will simply behave like {$R glyphs.res} and will not compile the RC file. Maybe this is a bug in Delphi.
I tried to do this in Delphi 2007 and it didn't work. I had put the line,
"{$R glyphs.res glyphs.rc}"
in my project file right above the "{$R *.res}" line that the IDE puts in there but when I added the rc file using the IDE, it put it above the "uses" line and then it worked.
I could not get rid from the mainicon in my application, so i made an trapgate.rc file put that file in the src directory, used:
MAINICON icon ".\Icon\MAINICON.ico"
5012 icon ".\Icon\5012.ico"
Then used BRCC32 to make from the RC a RES file, did the build and i had the correct icon.
you can also put more icons in there and switch thats why i added the line in makeres.bat
looks like this :
brcc32 folders.rc -fofolders.res
brcc32 main.rc -fomain.res
brcc32 xOutline.rc -foxOutline.res
brcc32 xSpin.rc -foxSpin.res
brcc32 credits.rc -focredits.res
brcc32 licence.rc -folicence.res
brcc32 trapgate.rc -fotrapgate.res <-- this is my icon file
So whatever you do even if you change the icon in the folder ..\icons of course be sure it has the correct name like mainicon.ico and 5012.ico
Hope that did help for does who can't change the icon in Delphi 7 itself.

Resources