def show_window(self): # https://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx win32gui.SetLayeredWindowAttributes( self.hWindow_edge, 0x00ffffff, self.alpha, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) win32gui.SetLayeredWindowAttributes( self.hWindow, 0x00ffffff, self.alpha, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) # https://msdn.microsoft.com/en-us/library/windows/desktop/dd145167(v=vs.85).aspx #win32gui.UpdateWindow(self.hWindow) # https://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx win32gui.SetWindowPos( self.hWindow_edge, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) win32gui.SetWindowPos( self.hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx # win32gui.ShowWindow(self.hWindow, win32con.SW_SHOW) self.DRAWING = True win32gui.PumpMessages()
def getWindow_Img_new(self, Emu_Index): self.Hwnd = int(self.Get_Self_Hawd(Emu_Index)) # 將 hwnd 換成 WindowLong s = win32gui.GetWindowLong(self.Hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong(self.Hwnd, win32con.GWL_EXSTYLE, s | win32con.WS_EX_LAYERED) # 判斷視窗是否最小化 show = win32gui.IsIconic(self.Hwnd) # 將視窗圖層屬性改變成透明 # 還原視窗並拉到最前方 # 取消最大小化動畫 # 取得視窗寬高 if show == 1: win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 0) win32gui.SetLayeredWindowAttributes(self.Hwnd, 0, 0, win32con.LWA_ALPHA) win32gui.ShowWindow(self.Hwnd, win32con.SW_RESTORE) x, y, width, height = self.getWindow_W_H(self.Hwnd) # 創造輸出圖層 hwindc = win32gui.GetWindowDC(self.Hwnd) srcdc = win32ui.CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp = win32ui.CreateBitmap() # 取得視窗寬高 x, y, width, height = self.getWindow_W_H(self.Hwnd) # 如果視窗最小化,則移到Z軸最下方 if show == 1: win32gui.SetWindowPos(self.Hwnd, win32con.HWND_BOTTOM, x, y, width, height, win32con.SWP_NOACTIVATE) # 複製目標圖層,貼上到 bmp bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.SelectObject(bmp) memdc.BitBlt((0, 0), (width, height), srcdc, (8, 3), win32con.SRCCOPY) # 將 bitmap 轉換成 np signedIntsArray = bmp.GetBitmapBits(True) img = np.fromstring(signedIntsArray, dtype='uint8') img.shape = (height, width, 4) #png,具有透明度的 # 釋放device content srcdc.DeleteDC() memdc.DeleteDC() win32gui.ReleaseDC(self.Hwnd, hwindc) win32gui.DeleteObject(bmp.GetHandle()) # 還原目標屬性 if show == 1: win32gui.SetLayeredWindowAttributes(self.Hwnd, 0, 255, win32con.LWA_ALPHA) win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 1) # 回傳圖片 #src_img = img #src_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #src_img = cv2.imwrite('test1.jpg',img) #self.ScreenHot = img return img
def assign_window_color_for_transparency(hwnd, color_tuple): win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*color_tuple), 0, win32con.LWA_COLORKEY)
def set_transparent(self, hwnd, alpha): # win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32con.WS_POPUP) print('p1o13p') # win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, (win32gui.GetWindowLong( # hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) & (~(win32con.WS_CAPTION # | win32con.WS_THICKFRAME # | win32con.WS_MINIMIZEBOX # | win32con.WS_MAXIMIZEBOX # | win32con.WS_SYSMENU # | win32con.WS_BORDER # | win32con.WS_DLGFRAME # ))) win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) # win32con.WS_BORDER) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(95, 95, 95), int(alpha * 255), win32con.LWA_ALPHA) # SetWindowPos(hwnd, NULL, 0,0,0,0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER); # SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER # win32gui.SetWindowPos(hwnd, 0, 0, 0, 480, 640, win32con.SWP_NOMOVE | # win32con.SWP_NOSIZE | win32con.SWP_NOOWNERZORDER | # win32con.SWP_NOZORDER | win32con.SWP_FRAMECHANGED) pass
def __init__(self, parent=None, point_size=23, *args, **kw): super().__init__(parent, style=wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP | wx.BORDER_NONE | wx.CLIP_CHILDREN, *args, **kw) # let the frame can be clicked through, which means it won't block mouse events # http://wxwidgets.10942.n7.nabble.com/wxPython-and-pywin32-Implementing-on-top-transparency-and-click-through-on-Windows-tp30543.html hwnd = self.GetHandle() extended_style_settings = win32gui.GetWindowLong( hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, extended_style_settings | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT) win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA) # prevent text flickering self.SetDoubleBuffered(True) self.SetTransparent(200) self.SetBackgroundColour('black') sizer = wx.BoxSizer(wx.VERTICAL) self.SetSizer(sizer) self.text = wx.StaticText(self) self.text.SetForegroundColour('white') font = self.text.GetFont() font.PointSize = point_size self.text.SetFont(font) sizer.Add(self.text, 0, wx.LEFT | wx.RIGHT, border=5)
def terminate(): clock = pygame.time.Clock() pygame.mixer.music.load("resources/final_message.mp3") pygame.mixer.music.play() while pygame.mixer.music.get_busy(): clock.tick(30) pygame.mixer.music.load("resources/ending.mp3") pygame.mixer.music.play() # Create layered window hwnd = pygame.display.get_wm_info()["window"] win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) clock = pygame.time.Clock() i = 255 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pass win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0, 0, 0), round(i), win32con.LWA_ALPHA) i = max(0, i - 1.5) if i == 0: break clock.tick(30) pygame.display.update() pygame.quit() sys.exit(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 mycreateoverlay(self): try: if self.overlaywindow.state() == "normal": self.overlaywindow.focus() except: self.overlaywindow = Toplevel(self.mainwindow) self.overlaywindow.attributes("-fullscreen", True) self.overlaywindow.attributes("-transparentcolor", "red") self.overlaywindow.update_idletasks() self.overlaywindow.lift() self.overlayframe = Frame(self.overlaywindow, bg="red", borderwidth=10, highlightthickness=0) self.overlayframe.pack(anchor=CENTER, ipady=self.mainwindow.winfo_screenheight(), ipadx=self.mainwindow.winfo_screenwidth()) l = Label(self.overlayframe, textvariable=foldername) l.pack(anchor=NW) hwnd = l.winfo_id() lExStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) lExStyle |= win32con.WS_EX_LAYERED win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, lExStyle) win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA) self.mylist() self.overlaywindow.wm_attributes("-topmost", True) self.overlaywindow.overrideredirect(1)
def main(): hInstance = win32api.GetModuleHandle() className = 'MyWindowClassName' wndClass = win32gui.WNDCLASS() wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = wndProc wndClass.hInstance = hInstance wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className wndClassAtom = win32gui.RegisterClass(wndClass) exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE hWindow = win32gui.CreateWindowEx( exStyle, wndClassAtom, None, # WindowName style, 0, 0, # x,y win32api.GetSystemMetrics(win32con.SM_CXSCREEN), win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # width,height None, # hWndParent None, # hMenu hInstance, None # lpParam ) win32gui.SetLayeredWindowAttributes( hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) #win32gui.UpdateWindow(hWindow) win32gui.SetWindowPos( hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)
def set_clickthrough(self): """ Allow for making mouse clicks pass through the window, in case you want to use it as an overlay in your game Also makes the window transparent to visualize this effect. Calling the function again reverts the window to normal state. """ hwnd = win32gui.FindWindow(None, self.title) if self.clickable: # Get window style and perform a 'bitwise or' operation to make the style layered and transparent, achieving # the clickthrough property l_ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) l_ex_style |= win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, l_ex_style) # Set the window to be transparent and appear always on top win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0, 0, 0), 190, win32con.LWA_ALPHA) # transparent win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, self.root.winfo_x(), self.root.winfo_y(), 0, 0, 0) self.clickable = False else: # Calling the function again sets the extended style of the window to zero, reverting to a standard window win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, 0) if not self.always_on_top: # Remove the always on top property again, in case always on top was set to false in options win32gui.SetWindowPos(hwnd, win32con.HWND_NOTOPMOST, self.root.winfo_x(), self.root.winfo_y(), 0, 0, 0) self.clickable = True
def _init_window(self): """Initialize the actual window and show it""" self._window = gui.CreateWindowEx( self._ex_style, # External style self._window_class_atom, # Window class self._class_name, # Window title self._style, # Window style self._position[0], # X coordinate self._position[1], # y coordinate self._size[0], # Width self._size[1], # Height None, # Parent window None, # Menu self._h_instance, None # lpParam ) gui.SetLayeredWindowAttributes(self._window, 0x00ffffff, 0xff, con.LWA_COLORKEY | con.LWA_ALPHA) gui.UpdateWindow(self._window) gui.SetWindowPos( self._window, # Window object con.HWND_TOPMOST, # Topmost window handle self._position[0], # x coordinate self._position[1], # y coorinate 0, # width (resizes automatically) 0, # height (resizes automatically) con.SWP_NOACTIVATE | con.SWP_NOMOVE | con.SWP_NOSIZE | con.SWP_SHOWWINDOW) gui.ShowWindow(self._window, con.SW_SHOW) self.__init = True
def initial_draw(self): self.window_handle = win32gui.CreateWindowEx( win32_contants.EX_STYLE, self._wndClassAtom, None, # WindowName win32_contants.STYLE, self.x, self.y, self.width, self.height, None, # hWndParent None, # hMenu self._hInstance, None # lpParam ) # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx win32gui.SetLayeredWindowAttributes( self.window_handle, 0x00ffffff, 255, win32_contants.LWA_COLORKEY | win32_contants.LWA_ALPHA) # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145167(v=vs.85).aspx #win32gui.UpdateWindow(self.window_handle) # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx win32gui.SetWindowPos( self.window_handle, win32_contants.HWND_TOPMOST, 0, 0, 0, 0, win32_contants.SWP_NOACTIVATE | win32_contants.SWP_NOMOVE | win32_contants.SWP_NOSIZE | win32_contants.SWP_SHOWWINDOW) self.window_rendered.set()
def imageBouncer(): global WIN_WIDTH, WIN_HEIGHT img = getImage() clock = pygame.time.Clock() dt = clock.tick(60) screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT), pygame.RESIZABLE) pygame.display.set_caption('Image Bouncer') pygame.display.set_icon(pygame.image.load('res/icon.png')) # Set window transparency color hwnd = pygame.display.get_wm_info()['window'] win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*BLACK), 0, win32con.LWA_COLORKEY) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.VIDEORESIZE: WIN_WIDTH = event.w WIN_HEIGHT = event.h screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT), pygame.RESIZABLE) screen.fill(BLACK) # Set transparent background if img.x + img.w >= WIN_WIDTH: img.x_vel = -INIT_VEL img.x = WIN_WIDTH - img.w elif img.x <= 0: img.x_vel = INIT_VEL img.x = 0 if img.y + img.h >= WIN_HEIGHT: img.y_vel = -INIT_VEL img.y = WIN_HEIGHT - img.h elif img.y <= 0: img.y_vel = INIT_VEL img.y = 0 if dt < 100: img.x += img.x_vel * dt img.y += img.y_vel * dt screen.blit(img.img, (int(img.x), int(img.y))) pygame.display.update() dt = clock.tick(60) pygame.display.quit()
def overlay_cv_window(hwnd, alpha=100): try: styles = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) styles = win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, styles) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), alpha, win32con.LWA_COLORKEY) except Exception as e: print(e)
def set_transparency(self): existing_bits = win32gui.GetWindowLong(self.shell_hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong(self.shell_hwnd, win32con.GWL_EXSTYLE, existing_bits | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes( self.shell_hwnd, 1, int(self.transparent_factor * 255), win32con.LWA_COLORKEY | win32con.LWA_ALPHA)
def set_transparency(window_title, transparency_level): window_handler = win32gui.FindWindow(None, window_title) win32gui.SetWindowLong( window_handler, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(window_handler, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(window_handler, win32api.RGB(0, 0, 0), transparency_level, win32con.LWA_ALPHA)
def set_transparent(self, hwnd, alpha): win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(95, 95, 95), int(alpha * 255), win32con.LWA_ALPHA) pass
def transparentwindow(self): self.hwnd = self.root.winfo_id() p = win32gui.GetWindowLong(self.hwnd, win32con.GWL_EXSTYLE) if not (p & win32con.WS_EX_LAYERED): win32gui.SetWindowLong(self.hwnd, win32con.GWL_EXSTYLE, p | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes( self.hwnd, win32api.RGB(1, 1, 1), int(180), win32con.LWA_ALPHA | win32con.LWA_COLORKEY ) self.root.update()
def main(): hInstance = win32api.GetModuleHandle() className = 'MyWindowClassName' # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx # win32gui does not support WNDCLASSEX. wndClass = win32gui.WNDCLASS() # http://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = wndProc wndClass.hInstance = hInstance wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className # win32gui does not support RegisterClassEx wndClassAtom = win32gui.RegisterClass(wndClass) print 'here' # http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx # Consider using: WS_EX_COMPOSITED, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT # The WS_EX_TRANSPARENT flag makes events (like mouse clicks) fall through the window. exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx # Consider using: WS_DISABLED, WS_POPUP, WS_VISIBLE style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx hWindow = win32gui.CreateWindowEx( exStyle, wndClassAtom, None, # WindowName style, 0, # x 0, # y win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height None, # hWndParent None, # hMenu hInstance, None # lpParam ) # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145167(v=vs.85).aspx #win32gui.UpdateWindow(hWindow) # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx #win32gui.ShowWindow(hWindow, win32con.SW_SHOW) win32gui.PumpMessages()
def set_transparency(self, alpha=120): "Set the window transparency from 0 to 255 by alpha attribute" if not (0 <= alpha <= 255): raise ValueError('alpha should be in [0, 255] interval!') # TODO: implement SetExStyle method win32gui.SetWindowLong(self.handle, win32defines.GWL_EXSTYLE, self.exstyle() | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(self.handle, win32api.RGB(0, 0, 0), alpha, win32con.LWA_ALPHA)
def FadeOut(Hwnd): """ 淡出后退出 """ for i in range(240, 1, -1): win32gui.SetLayeredWindowAttributes(Hwnd, 0, i, win32con.LWA_ALPHA) pygame.display.flip() pygame.time.delay(3) pygame.quit()
def set_window_boring_stuff(): hwnd = pygame.display.get_wm_info()["window"] win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY)\ win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
def transparent_mode(self): opt = Options() SetWindowPos = windll.user32.SetWindowPos hwnd = pygame.display.get_wm_info()["window"] win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(44, 44, 0), 0, win32con.LWA_COLORKEY) # RGB(44, 44, 0) = #2C2C00 win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, opt.x_position, opt.y_position, opt.width, opt.height, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) # Always set it on the top, useful when playing in fullscreen self.screen.fill(DrawingTool.color("#2C2C00")) # This color is a good comprise between readability and performances. Somehow, using black or white make the tracker not responding.
def test(hWnd, windows): if not isRealWindows(hWnd): return windows.append(hWnd) win32gui.SetWindowLong( hWnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hWnd, win32api.RGB(0, 0, 0), 0, win32con.LWA_ALPHA)
def getWindow_Img(hwnd): # 將 hwnd 換成 WindowLong s = win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE) win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, s|win32con.WS_EX_LAYERED) # 判斷視窗是否最小化 show = win32gui.IsIconic(hwnd) # 將視窗圖層屬性改變成透明 # 還原視窗並拉到最前方 # 取消最大小化動畫 # 取得視窗寬高 if show == 1: win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 0) win32gui.SetLayeredWindowAttributes(hwnd, 0, 0, win32con.LWA_ALPHA) win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) x, y, width, height = getWindow_W_H(hwnd) # 創造輸出圖層 hwindc = win32gui.GetWindowDC(hwnd) srcdc = win32ui.CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp = win32ui.CreateBitmap() # 取得視窗寬高 x, y, width, height = getWindow_W_H(hwnd) # 如果視窗最小化,則移到Z軸最下方 if show == 1: win32gui.SetWindowPos(hwnd, win32con.HWND_BOTTOM, x, y, width, height, win32con.SWP_NOACTIVATE) # 複製目標圖層,貼上到 bmp bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.SelectObject(bmp) memdc.BitBlt((0 , 0), (width, height), srcdc, (0, 0), win32con.SRCCOPY) # 將 bitmap 轉換成 np signedIntsArray = bmp.GetBitmapBits(True) img = np.fromstring(signedIntsArray, dtype='uint8') img.shape = (height,width,4) # 釋放device content srcdc.DeleteDC() memdc.DeleteDC() win32gui.ReleaseDC(hwnd, hwindc) win32gui.DeleteObject(bmp.GetHandle()) # 還原目標屬性 if show == 1 : win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA) win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 1) # 回傳圖片 return img
def getWindowImg(hwnd): # Check whether window is minimized minimized = win32gui.IsIconic(hwnd) if minimized == 1: # Cancel max/minimize animation win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 0) # Set window as transparent win32gui.SetLayeredWindowAttributes(hwnd, 0, 0, win32con.LWA_ALPHA) # Restore window from minimized state win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) # Put the window on bottom layer left, top, width, height, x, y = getWindowSize(hwnd) win32gui.SetWindowPos(hwnd, win32con.HWND_BOTTOM, left, top, width, height, win32con.SWP_NOACTIVATE) # Restore transparent and animation settings win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA) win32gui.SystemParametersInfo(win32con.SPI_SETANIMATION, 1) # Capture left, top, width, height, x, y = getWindowSize(hwnd) hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, x, y) saveDC.SelectObject(saveBitMap) windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1) signedIntsArray = saveBitMap.GetBitmapBits(True) img = np.frombuffer(signedIntsArray, dtype='uint8') img.shape = (y, x, 4) # Debug # print("saveBitMap.GetInfo():", saveBitMap.GetInfo()) # saveBitMap.SaveBitmapFile(saveDC, "debug.png") # print(img) # Release memory saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) win32gui.DeleteObject(saveBitMap.GetHandle()) return img
def move_transparent_window(x, y, fuchsia): SetWindowPos = windll.user32.SetWindowPos hwnd = pygame.display.get_wm_info()["window"] win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY) SetWindowPos(hwnd, -1, x, y, 0, 0, 0x0001)
def makeItTransparent( self, alpha): # Set alpha between 0 and 1. 0 no opacity, 1 invisible alpha = int((1 - alpha) * 255) win32gui.SetWindowLong( self.getHandleOfThisWindow(), win32con.GWL_EXSTYLE, win32gui.GetWindowLong(self.getHandleOfThisWindow(), win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) # Make it a layered window win32gui.SetLayeredWindowAttributes( self.getHandleOfThisWindow(), win32api.RGB(0, 0, 0), alpha, win32con. LWA_ALPHA) # make it transparent (alpha between 0 and 255)
def SetWindowTransparent(hwnd, transparency=255): """ 设置窗口透明度 :param hwnd: :param transparency: 透明度最大255,不透明,0最小,完全透明 :return: """ win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0, 0, 0), transparency, win32con.LWA_ALPHA)
def set_invisible(self, hwnd): try: # win32gui.ShowWindow(hwnd, win32con.SW_HIDE) win32gui.SetWindowPos(hwnd, win32con.HWND_BOTTOM, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE) # win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, # win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_TOOLWINDOW); win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED | win32con.WS_EX_TOOLWINDOW); win32gui.SetLayeredWindowAttributes(hwnd, 0, 0, 2) # win32gui.ShowWindow(hwnd, win32con.SW_SHOW) except: print('fail: ' + str(sys.exc_info()[0]) + '(' + str(sys.exc_info()[1]) + ')')