[madRes] change icon in exe

delphi package - madRes, madTools, madStrings, ...

[madRes] change icon in exe

Postby ciuly » Thu Oct 11, 2007 11:56 am

Hello,

I've been trying to help somebody on EE to use madRes to change the icon from an exe. the following code works fine if the icon group is something text, but if it's a number, then the icon group is duplicated.

bogus, input, app
Code: Select all
program Project1;

{$R *.res}
{$R icons.res}

begin
end.

icons.rc
Code: Select all
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
128 ICON "green.ico"
TEST ICON "green.ico"

actual test program
Code: Select all
program Project2;

uses
  windows, madres;

{$R *.res}

function MAKELANGID(sPrimaryLanguage : Word;
                    sSubLanguage : Word) : Word;
begin
  result := (sSubLanguage shl 10) or
             sPrimaryLanguage;
end;

function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then begin
    result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)));
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;

begin
  UpdateExeIcon('test.exe', '128', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
end.

if you use
UpdateExeIcon('test.exe', 'TEST', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
then the icon changes from green to red: no problems. but if you use it with '128', then the problem is reproducing.

using // madRes.pas version: 1.0j · date: 2005-07-15

any ideas?
ciuly
 
Posts: 62
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

Postby ciuly » Fri Oct 12, 2007 1:09 pm

after debugging madres, I found the solution
Code: Select all
program Project2;

uses
  windows,
  madres,
  sysutils;

{$R *.res}

function MAKELANGID(sPrimaryLanguage : Word;
                    sSubLanguage : Word) : Word;
begin
  result := (sSubLanguage shl 10) or
             sPrimaryLanguage;
end;

function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
    c:TPIconGroup;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then
  begin
    if GetIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, c) then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)))
                                                                                             else
    if StrToIntDef(iconGroup, -1)>-1 then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(pointer(strtoint(iconGroup))), language, PWideChar(wideString(icoFile)))
    else
      result := false;
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;

begin
  UpdateExeIcon('test.exe', '128', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
end.
ciuly
 
Posts: 62
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

Postby ciuly » Fri Oct 12, 2007 7:15 pm

and an update to support mainicon changin as well. provide proper language, of course.
Code: Select all
function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
    c:TPIconGroup;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then
  begin
    if (icongroup='MAINICON') or GetIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, c) then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)))
                                                                                             else
    if StrToIntDef(iconGroup, -1)>-1 then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(pointer(strtoint(iconGroup))), language, PWideChar(wideString(icoFile)))
    else
      result := false;
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;
ciuly
 
Posts: 62
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

Postby madshi » Sat Oct 13, 2007 7:50 am

Good to see you found it yourself... :)
madshi
Site Admin
 
Posts: 9412
Joined: Sun Mar 21, 2004 5:25 pm


Return to madBasic

Who is online

Users browsing this forum: No registered users and 1 guest

cron