Add image in window tray menu

There are issues with the handles against types that may not result in errors.

I got this working by using the win32ui classes like PyCDC and PyCBitMap instead of handles.

Try to change prep_menu_icon to this:

def prep_menu_icon(self, icon):
    # First load the icon.
    ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON)
    ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON)
    hIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE)

    hwndDC = win32gui.GetWindowDC(self.hwnd)
    dc = win32ui.CreateDCFromHandle(hwndDC)
    memDC = dc.CreateCompatibleDC()
    iconBitmap = win32ui.CreateBitmap()
    iconBitmap.CreateCompatibleBitmap(dc, ico_x, ico_y)
    oldBmp = memDC.SelectObject(iconBitmap)
    brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU)

    win32gui.FillRect(memDC.GetSafeHdc(), (0, 0, ico_x, ico_y), brush)
    win32gui.DrawIconEx(memDC.GetSafeHdc(), 0, 0, hIcon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL)

    memDC.SelectObject(oldBmp)
    memDC.DeleteDC()
    win32gui.ReleaseDC(self.hwnd, hwndDC)

    return iconBitmap.GetHandle()

And I get the menu item icons:

Popup menu with icons


I can't seem to get the package set up on my computer, so can't really test this, but this line

option_icon = self.prep_menu_icon("\print_pref.ico")

gives me some concern. I'm not sure if you are reading the file that you think you are.

That \ is going to indicate an escape sequence. On Windows, you need to double those backslashes to prevent them from being escaped like "\\print_pref.ico". If you are trying to load a file in the current directory, you may not need that at all and can just give the file name - "print_pref.ico". If you are trying to locate a file in the drive's root directory, you need to give the drive letter "C:\\print_pref.ico".