-
Notifications
You must be signed in to change notification settings - Fork 515
fix(windows): pre-multiply alpha when setting menu item icons to fix black background #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,7 @@ import ( | |||||
|
|
||||||
| var ( | ||||||
| g32 = windows.NewLazySystemDLL("Gdi32.dll") | ||||||
| pCreateCompatibleBitmap = g32.NewProc("CreateCompatibleBitmap") | ||||||
| pCreateDIBSection = g32.NewProc("CreateDIBSection") | ||||||
| pCreateCompatibleDC = g32.NewProc("CreateCompatibleDC") | ||||||
| pDeleteDC = g32.NewProc("DeleteDC") | ||||||
| pSelectObject = g32.NewProc("SelectObject") | ||||||
|
|
@@ -751,7 +751,36 @@ func (t *winTray) iconToBitmap(hIcon windows.Handle) (windows.Handle, error) { | |||||
| defer pDeleteDC.Call(hMemDC) | ||||||
| cx, _, _ := pGetSystemMetrics.Call(SM_CXSMICON) | ||||||
| cy, _, _ := pGetSystemMetrics.Call(SM_CYSMICON) | ||||||
| hMemBmp, _, err := pCreateCompatibleBitmap.Call(hDC, cx, cy) | ||||||
|
|
||||||
| var bi struct { | ||||||
| Size uint32 | ||||||
| Width int32 | ||||||
| Height int32 | ||||||
| Planes uint16 | ||||||
| BitCount uint16 | ||||||
| Compression uint32 | ||||||
| SizeImage uint32 | ||||||
| XPelsPerMeter int32 | ||||||
| YPelsPerMeter int32 | ||||||
| ClrUsed uint32 | ||||||
| ClrImportant uint32 | ||||||
| } | ||||||
| bi.Size = uint32(unsafe.Sizeof(bi)) | ||||||
| bi.Width = int32(cx) | ||||||
| bi.Height = int32(-cy) // top-down | ||||||
| bi.Planes = 1 | ||||||
| bi.BitCount = 32 | ||||||
| bi.Compression = 0 // BI_RGB | ||||||
|
|
||||||
| var bits unsafe.Pointer | ||||||
| hMemBmp, _, err := pCreateDIBSection.Call( | ||||||
| hDC, | ||||||
| uintptr(unsafe.Pointer(&bi)), | ||||||
| 0, // DIB_RGB_COLORS | ||||||
| uintptr(unsafe.Pointer(&bits)), | ||||||
| 0, | ||||||
| 0, | ||||||
| ) | ||||||
| if hMemBmp == 0 { | ||||||
| return 0, err | ||||||
| } | ||||||
|
|
@@ -761,6 +790,18 @@ func (t *winTray) iconToBitmap(hIcon windows.Handle) (windows.Handle, error) { | |||||
| if res == 0 { | ||||||
| return 0, err | ||||||
| } | ||||||
|
|
||||||
| if bits != nil { | ||||||
| pixels := (*[1 << 30]byte)(bits)[:cx*cy*4 : cx*cy*4] | ||||||
| // Pre-multiply alpha channel before creating HBITMAP | ||||||
|
Comment on lines
+794
to
+796
|
||||||
| // Pre-multiply alpha channel before creating HBITMAP | |
| // Pre-multiply alpha channel in-place before returning/using the HBITMAP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
DrawIconExfails, the function returns without cleaning uphMemBmp, which leaks a GDI bitmap handle on the error path. Consider adding aDeleteObjectproc and deferring cleanup afterCreateDIBSectionsucceeds, then canceling the defer when returning the bitmap successfully.