MSDN COLORREF structure macros, Python
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()
Three things:
- C preprocessor macros don't exist outside C code. They are textually expanded before the actual compilation takes place.
- COLORREF is a typedef to DWORD, not a structure.
- 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)
精彩评论