pythonchromium-embeddedcefpython

How to set a CEF window icon (in python)


Not wx, gtk3, pyqt etc... I need something like:

cef.Initialize(settings=settings) window_info = cef.WindowInfo() browser = cef.CreateBrowserSync(url="localhost:8080/", window_title="Hello World!" icon="myicon.png")


Solution

  • On Linux execute xseticon program programmtically using os.system() function or similar, see: http://www.leonerd.org.uk/code/xseticon/ .

    On Windows use ctypes built-in Python module to execute native win32 functions. Example code below. The _hWnd variable holds window handle which can be obtained by calling browser.GetWindowHandle().

    from ctypes import *
    from ctypes.wintypes import *
    from os import path
    import platform
    
    LRESULT = c_int64 if platform.architecture()[0] == "64bit" else c_long
    
    SendMessage = windll.user32.SendMessageW
    SendMessage.restype = LRESULT
    SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM]
    
    GetModuleHandle = windll.kernel32.GetModuleHandleW
    GetModuleHandle.restype = HMODULE
    GetModuleHandle.argtypes = [LPCWSTR]
    
    IMAGE_ICON = 1
    LR_LOADFROMFILE = 0x00000010
    LR_CREATEDIBSECTION = 0x00002000
    
    LoadImage = windll.user32.LoadImageW
    LoadImage.restype = HANDLE
    LoadImage.argtypes = [HINSTANCE, LPCWSTR, UINT, c_int, c_int, UINT]
    
    RelPath = lambda file : path.join(path.dirname(path.abspath(__file__)), file)
    
    
    def AlterIcon(_hWnd, lpszIcon):
    
        WM_SETICON = 0x0080
        ICON_BIG = 1
    
        hModel = GetModuleHandle(None)
        hIcon = LoadImage(hModel,
                          RelPath(lpszIcon),
                          IMAGE_ICON,
                          0, 0,
                          LR_LOADFROMFILE | LR_CREATEDIBSECTION)
    
    
        SendMessage(_hWnd, WM_SETICON, ICON_BIG, hIcon)
    

    Ref: http://qaru.site/questions/7837596/how-to-include-image-in-message-box-using-ctypes-in-python