Question

I'm attempting to use the SetLayeredWindowAttributes function to change the windows transparency color. I made a structure using the ctypes module. I'm pretty sure I have to use the COLORREF RGB macro to get this to work properly.

How do I use macros on a structure made using ctypes?

What I have going.

import Tkinter as tk

import win32gui
import win32con

class ColorRef (ctypes.Structure) :

    _fields_ = [("byRed", ctypes.c_byte),
                ("byGreen", ctypes.c_byte),
                ("byBlue", ctypes.c_byte)]

# makes a Tkinter window
root = tk.Tk()

# a handle to that window
handle = int(root.wm_frame(), 0)

# a COLORRED struct
colorref = ColorRef(1, 1, 1)

# attempting to change the transparency color
win32gui.SetLayeredWindowAttributes(handle, colorref, 0, win32con.LWA_COLORKEY)

root.mainloop()
Was it helpful?

Solution

Three things:

  1. C preprocessor macros don't exist outside C code. They are textually expanded before the actual compilation takes place.
  2. COLORREF is a typedef to DWORD, not a structure.
  3. All RGB macro does is some bitshifting to get 0x00bbggrr value.

So the code would look like this:

def RGB(r, g, b):
    r = r & 0xFF
    g = g & 0xFF
    b = b & 0xFF
    return (b << 16) | (g << 8) | r

colour = RGB(1, 1, 1)
win32gui.SetLayeredWindowAttributes(handle, colour, 0, win32con.LWA_COLORKEY)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top