Forum > Windows

Trying to do a resource update for icons and I'm nearly there ...

(1/4) > >>

Spoonhorse:
I have become enlightened. Will either update, or this is the best prank ever on the next person who googles this topic. Or I died of a heart attack.

ETA: my solution is in post #14, in return be nice to someone today.

Spoonhorse:
P.S. it looks like whoever wrote this understands the whole thing perfectly so maybe I'll re-use their code. I'd like to understand it better though.

https://stackoverflow.com/questions/35189283/change-icon-of-application-on-runtime

wp:
Why are you doing it in the most complicated way? The easiest way is to open the project options ("Project" > "Project Options" > "Application") and to load the icon by using the "Load Icon" button. Then Lazarus takes care of everything.

Or do you want to replace the icon while the program is running? In this case, you simply can set "Application.Icon := OtherIcon", or in more detail:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    OtherIcon: TIcon;   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  Application.Icon := OtherIcon;end; procedure TForm1.FormCreate(Sender: TObject);begin  OtherIcon := TIcon.Create;  OtherIcon.LoadFromFile('C:\Lazarus\lazarus-trunk_fpc-3.2.0\images\icons\brown.ico');  // or load an icon from resourceend; procedure TForm1.FormDestroy(Sender: TObject);begin  OtherIcon.Free;end;  

Spoonhorse:
Thank you for the advice, wp, but what if I do not have access to the source code of executable.exe? What if it is not written in Pascal?

Spoonhorse:
The trouble with the code I found on Stack Overflow is that the guy's copying it from one .exe file to another rather than from an .ico file and that's no good to me.

Here's a fresh version of my code. It is now even closer, if I look at the updated version in Resource Hacker it tells me that the group has nine icons, which is true, that they're 16.8 million color, which is true, that they're all 16 x 16, which is not true, and that it can't actually show me what they look like, which is annoying. And that they all have the ordinal name 150.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var   vResHandle: THandle;      MyIcon: TMemoryStream;begin  // Get the icon.  MyIcon := TMemoryStream.Create;  MyIcon.LoadFromFile('icon.ico');  // Set the position in the memory stream to the start.  MyIcon.Seek(0, soFromBeginning);   // Get the handle.  vResHandle := BeginUpdateResource('exec.exe', False);  if vResHandle=0 then    raise Exception.Create('System giving error message: '                           + SysErrorMessage(GetLastError));    try    // Change the icon.    if not UpdateResource(vResHandle                          , RT_GROUP_ICON                          , PChar('MAINICON')                          , LANG_NEUTRAL                          , MyIcon.Memory                          , MyIcon.Size)    then      raise Exception.Create('System giving error message: '                             + SysErrorMessage(GetLastError));    finally    EndUpdateResource(vResHandle, False);    end;  MyIcon.Free;end;  

Navigation

[0] Message Index

[#] Next page

Go to full version