Einzelnen Beitrag anzeigen

Benutzerbild von Uncle Cracker
Uncle Cracker

Registriert seit: 30. Mär 2003
Ort: Freital
694 Beiträge
 
#1

Icon ersetzen funktioniert nicht

  Alt 16. Jul 2004, 11:04
Ich habe jetzt 2 Methoden probiert das Icon einer anderen selbsterstellten Delphi Anwendung zuändern, aber keine davon hat funktioniert, was muss ich denn noch machen?

Hier mal die beiden Funktionen:

Delphi-Quellcode:
type
  PICONDIRENTRYCOMMON = ^ICONDIRENTRYCOMMON;
  ICONDIRENTRYCOMMON = packed record
    bWidth: Byte; // Width, in pixels, of the image
    bHeight: Byte; // Height, in pixels, of the image
    bColorCount: Byte; // Number of colors in image (0 if >=8bpp)
    bReserved: Byte; // Reserved ( must be 0)
    wPlanes: Word; // Color Planes
    wBitCount: Word; // Bits per pixel
    dwBytesInRes: DWord; // How many bytes in this resource?
  end;
  PICONDIRENTRY = ^ICONDIRENTRY;
  ICONDIRENTRY = packed record
    common: ICONDIRENTRYCOMMON;
    dwImageOffset: DWord; // Where in the file is this image?
  end;
  PICONDIR = ^ICONDIR;
  ICONDIR = packed record
    idReserved: Word; // Reserved (must be 0)
    idType: Word; // Resource Type (1 for icons)
    idCount: Word; // How many images?
    idEntries: ICONDIRENTRY; // An entry for each image (idCount of 'em)
  end;
  PGRPICONDIRENTRY = ^GRPICONDIRENTRY;
  GRPICONDIRENTRY = packed record
    common: ICONDIRENTRYCOMMON;
    nID: Word; // the ID
  end;
  PGRPICONDIR = ^GRPICONDIR;
  GRPICONDIR = packed record
    idReserved: Word; // Reserved (must be 0)
    idType: Word; // Resource type (1 for icons)
    idCount: Word; // How many images?
    idEntries: GRPICONDIRENTRY; // The entries for each image
  end;
function UpdateApplicationIcon(icofile: PChar; exefile: PChar; resID:PcHar; LangID:DWORD): Boolean;
var
  hInst,hFile: THandle;
  id: ICONDIR;
  pid: PICONDIR;
  pgid: PGRPICONDIR;
  uRead: DWord;
  nSize: DWord;
  pvFile: PByte;
begin
  result := False;
  hFile := CreateFile(icofile, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if hFile <> INVALID_HANDLE_VALUE then
  try
    ReadFile(hFile, id, sizeof(id), uRead, nil);
    SetFilePointer(hFile, 0, nil, FILE_BEGIN);
    GetMem(pid, sizeof(ICONDIR) + sizeof(ICONDIRENTRY));
    GetMem(pgid, sizeof(GRPICONDIR) + sizeof(GRPICONDIRENTRY));
    ReadFile(hFile, pid^, sizeof(ICONDIR) + sizeof(ICONDIRENTRY), uRead, nil);
    move(pid^, pgid^, sizeof(GRPICONDIR));
    pgid^.idEntries.common := pid^.idEntries.common;
    pgid^.idEntries.nID := 1;
    nSize := pid^.idEntries.common.dwBytesInRes;
    GetMem(pvFile, nSize);
    SetFilePointer(hFile, pid^.idEntries.dwImageOffset, nil, FILE_BEGIN);
    ReadFile(hFile, pvFile^, nSize, uRead, nil);
    hInst := BeginUpdateResource(exefile, False);
    if hInst <> 0 then
    try
      result:=UpdateResource(hInst, RT_ICON, resID, LangID, pvFile, nSize);
    finally
      EndUpdateResource(hInst, False);
    end;
    FreeMem(pvFile);
    FreeMem(pgid);
    FreeMem(pid);
  finally
    CloseHandle(hFile);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if UpdateApplicationIcon('Test.ico', 'Test.exe', MAKEINTRESOURCE(1), 1031) then
  Showmessage('Icon successfully updated') else Showmessage('Icon update failed Errorcode:', GetLastError);
end;
Als Fehlercode habe ich bei der oberen Funktion '0' erhalten.
Hier meine andere Methode, diese funktioniert ebenfalls nicht

Delphi-Quellcode:
function ErsetzeICO(Datei, ICO: string): Boolean;
var
   stream : TFilestream;
   hInst: THandle;
   ptr : Pointer;

begin
  result := false;
   Stream := TFileStream.Create(ICO,fmOpenRead);
   getmem (ptr,Stream.size+1);
   try
      Stream.Seek(0,sofrombeginning);
      stream.read(ptr^,Stream.size);
      hInst:=BeginUpdateResource(Pchar(Datei), true);
      if hInst > 0 then
         begin
         UpdateResource(hInst, RT_Icon, MAKEINTRESOURCE(1),
         1031,ptr,Stream.size);
         EndUpdateResource(hInst, False);
         result := true;
         end;
   finally
      freemem (ptr,Stream.size+1);
      stream.Free;
   end;
end;

Weiß vielleicht jemand, warum das Icon der EXE nicht ersetzt wird? Oder kennt vielleicht noch jemand eine andere Funktion zum ersetzten eines Icons?


Danke UC
I wish it was legal to marry software because I'm madly in love with Delphi...
  Mit Zitat antworten Zitat