开发者

Delphi Shell IExtractIcon usage and result

I am developing a listview baised on the shell. When trying to extract a shell icon/image... I try to extract thumbnail using IExtractImage if that fail开发者_如何转开发 I try to extract icons using IExtractIcon, to get maximum iconsize, but IExtractIcon gives strange results. Problem is I tried to use a methode that extracts icons from an imagelist but if there is no large icon (256x256) it will render the smaller icon at the topleft position of the icon and that does not look good. That is why I am trying to use the IExtractIcon instead. But icons that show up as 256x256 icons in my imagelist extraction methode reports icon sizes as 33 large and 16 small. So how do I check if a large (256x256) icon exists? If you need more info I can provide some sample code.

if PThumb.Image = nil then
begin
  OleCheck(ShellFolder.ParseDisplayName(0, nil, StringToOleStr(PThumb.Name), Eaten, PIDL, Atribute));
  ShellFolder.GetUIObjectOf(0, 1, PIDL, IExtractIcon, nil, XtractIcon);
  CoTaskMemFree(PIDL);
  bool:= False;
  if Assigned(XtractIcon) then
  begin
    GetLocationRes := XtractIcon.GetIconLocation(GIL_FORSHELL, @Buf, sizeof(Buf), IIdx, IFlags);
    if (GetLocationRes = NOERROR) or (GetLocationRes = E_PENDING) then
    begin
      Bmp := TBitmap.Create;
      try
        OleCheck(XtractIcon.Extract(@Buf, IIdx, LIcon, SIcon, 32 + (16 shl 16)));
        Done:= False;

Roy M Klever


Take your 256x256 bitmap and simply check for alpha. Make sure the bmp is 32bits. Any part that has 0 for the pixel value (that is BGRA 0,0,0,0, use TBitmap.Scanline to access) is transparent. You can find the smallest x and smallest y coord that has a nonzero value and that's the actual size of your icon. Now, this number may be smaller than the "icon size" as it was designed. For instance, a 16x16 icon can contain an image of 2x2, which would be a weird design.

But of course an 8x16 icon is very well possible. Considering the images are always square, take the max if the x and y coordinates found (with and height of actual image) and round it up to the nearest of 16,24,32,48,64,128 or 256. You can be quite certain that you'll have the icon centered in your bitmap if you crop to this size next. Use Bmp.Width=sz; Bmp.Height=sz;. You could then scale it up, or center it onto your standard bitmap (56x56? 256x256?).

So, even if windows doesn't feel like giving you the right info, you can bypass the need for this info by simply finding out yourself. This may not be the "correct" way to do it, but you'll know that it'll work when you're done, as opposed to browsing MSDN for 4 hours and never finding an answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜