Skip to content

Commit

Permalink
Icon: Switch to LoadIconWithScaleDown()
Browse files Browse the repository at this point in the history
Legacy LoadImage() picks a smaller icon variant and scales it up when
the desired size is not available. LoadIconWithScaleDown() introduced in
Windows Vista picks a larger icon variant and scales it down producing
slightly better results.

Signed-off-by: Simon Rozman <[email protected]>
  • Loading branch information
rozmansi committed Oct 31, 2019
1 parent 45de27c commit c0bb82a
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,15 @@ func (i *Icon) handleForDPIWithError(dpi int) (win.HICON, error) {

var hInst win.HINSTANCE
var name *uint16
var flags uint32
if i.filePath != "" {
absFilePath, err := filepath.Abs(i.filePath)
if err != nil {
return 0, err
}

name = syscall.StringToUTF16Ptr(absFilePath)

flags |= win.LR_LOADFROMFILE
} else {
if i.isStock {
flags |= win.LR_SHARED
} else {
if !i.isStock {
if hInst = win.GetModuleHandle(nil); hInst == 0 {
return 0, lastError("GetModuleHandle")
}
Expand All @@ -270,7 +265,6 @@ func (i *Icon) handleForDPIWithError(dpi int) (win.HICON, error) {

var size Size
if i.size96dpi.Width == 0 || i.size96dpi.Height == 0 {
flags |= win.LR_DEFAULTSIZE
size = SizeFrom96DPI(defaultIconSize(), dpi)
} else {
size = SizeFrom96DPI(i.size96dpi, dpi)
Expand All @@ -290,15 +284,14 @@ func (i *Icon) handleForDPIWithError(dpi int) (win.HICON, error) {
return 0, newError("SHDefExtractIcon")
}
} else {
hIcon = win.HICON(win.LoadImage(
hr := win.HICON(win.LoadIconWithScaleDown(
hInst,
name,
win.IMAGE_ICON,
int32(size.Width),
int32(size.Height),
flags))
if hIcon == 0 {
return 0, lastError("LoadImage")
&hIcon))
if hr < 0 || hIcon == 0 {
return 0, lastError("LoadIconWithScaleDown")
}
}

Expand Down

2 comments on commit c0bb82a

@xlplbo
Copy link

@xlplbo xlplbo commented on c0bb82a Nov 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes can not work on window server 2003 r3 SP2, LoadIconWithScaleDown failed.

@rozmansi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See you have provided a fallback to LoadImage() in #657. Thanks.

Please sign in to comment.