Delphi form icons are blurry on Windows 7's taskbar (with MainFormOnTaskbar enabled)

回眸只為那壹抹淺笑 提交于 2019-11-29 21:55:19

The problem lies within lazy programming within the VCL not fitting with the behavioral change of the OS. More or less it is like this;

TCustomForm.CreateWnd, after the window handle is created, calls;

  SendMessage(Handle, WM_SETICON, 1, LPARAM(GetIconHandle)) else

Notice the "1" in place of wParam, that's ICON_BIG. Actually the VCL sets the large icon of the form. But the icon's requested size (TIcon.FRequestedSize) is 16x16 (by default), and so the TIcon of the form returns a handle for the small icon. That's the size for the system small icon and is determined in the constructor CreateNew with calls to GetSystemMetrics.

Since earlier versions of Windows used the small icon on the taskbar this was no problem. Hovewer the Alt+Tab dialog had the problem other way around; if an icon was assigned to a form it showed "blurred" in the Alt+Tab dialog. Anyway, Windows 7, still, by default returns 16x16 for the small icon (SM_CXSMICON/SM_CYSMICON) and 32x32 for the large icon (SM_CXICON/SM_CYICON), but the large taskbar displays the large icon, if there is one that is..

The correct approach would be to assign a large image (if there is one in the icon) for the large icon and assign a small image (if there is one) to the small icon. Granted, since the sizes would not have to have exact matches, this would require a complex algorithm. Instead, a simpler but broken design is implemented.


For a workaround, I don't assign an icon to the forms in the OI and instead use this;

procedure SetFormIcons(FormHandle: HWND; SmallIconName, LargeIconName: string);
var
  hIconS, hIconL: Integer;
begin
  hIconS := LoadIcon(hInstance, PChar(SmallIconName));
  if hIconS > 0 then begin
    hIconS := SendMessage(FormHandle, WM_SETICON, ICON_SMALL, hIconS);
    if hIconS > 0 then
      DestroyIcon(hIconS);
  end;
  hIconL := LoadIcon(hInstance, PChar(LargeIconName));
  if hIconL > 0 then begin
    hIconL := SendMessage(FormHandle, WM_SETICON, ICON_BIG, hIconL);
    if hIconL > 0 then
      DestroyIcon(hIconL);
  end;
end;

and include an "icons.res" with named icons having 16x16 and 32x32 images, in the project. All the forms in their OnCreate call

 SetFormIcons(Handle, 'MYFORM', 'MYFORM');

It can be a bit of a nightmare getting this right. I have found that the most successful strategy is to place a very simple icon on the main form and application, and then to incorporate the ReplaceVistaIcon program into the build workflow to replace the icon with your multiple-icon file after the build is complete (and before signing the exe). This seems to place the icon correctly so that Windows picks it up in preference to any other icon resource. It's a nuisance to have to do this, but once set up (in our FinalBuilder project) it works for us.

The annoying problem, while you are testing this, is that you may have to delete the Windows icon cache to see the effect of any changes. This involves shutting down Explorer to allow you to delete the cache file from a command session.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!