Change icon of my app in runtime

contains all delphi packages mentioned below
Post Reply
carlos_am_2003
Posts: 4
Joined: Sat Jul 08, 2023 1:57 pm

Change icon of my app in runtime

Post by carlos_am_2003 »

Hi all!

Is possible using mad api change the icon of my app in the runtime?
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Change icon of my app in runtime

Post by madshi »

Do you mean you want Windows to change the icon that is shown in the top left corner of your main form, without changing anything on harddisk?

Or do you mean you want to modify your own EXE file on harddisk to replace the icon?
carlos_am_2003
Posts: 4
Joined: Sat Jul 08, 2023 1:57 pm

Re: Change icon of my app in runtime

Post by carlos_am_2003 »

Hi madshi,

Thanks for your reply!

I want to modify my own EXE file on harddisk to replace the icon. But my app is running :( :( Or using another app to replace the icon.
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Change icon of my app in runtime

Post by madshi »

You can't do that while your EXE is running. It's locked for write-access in that moment. What you probably could do is rename your running EXE file (Project1.exe -> Project1old.exe), then copy it to the old name (Project1old.exe -> Project1.exe). Windows usually allows that, and the running EXE is then Project1old.exe, and then it will be able to modify Project1.exe.

You can use the APIs in "madRes" to change the icon.
carlos_am_2003
Posts: 4
Joined: Sat Jul 08, 2023 1:57 pm

Re: Change icon of my app in runtime

Post by carlos_am_2003 »

Hi!

Do you have any simples sample to show me?
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Change icon of my app in runtime

Post by madshi »

No, sorry. But ChatGPT is your friend. Maybe it will even write the code for you.
carlos_am_2003
Posts: 4
Joined: Sat Jul 08, 2023 1:57 pm

Re: Change icon of my app in runtime

Post by carlos_am_2003 »

Hi again!

ChatGPT wrote this:

Code: Select all

uses
  MADRes;

procedure ChangeExeIcon(const FileName, IconPath: string);
var
  Icon: TIcon;
  ResUpdater: TCustomResUpdater;
begin
  Icon := TIcon.Create;
  try
    Icon.LoadFromFile(IconPath);

    ResUpdater := TCustomResUpdater.Create(FileName);
    try
      if ResUpdater.UpdateIcon(Icon) then
        ShowMessage('updated icon')
      else
        ShowMessage('Error');
    finally
      ResUpdater.Free;
    end;
  finally
    Icon.Free;
  end;
end;

Where can I found the class:
TCustomResUpdater ?
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: Change icon of my app in runtime

Post by madshi »

I don't know, probably ChatGPT lied, as usual... :(
Post Reply