def MakeTransparent(self, amount):
if os.name == 'nt': # could substitute: sys.platform == 'win32'
hwnd = self.GetHandle()
_winlib = win32api.LoadLibrary("user32")
pSetLayeredWindowAttributes = win32api.GetProcAddress(
_winlib, "SetLayeredWindowAttributes")
if pSetLayeredWindowAttributes is None:
return
exstyle = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
if 0 == (exstyle & 0x80000):
exstyle |= win32con.WS_EX_LAYERED | win32con.WS_EX_TOOLWINDOW | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, exstyle)
win32gui.SetLayeredWindowAttributes(hwnd, 0, amount, 2)
else:
print('#### OS Platform must be MS Windows')
self.Destroy()
python类GWL_EXSTYLE的实例源码
def TestGradientFill():
wc = win32gui.WNDCLASS()
wc.lpszClassName = 'test_win32gui_2'
wc.style = win32con.CS_GLOBALCLASS|win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.hbrBackground = win32con.COLOR_WINDOW+1
wc.lpfnWndProc=wndproc_2
class_atom=win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindowEx(0, class_atom,'Kaleidoscope',
win32con.WS_CAPTION|win32con.WS_VISIBLE|win32con.WS_THICKFRAME|win32con.WS_SYSMENU,
100,100,900,900, 0, 0, 0, None)
s=win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s|win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA)
for x in xrange(30):
win32gui.InvalidateRect(hwnd,None,True)
win32gui.PumpWaitingMessages()
time.sleep(0.3)
win32gui.DestroyWindow(hwnd)
win32gui.UnregisterClass(class_atom,None)
def TestGradientFill():
wc = win32gui.WNDCLASS()
wc.lpszClassName = 'test_win32gui_2'
wc.style = win32con.CS_GLOBALCLASS|win32con.CS_VREDRAW | win32con.CS_HREDRAW
wc.hbrBackground = win32con.COLOR_WINDOW+1
wc.lpfnWndProc=wndproc_2
class_atom=win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindowEx(0, class_atom,'Kaleidoscope',
win32con.WS_CAPTION|win32con.WS_VISIBLE|win32con.WS_THICKFRAME|win32con.WS_SYSMENU,
100,100,900,900, 0, 0, 0, None)
s=win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s|win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 175, win32con.LWA_ALPHA)
for x in range(30):
win32gui.InvalidateRect(hwnd,None,True)
win32gui.PumpWaitingMessages()
time.sleep(0.3)
win32gui.DestroyWindow(hwnd)
win32gui.UnregisterClass(class_atom,None)
def remove_taskbar_button(self):
"""Hide window from taskbar and ALT+TAB dialog."""
win32gui.ShowWindow(self.hwnd, win32con.SW_HIDE)
win32api.SetWindowLong(
self.hwnd, win32con.GWL_EXSTYLE,
win32api.GetWindowLong(self.hwnd, win32con.GWL_EXSTYLE)
| win32con.WS_EX_NOACTIVATE
| win32con.WS_EX_TOOLWINDOW)
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)
def test_manipulate_ui(self):
logging.debug('Starting UI test')
win_handler = wh.WinHandler('Kalkulator')
hwnd = win_handler.get_hwnd()
style_base = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
win_handler.hide_extra_ui()
style_removed = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
assert style_base != style_removed
win_handler.hide_extra_ui(remove=False)
style_added = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
assert style_removed != style_added
def hide_extra_ui(self, hwnd=None, remove=True):
"""
:param hwnd: Hwnd to remove all styling from. If not supplied, then the default hwnd is used
:param remove: If true: Removes all styling. If false: Adds back the removed styles
:return: NoneType
"""
logging.debug('Trying to manipulate UI')
if hwnd is None:
hwnd = self.get_hwnd()
style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
if remove:
logging.debug('Removing UI')
style = style | win32con.WS_POPUP
style = style & ~win32con.WS_OVERLAPPEDWINDOW
else:
logging.debug('Adding UI')
style = style & ~win32con.WS_POPUP
style = style | win32con.WS_OVERLAPPEDWINDOW
win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, style)
win32gui.ShowWindow(hwnd, win32con.SW_SHOW)