MSDN COLORREF 结构宏,Python

rec*_*gle 2 macros winapi pywin32 ctype

我正在尝试使用SetLayeredWindowAttributes函数来更改窗口透明度颜色。我使用 ctypes 模块创建了一个结构。我很确定我必须使用COLORREF RGB 宏才能使其正常工作。

如何在使用 ctypes 生成的结构上使用宏?

我要去做什么。

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()
Run Code Online (Sandbox Code Playgroud)

Cat*_*lus 5

三件事:

  1. C 预处理器宏不存在于 C 代码之外。它们在实际编译之前进行文本扩展。
  2. COLORREF 是 DWORD 的类型定义,而不是结构。
  3. RGB 宏所做的只是进行一些位移来获取0x00bbggrr值。

所以代码看起来像这样:

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)
Run Code Online (Sandbox Code Playgroud)