Talk:How to use a TrayIcon

From Free Pascal wiki
Revision as of 20:02, 24 July 2010 by Dieselnutjob (talk | contribs) (New page: Could someone expand on this comment "The image of the icon can be altered using a HICON handle. "? As a newbie I don't really know what a HICON handle is or how to use it. I have found i...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Could someone expand on this comment "The image of the icon can be altered using a HICON handle. "? As a newbie I don't really know what a HICON handle is or how to use it.

I have found it quite hard to generate icons that will load and display using

SysTrayIcon.LoadFromFile('file.ico');
SysTrayIcon.Show;

I found that the format of the icon in Windows XP is very fussy. The icon will only display if it contains a 32x32 image saved with the parameters "8bpp,1-bit alpha,256-slot palette" in gimp for example.

The next challenge is to modify an icon in memory and then display it. I'm sure my method isn't exactly correct but maybe it will help others. First I tried to create a tbitmap and load an icon into it but fails. Also any attempt to do a SysTrayIcon.Icon.LoadFromBitMapHandles() seems to result in a blank icon.

In the end I did this which works

MemIcon:=TIcon.Create;
MemIcon.LoadFromLazarusResource('blankicon');
PByte:=MemIcon.RawImage.Data+100;
//PByte now points to the first byte of the first pixel.
//Set the top left pixel red
Pbyte^:=$00; //green
(PByte+1)^:=$FF;  //red
(PByte+2)^:=$00:  //blue
SysTrayIcon.Icon:=MemIcon;
SysTrayIcon.Show:

Now I have an icon in my system tray with a red dot in the top left corner. Each pixel uses 3 bytes so a procedure like this works to manipulate each 32x32 pixel

procedure TForm1.SetIconPixel(PTRIcon:PIcon;xpos,ypos,red,green,blue:byte);
var
  PByte: ^Byte;
begin
  {xpos and ypos are 0 to 31}
  PByte:=PtrIcon^.RawImage.Data+100+((xpos+(ypos*32))*3);
  if PByte<PtrIcon^.RawImage.Data+PtrIcon^.RawImage.DataSize-1 then
  begin
    Pbyte^:=green;
    (PByte+1)^:=red;
    (PByte+2)^:=blue;
  end;
end;

I have no idea what the first 100 bytes do.

The last annoyance is that other icons in my system tray appear to be 16x16 yet I can't get SysTrayIcon to display a 16x16 icon (maybe I haven't tried hard enough). Windows seams to scale my 32x32 icons down to fit which means a softer icon with missing pixels :-( Still, at least it works and I can programmatically modify them dieselnutjob