Search code examples
winapilazarus

How to draw on a TTrayIcon


I'm trying to draw on the canvas of a TTrayIcon but have no success. I'm programming with Lazarus V1.6.2 on windows 10.

What I tried so far is:

procedure TForm1.TrayIcon1Paint(Sender: TObject);
var
  tmpcanvas: TCanvas;
begin
  tmpcanvas := TrayIcon1.Canvas;
  tmpcanvas := TrayIcon1.Icon.Canvas; // also not working
  tmpcanvas.Font.Size := 22;
  tmpcanvas.Brush.Color := RGBToColor(255,255,255);
  tmpcanvas.FillRect(1,1,200,200);
  tmpcanvas.Font.Color := RGBToColor(0,0,0);
  tmpcanvas.TextOut(1,1, 'TEST');
end;

I tried this code in the event Form1.OnPaint with the Form1.Canvas and there it works as expected.

But I have no luck with painting on the TTrayIcon.Canvas. It seems, that drawing on canvas of TTrayIcon is different from drawing on other canvases...

After some debuging I realized, that the TTrayIcon.OnPaint event is never triggered. But even when I force to execute TrayIcon1Paint(..) nothing happens.

Now I am out of ideas. Any help is highly appreciated.


Solution

  • The Windows notification icons do not offer any interface that would match an OnPaint event. I can only imagine that the OnPaint event is intended for use on different platforms.

    On Windows notification icons are provided to the system in the form of Windows icon objects. If you wish to change the appearance of your notification icon, you need to provide a new icon object. I'm not familiar with this particular wrapper of the Windows API function, but I would expect that you could write code like this to update the notification icon's appearance:

    TrayIcon1.Icon := MyNewIcon;