Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - How to load a small system icon?

I need to display 16x16 pixel icons for error/warning/information. Unfortunately both LoadIcon(0, IDI_*) and LoadImage(0, OIC_*, IMAGE_ICON, 16, 16, LR_SHARED) always give me the 32x32 version of the icon.

I read about ShGetStockIconInfo but that is only available from Vista onwards and I still need to support XP.

Any ideas?

I'm using Delphi 2010 with a TImage component if that matters.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that when you do it this way you get a cached version of the icon, the first one that the system loaded. That will be the large sized icon, typically 32x32. It matters not what size you specify.

What you can do is find the ID of the desired resource in user32.dll and use something like this:

LoadImage(GetModuleHandle('user32'), MAKEINTRESOURCE(103), IMAGE_ICON,
    16, 16, LR_DEFAULTCOLOR);

You would be better to call GetSystemMetrics(SM_CXSMICON) to get hold of the icon size rather than to hard code 16, but you probably already know that.

I'm not sure where you get the resource IDs from for the resources in user32, or even if they is any guarantee that they will stay constant across different Windows versions. My guess is that they will because too many programs would break, but that's just pure guesswork.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...