From f39dbf1cbb3893961eea4324c325b2bdf1b67307 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Sat, 2 Jan 2021 15:00:15 -0500 Subject: [PATCH] Apply transparency mask to the connecting-state checkmark image Combine the image and mask in the connecting-state icon to form a bitmap with transparency for use as the checkmark. MSDN docs on SetMenuItemBitmaps is unclear about the use of color bitmaps for checkmarks, but this appears to display well. (Tested on Windows 10 only). Signed-off-by: Selva Nair --- Makefile.am | 3 ++- openvpn-gui.vcxproj | 4 ++-- tray.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Makefile.am b/Makefile.am index 530ed7a..eceb926 100644 --- a/Makefile.am +++ b/Makefile.am @@ -108,7 +108,8 @@ openvpn_gui_LDADD = \ -lnetapi32 \ -lole32 \ -lshlwapi \ - -lsecur32 + -lsecur32 \ + -lmsimg32 openvpn-gui-res.o: $(openvpn_gui_RESOURCES) $(srcdir)/openvpn-gui-res.h $(RCCOMPILE) -i $< -o $@ diff --git a/openvpn-gui.vcxproj b/openvpn-gui.vcxproj index e50b664..80bdb97 100644 --- a/openvpn-gui.vcxproj +++ b/openvpn-gui.vcxproj @@ -117,7 +117,7 @@ HAVE_CONFIG_H;_MSC_VER - Netapi32.lib;Wtsapi32.lib;Comctl32.lib;Secur32.lib;Ws2_32.lib;Crypt32.lib;Shlwapi.lib;Winhttp.lib;%(AdditionalDependencies) + Netapi32.lib;Wtsapi32.lib;Comctl32.lib;Secur32.lib;Ws2_32.lib;Crypt32.lib;Shlwapi.lib;Winhttp.lib;Msimg32.lib;%(AdditionalDependencies) copy config-msvc.h config.h @@ -132,7 +132,7 @@ _INC_MATH;WIN32_LEAN_AND_MEAN;UNICODE;_UNICODE;_CRT_NON_CONFORMING_WCSTOK;HAVE_CONFIG_H - Netapi32.lib;Wtsapi32.lib;Comctl32.lib;Secur32.lib;Ws2_32.lib;Crypt32.lib;Shlwapi.lib;Winhttp.lib;%(AdditionalDependencies) + Netapi32.lib;Wtsapi32.lib;Comctl32.lib;Secur32.lib;Ws2_32.lib;Crypt32.lib;Shlwapi.lib;Winhttp.lib;Msimg32.lib;%(AdditionalDependencies) diff --git a/tray.c b/tray.c index 2c32438..1861ff3 100644 --- a/tray.c +++ b/tray.c @@ -66,8 +66,46 @@ CreateBitmaps() { HICON icon = LoadLocalizedIconEx(ID_ICO_CONNECTING, cx, cy); ICONINFO iconinfo; - GetIconInfo(icon, &iconinfo); - hbmpConnecting = iconinfo.hbmColor; + + if (!GetIconInfo(icon, &iconinfo)) + { + MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Error loading ID_ICO_CONNECTING."); + return; + } + + /* Make a color bitmap with transparency for use as a + * checkmark for indicating the connecting state. We do this + * by combining the mask bitmap in the icon with its image bitmap. + * These bitmaps are created by the GetIconInfo call. + */ + + /* Create two DCs for drawing the images in memory */ + HDC maskDC = CreateCompatibleDC(NULL), imgDC = CreateCompatibleDC(NULL); + if (!maskDC || !imgDC) + { + DeleteObject(iconinfo.hbmMask); + DeleteObject(iconinfo.hbmColor); + if (maskDC) DeleteDC(maskDC); + if (imgDC) DeleteDC(imgDC); + return; + } + + /* Load the image and mask bitmaps into the DCs saving the default one's */ + HBITMAP def1 = (HBITMAP) SelectObject(imgDC, iconinfo.hbmColor); + HBITMAP def2 = (HBITMAP) SelectObject(maskDC, iconinfo.hbmMask); + + /* Transfer bit blocks from mask to image -- transparent color is black */ + TransparentBlt(imgDC, 0, 0, cx, cy, maskDC, 0, 0, cx, cy, RGB(0, 0, 0)); + + /* Save the result and restore the default bitmaps back in the DC */ + hbmpConnecting = (HBITMAP) SelectObject(imgDC, def1); + SelectObject(maskDC, def2); + + /* We don't need the mask bitmap -- free it */ + DeleteObject(iconinfo.hbmMask); + + DeleteDC(imgDC); + DeleteDC(maskDC); } }