Why is my icon ugly on a tray with TTrayIcon?

I recently discovered the TTrayIcon component in Delphi 2007. The code used is pretty simple.

procedure TForm1.FormCreate(Sender: TObject); begin AppTrayIcon := TTrayIcon.Create(nil); AppTrayIcon.OnDblClick := OnAppTrayIconDblClick; Application.OnMinimize := OnApplicationMinimize; Application.OnRestore := OnApplicationRestore; end; procedure TForm1.OnApplicationRestore(Sender: TObject); begin AppTrayIcon.Visible := False; ShowWindow(Application.Handle, SW_SHOW); Application.BringToFront; end; procedure TForm1.OnApplicationMinimize(Sender: TObject); begin AppTrayIcon.Visible := True; ShowWindow(Application.Handle, SW_HIDE); end; procedure TForm1.OnAppTrayIconDblClick(Sender: TObject); begin Application.Restore; end; 

Since there is no icon assigned, Delphi uses Application.Icon, which is this icon: http://artbyloveland.com/icon.ico This icon contains the following sizes: 64x64, 48x48, 32x32, 24x24 and 16x16.

Now, on my Windows Vista, everything is fine.

On non-thematic Windows, such as Windows Server 2003, the result is spoiled:

Screwed-up icon

EDIT: At first I thought it was due to the alpha channel. So I tried to make the ico file version without using the alpha channel. I also tried the GreenFish icon editor suggested by Ken; I selected every color depth and every available size. In both cases, the end result is better. However, there is a black stroke that does not exist at all in the ico file.

Screwed-up icon 2

+8
delphi alpha blur tray
source share
3 answers

You declare that you are not assigning a badge. In this case, the component uses Application.Icon . But usually it will be an icon that is the wrong size for the notification area.

For the notification area, you need to use a square icon with a size determined by the metric of the SM_CXSMICON system. The best way to get this is to call LoadImage , which allows you to specify the size of the icon. Once you have loaded the icon in HICON , you can simply write this:

 AppTrayIcon.Icon.Handle := IconHandle; 
+8
source share

You do not have the right size or color depth for your icon.

You can use the icon editor to display icons of several sizes and color depths in a single .ico file, and Windows will automatically choose the right one depending on the user’s settings and video driver configuration. Windows will then have several choices when choosing the closest match, and scaling and blending will have a much better look.

I am using the GreenFish Icon Editor , which is a donation. This will allow you to open any supported graphic type, and then automatically create a Windows icon with several color depths and resolutions (see Icon menu). I tested the icon files of several images in Delphi 7, 2007, 2010, XE and XE3, and they work great for Application.Icon and TForm.Icon .

Also see Best icon size to display in the tray.

+6
source share

I thought that I would share my solution to this problem, since there is currently no complete solution.

This problem drove me crazy because it is actually clearly a Delphi / VCL bug. If you assign an icon with all the necessary sizes (16, 24, 32, 48, 256) to your project, Delphi should automatically use the correct size in TTrayIcon, but instead it takes only the 32px icon and reduces it.

Since the necessary images are already in the executable file (for display in Windows Explorer), you can simply fix it like this:

 procedure FixTrayIcon(TrayIcon: TTrayIcon); var i: Integer; begin i := GetSystemMetrics(SM_CXSMICON); //Gets the correct size for the tray (eg 16) TrayIcon.Icon.Handle := LoadImage(hInstance, 'MAINICON', IMAGE_ICON, i, i, LR_DEFAULTCOLOR); TrayIcon.SetDefaultIcon; //Updates the icon end; 

Just call FormCreate and your tray icon will look as intended.

0
source share

All Articles