def set_alpha(self, alpha=100, colorkey=0, mask=False):
""" Sets the transparency of the window. """
if self.alpha != None:
if not self._transparency:
style = win32gui.GetWindowLong(self._hwnd, GWL_EXSTYLE)
if (style & WS_EX_LAYERED) != WS_EX_LAYERED:
style = style | WS_EX_LAYERED
win32gui.SetWindowLong(self._hwnd, GWL_EXSTYLE, style)
self._transparency = True
if mask and colorkey:
flags = LWA_COLORKEY
else:
flags = LWA_ALPHA
if colorkey:
flags = flags | LWA_COLORKEY
win_alpha = int(float(alpha)/100*255)
winxpgui.SetLayeredWindowAttributes(self._hwnd, colorkey,
win_alpha, flags)
self.alpha = int(alpha)
python类GetWindowLong()的实例源码
def _accessible_object_from_window(self, hwnd):
"""???????AccessibleObject
:type hwnd: int
:param hwnd: ??
:raises: ValueError
:rtype: comtypes.gen.Accessibility.IAccessible
"""
if not win32gui.IsWindow(hwnd):
raise ValueError("window(%s) is not valid!" % hwnd)
OBJID_WINDOW = 0
OBJID_CLIENT = -4
if (win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE) & win32con.WS_CHILDWINDOW) > 0:
objID = OBJID_CLIENT
else:
objID = OBJID_WINDOW
accObj = ctypes.POINTER(IAccessible)()
ctypes.oledll.oleacc.AccessibleObjectFromWindow(hwnd,
objID,
ctypes.byref(IAccessible._iid_),
ctypes.byref(accObj))
return accObj
def OnPaint_1(hwnd, msg, wp, lp):
dc, ps=win32gui.BeginPaint(hwnd)
win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
br=win32gui.CreateSolidBrush(win32api.RGB(255,0,0))
win32gui.SelectObject(dc, br)
angle=win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA)
win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle+2)
r_angle=angle*(math.pi/180)
win32gui.SetWorldTransform(dc,
{'M11':math.cos(r_angle), 'M12':math.sin(r_angle), 'M21':math.sin(r_angle)*-1, 'M22':math.cos(r_angle),'Dx':250,'Dy':250})
win32gui.MoveToEx(dc,250,250)
win32gui.BeginPath(dc)
win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10)
win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10)
win32gui.LineTo(dc, 300,300)
win32gui.LineTo(dc, 100, 20)
win32gui.LineTo(dc, 20, 100)
win32gui.LineTo(dc, 400, 0)
win32gui.LineTo(dc, 0, 400)
win32gui.EndPath(dc)
win32gui.StrokeAndFillPath(dc)
win32gui.EndPaint(hwnd, ps)
return 0
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 OnPaint_1(hwnd, msg, wp, lp):
dc, ps=win32gui.BeginPaint(hwnd)
win32gui.SetGraphicsMode(dc, win32con.GM_ADVANCED)
br=win32gui.CreateSolidBrush(win32api.RGB(255,0,0))
win32gui.SelectObject(dc, br)
angle=win32gui.GetWindowLong(hwnd, win32con.GWL_USERDATA)
win32gui.SetWindowLong(hwnd, win32con.GWL_USERDATA, angle+2)
r_angle=angle*(math.pi/180)
win32gui.SetWorldTransform(dc,
{'M11':math.cos(r_angle), 'M12':math.sin(r_angle), 'M21':math.sin(r_angle)*-1, 'M22':math.cos(r_angle),'Dx':250,'Dy':250})
win32gui.MoveToEx(dc,250,250)
win32gui.BeginPath(dc)
win32gui.Pie(dc, 10, 70, 200, 200, 350, 350, 75, 10)
win32gui.Chord(dc, 200, 200, 850, 0, 350, 350, 75, 10)
win32gui.LineTo(dc, 300,300)
win32gui.LineTo(dc, 100, 20)
win32gui.LineTo(dc, 20, 100)
win32gui.LineTo(dc, 400, 0)
win32gui.LineTo(dc, 0, 400)
win32gui.EndPath(dc)
win32gui.StrokeAndFillPath(dc)
win32gui.EndPaint(hwnd, ps)
return 0
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 _hide_w32_window(self):
try:
w32win = win32gui.FindWindow(None, self.title)
win32gui.ShowWindow(w32win, SW_HIDE)
win32gui.SetWindowLong(
w32win,
GWL_EXSTYLE,
win32gui.GetWindowLong(
w32win, GWL_EXSTYLE) | WS_EX_TOOLWINDOW
)
win32gui.ShowWindow(w32win, SW_SHOW)
self._return_focus_w32()
except Exception:
tb = traceback.format_exc()
Logger.error(
'Notification: An error occured in {}\n'
'{}'.format(self.title, tb)
)
def add_titlebar(hwnd):
"""
Sets the window style to include a titlebar if it doesn't have one.
Args:
hwnd (int): The window handler.
"""
style = wg.GetWindowLong(hwnd, wc.GWL_STYLE)
style |= wc.WS_CAPTION
wg.SetWindowLong(hwnd, wc.GWL_STYLE, style)
maximize(hwnd)
restore(hwnd)
def remove_titlebar(hwnd):
"""
Sets window style to caption (no titlebar).
Args:
hwnd (int): The window handler.
"""
style = wg.GetWindowLong(hwnd, wc.GWL_STYLE)
style &= ~wc.WS_CAPTION
wg.SetWindowLong(hwnd, wc.GWL_STYLE, style)
maximize(hwnd)
restore(hwnd)
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)
def WndProcManage(wnd):
if not hasattr(wnd, 'GetHandle'):
return False
# Make a dictionary of message names to be used for printing below
if PRINT_MESSAGES:
msgdict = dict()
for name in dir(win32con):
if name.startswith('WM_'):
value = getattr(win32con, name)
msgdict[value] = name
_oldWndProc = win32gui.GetWindowLong(wnd.GetHandle(),
win32con.GWL_WNDPROC)
def MyWndProc(self, hwnd, msg, wParam, lParam):
# Display what we've got.
if PRINT_MESSAGES:
print (msgdict.get(msg), msg, wParam, lParam)
# Restore the old WndProc. Notice the use of win32api
# instead of win32gui here. This is to avoid an error due to
# not passing a callable object.
if msg == win32con.WM_DESTROY:
win32api.SetWindowLong(self.GetHandle(),
win32con.GWL_WNDPROC,
_oldWndProc)
stopproc = False
for cb, cbtrigger in CALLBACKS[self]:
if cbtrigger(self, msg, wParam, lParam):
if not cb(self, msg, wParam, lParam):
stopproc = True
break
if stopproc:
return
# Pass all messages (in this case, yours may be different) on to
# the original WndProc
return win32gui.CallWindowProc(_oldWndProc,
hwnd, msg,
wParam,
lParam)
# Bind the function to the passed object
_newWndProc = MyWndProc.__get__(wnd, wnd.__class__)
# Set the WndProc to our function
win32gui.SetWindowLong(wnd.GetHandle(),
win32con.GWL_WNDPROC,
_newWndProc)
return True