def register(self): self.hInstance = win32api.GetModuleHandle() className = 'MyWindowClassName' # https://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx # win32gui does not support WNDCLASSEX. wndClass = win32gui.WNDCLASS() # https://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = self.wndProc wndClass.hInstance = self.hInstance wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className # win32gui does not support RegisterClassEx self.wndClassAtom = win32gui.RegisterClass(wndClass) self.hInstance_edge = win32api.GetModuleHandle() className = 'MyWindowClassName_edge' wndClass_edge = win32gui.WNDCLASS() # https://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx wndClass_edge.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass_edge.lpfnWndProc = self.wndProc_edge wndClass_edge.hInstance = self.hInstance_edge wndClass_edge.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass_edge.hbrBackground = win32gui.GetStockObject( win32con.WHITE_BRUSH) wndClass_edge.lpszClassName = className self.wndClassAtom_edge = win32gui.RegisterClass(wndClass_edge)
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 main(): hInstance = win32api.GetModuleHandle() className = 'SimpleWin32' wndClass = win32gui.WNDCLASS() wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = wndProc wndClass.hInstance = hInstance wndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className try: wndClassAtom = win32gui.RegisterClass(wndClass) except Exception as e: raise e hWindow = win32gui.CreateWindow( wndClassAtom, 'FullGenReport', win32con.WS_OVERLAPPEDWINDOW, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, None, None, hInstance, None) win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL) win32gui.UpdateWindow(hWindow) win32gui.PumpMessages()
def _register_window(self): message_map = { win32con.WM_DESTROY: self._on_destroy, win32con.WM_SIZE: self._on_resize, win32con.WM_ERASEBKGND: self._on_erase_bkgnd, win32con.WM_GETMINMAXINFO: self._on_minmax_info } self.wndclass = win32gui.WNDCLASS() self.wndclass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW self.wndclass.lpfnWndProc = message_map self.wndclass.hInstance = win32api.GetModuleHandle() self.wndclass.hCursor = win32gui.LoadCursor(win32con.NULL, win32con.IDC_ARROW) self.wndclass.hbrBackground = win32gui.GetStockObject( win32con.WHITE_BRUSH) self.wndclass.lpszMenuName = "" self.wndclass.lpszClassName = "MainWin" try: # Try loading an icon embedded in the exe file. This will crash when frozen with PyInstaller self.wndclass.hIcon = win32gui.LoadIcon(self.wndclass.hInstance, 1) except: pass # Register Window Class if not win32gui.RegisterClass(self.wndclass): raise WinError()
def main(): hInstance = win32api.GetModuleHandle() # 获取当前的实例句柄 # 定义窗口类 wndClass = win32gui.WNDCLASS() wndClass.lpszClassName = 'window' # 窗口的类名 wndClass.lpfnWndProc = wndProc wndClass.hInstance = hInstance wndClass.cbWndExtra = 0 wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.hIcon = win32gui.LoadIcon(None, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) # 注册窗口类 wndClassAtom = win32gui.RegisterClass(wndClass) # 创建窗口 hWindow = win32gui.CreateWindow( wndClassAtom, 'Python Win32 Window', win32con.WS_OVERLAPPEDWINDOW, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hInstance, None) # 显示窗口 win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL) # 更新窗口 win32gui.UpdateWindow(hWindow) # 消息循环 win32gui.PumpMessages()
def setPenColor(self, r, g, b): self.hbrush = win32gui.GetStockObject( win32con.NULL_BRUSH) # 定义透明画刷,这个很重要!! self.hPen = win32gui.CreatePen(win32con.PS_SOLID, 2, win32api.RGB(r, g, b)) # 定义框颜色 win32gui.SelectObject(self.hwndDC, self.hPen) self.prebrush = win32gui.SelectObject(self.hwndDC, self.hbrush) win32gui.SelectObject(self.hwndDC, self.prebrush)
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 highlightWindow(self, hwnd): if hwnd is not None: left, top, right, bottom = win32gui.GetWindowRect(hwnd) windowDc = win32gui.GetWindowDC(hwnd) if windowDc: prevPen = win32gui.SelectObject(windowDc, self.rectanglePen) prevBrush = win32gui.SelectObject( windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH)) win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top) win32gui.SelectObject(windowDc, prevPen) win32gui.SelectObject(windowDc, prevBrush) win32gui.ReleaseDC(hwnd, windowDc)
def GetWndClasss(self, hInstance, WndProc, szWindowClass='DesktopApp', Transparent=True): WndClass = win32gui.WNDCLASS() WndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW WndClass.lpfnWndProc = WndProc WndClass.hInstance = hInstance WndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) WndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) if Transparent: WndClass.hbrBackground = self.RGB(0,0,0) else: WndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) WndClass.lpszClassName = szWindowClass return WndClass
def _show_splash_screen(self): #get instance handle hInstance = win32api.GetModuleHandle() # the class name className = 'SimpleWin32' # create and initialize window class wndClass = win32gui.WNDCLASS() wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = self.wndProc wndClass.hInstance = hInstance wndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className # register window class wndClassAtom = None try: wndClassAtom = win32gui.RegisterClass(wndClass) except Exception as e: raise e displayWidth = win32api.GetSystemMetrics(0) displayHeigh = win32api.GetSystemMetrics(1) self._splash_screen_height = 100 self._splash_screen_width = displayWidth // 4 windowPosX = (displayWidth - self._splash_screen_width) // 2 windowPosY = (displayHeigh - self._splash_screen_height) // 2 hWindow = win32gui.CreateWindow( wndClassAtom, #it seems message dispatching only works with the atom, not the class name 'Bleachbit splash screen', win32con.WS_POPUPWINDOW | win32con.WS_VISIBLE, windowPosX, windowPosY, self._splash_screen_width, self._splash_screen_height, 0, 0, hInstance, None) is_splash_screen_on_top = self._force_set_foreground_window(hWindow) logger.debug( 'Is splash screen on top: {}'.format(is_splash_screen_on_top) ) return hWindow
def hightligt_window(handle): left, top, right, bottom = win32gui.GetWindowRect(handle) rectanglePen = win32gui.CreatePen(win32con.PS_SOLID, 3, win32api.RGB(255, 0, 0)) windowDc = win32gui.GetWindowDC(handle) if windowDc: prevPen = win32gui.SelectObject(windowDc, rectanglePen) prevBrush = win32gui.SelectObject( windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH)) win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top) win32gui.SelectObject(windowDc, prevPen) win32gui.SelectObject(windowDc, prevBrush) win32gui.ReleaseDC(handle, windowDc)
def _init_win32(self): """Initialize the window and its attributes""" self._h_instance = api.GetModuleHandle() self._window_class = gui.WNDCLASS() self._window_class.style = con.CS_HREDRAW | con.CS_VREDRAW self._window_class.lpfnWndProc = self._draw self._window_class.hInstance = self._h_instance self._window_class.hCursor = gui.LoadCursor(None, con.IDC_ARROW) self._window_class.hbrBackground = gui.GetStockObject(con.WHITE_BRUSH) random.seed(time.time()) rand = random.randint(0, sys.maxsize) self._window_class.lpszClassName = "{}_{}".format( self._window_class, rand) self._window_class_atom = gui.RegisterClass(self._window_class)
def main(): # get instance handle hInstance = win32api.GetModuleHandle() print(hInstance) # the class name className = 'SimpleWin32' # create and initialize window class wndClass = win32gui.WNDCLASS() print(wndClass) wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.lpfnWndProc = wndProc wndClass.hInstance = hInstance wndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className print(wndClass) # register window class wndClassAtom = None try: wndClassAtom = win32gui.RegisterClass(wndClass) print(wndClassAtom) except Exception as e: print(e) raise e hWindow = win32gui.CreateWindow( wndClassAtom, # it seems message dispatching only works with the atom, not the # class name 'FullGenReport', win32con.WS_OVERLAPPEDWINDOW, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, None, None, hInstance, None) print(hWindow) # Show & update the window win32gui.ShowWindow(hWindow, win32con.SW_HIDE) win32gui.UpdateWindow(hWindow) # Dispatch messages win32gui.PumpMessages()
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.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) 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, style, 0, # x 0, # y win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height None, # hWndParent None, # hMenu hInstance, None # lpParam ) win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) ####### COLOR win32gui.SetWindowPos( hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) thr = threading.Thread(target=customDraw, args=(hWindow, )) thr.setDaemon(False) thr.start() win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL) win32gui.UpdateWindow(hWindow) timer.set_timer(10000, customDraw) win32gui.PumpMessages()
def _win32_setup(self): hInstance = win32api.GetModuleHandle() className = str(uuid.uuid4()) # probably a better way to do this # 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 = self._win_message 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) return wndClassAtom, hInstance
def GetWndClassEx(self, hInstance, WndProc, szWindowClass='DesktopApp', Transparent=True): WndClassEx = WNDCLASSEX() WndClassEx.cbSize = ctypes.sizeof(WNDCLASSEX) WndClassEx.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW WndClassEx.lpfnWndProc = WNDPROCTYPE(WndProc) WndClassEx.cbClsExtra = 0 WndClassEx.cbWndExtra = 0 WndClassEx.hInstance = hInstance WndClassEx.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) WndClassEx.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) if Transparent: WndClassEx.hbrBackground = self.RGB(0,0,0) else: WndClassEx.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) WndClassEx.lpszMenuName = None WndClassEx.lpszClassName = szWindowClass WndClassEx.hIconSm = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) return WndClassEx
def activate(): bkgndWndClass = win32gui.WNDCLASS() bkgndWndClass.hbrBackground = win32gui.GetStockObject(win32con.BLACK_BRUSH) bkgndWndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) bkgndWndClass.lpszClassName = 'VimFullscreenBkgnd' class_atom = win32gui.RegisterClass(bkgndWndClass) top_wnd = win32gui.FindWindow('Vim', None) mon = win32api.MonitorFromRect(win32gui.GetWindowRect(top_wnd), win32con.MONITOR_DEFAULTTONEAREST) (x, y, dx, dy) = win32api.GetMonitorInfo(mon)['Monitor'] old_window_placement = win32gui.GetWindowPlacement(top_wnd) old_style = win32api.GetWindowLong(top_wnd, win32con.GWL_STYLE) old_exstyle = win32api.GetWindowLong(top_wnd, win32con.GWL_EXSTYLE) win32api.SetWindowLong( top_wnd, win32con.GWL_STYLE, old_style & ~(win32con.WS_CAPTION | win32con.WS_BORDER | win32con.WS_THICKFRAME)) win32api.SetWindowLong(top_wnd, win32con.GWL_EXSTYLE, old_exstyle & ~win32con.WS_EX_WINDOWEDGE) win32gui.SetWindowPos(top_wnd, win32con.HWND_TOP, x, y, dx - x, dy - y, win32con.SWP_SHOWWINDOW | win32con.SWP_FRAMECHANGED) # Black background bkgnd_wnd = win32gui.CreateWindow(class_atom, '', win32con.WS_CHILD, 0, 0, dx - x, dy - y, top_wnd, None, win32api.GetModuleHandle(None), None) win32gui.SetWindowPos( bkgnd_wnd, win32con.HWND_BOTTOM, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) restore_data = { 'window_placement': old_window_placement, 'style': old_style, 'exstyle': old_exstyle, 'class_atom': class_atom, 'bkgnd_wnd': bkgnd_wnd } vim.vars.update({'fullscreen_restoredata': restore_data}) vim.vars.update({'fullscreen_active': 1})
def __init__(self): # message map message_map = { # msg_TaskbarRestart: self.OnRestart, # win32con.WM_DESTROY: self.OnDestroy, CA_WMCAEVENT: self.wnd_proc, CA_RECEIVEERROR: self.wnd_proc, # win32con.WM_COMMAND: self.on_command, ON_TASKBAR_NOTIFY: self.on_taskbar_notify, } # Define Window Class wc = win32gui.WNDCLASS() wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.lpfnWndProc = message_map wc.hInstance = win32gui.GetModuleHandle(None) wc.hIcon = win32gui.LoadIcon(None, IDI_APPLICATION) wc.hCursor = win32gui.LoadCursor(None, IDC_ARROW) wc.hbrBackground = win32gui.GetStockObject(WHITE_BRUSH) wc.lpszClassName = "MainWin" # Register Window Class if not win32gui.RegisterClass(wc): raise WinError() # Create Window self.hwnd = win32gui.CreateWindow(wc.lpszClassName, "Namuh Window", wc.style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, wc.hInstance, None) # Show Window # win32gui.ShowWindow(self.hwnd, SW_SHOWNORMAL) win32gui.UpdateWindow(self.hwnd) # self.create_tray_icons() # 트레이 아이콘 생성 self.wmca = WinDllWmca() # 모바일증권 나무 DLL 로드 print("NamuhWindow START!")
def on_paint(self, hwnd, message, wparam, lparam): """Draw current selection rectangle.""" hdc, paint = win32gui.BeginPaint(hwnd) # Selection not started yet if self._selection is None: win32gui.EndPaint(hwnd, paint) return 0 brush = win32gui.GetStockObject(win32con.NULL_BRUSH) pen = win32gui.CreatePen(win32con.PS_SOLID, self.REGION_WIDTH, self.REGION_COLOR) brush_default = win32gui.SelectObject(hdc, brush) pen_default = win32gui.SelectObject(hdc, pen) win32gui.Rectangle(hdc, *self._selection) win32gui.SelectObject(hdc, pen_default) win32gui.SelectObject(hdc, brush_default) win32gui.EndPaint(hwnd, paint) return 0
def _thread_function(x, y, w, h, delay): [x, y, w, h] = map(int, [x, y, w, h]) delay = float(delay) # Получим контекст всех дисплев или всего рабочего стола: #scr_hdc = win32gui.GetDC(0) scr_hdc = win32gui.CreateDC('DISPLAY', None, None) mem_hdc = win32gui.CreateCompatibleDC( scr_hdc ) # New context of memory device. This one is compatible with 'scr_hdc' new_bitmap_h = win32gui.CreateCompatibleBitmap(scr_hdc, w + 2, h + 2) win32gui.SelectObject( mem_hdc, new_bitmap_h ) # Returns 'old_bitmap_h'. It will be deleted automatically. # Сохраняем рамочку в 1 пиксель (она вокруг области (x,y,w,h)): _cp_boundary(mem_hdc, 0, 0, scr_hdc, x - 1, y - 1, w + 2, h + 2) # Рисуем подсветку области: # brush = win32gui.CreateSolidBrush(win32api.RGB(255,0,0)) win32gui.SelectObject(scr_hdc, win32gui.GetStockObject(win32con.NULL_BRUSH)) pen = win32gui.CreatePen(win32con.PS_DOT, 1, win32api.RGB(148, 0, 0)) win32gui.SelectObject(scr_hdc, pen) for i in range(2): win32gui.Rectangle(scr_hdc, x - 1, y - 1, x + w + 1, y + h + 1) # Восстаналиваема рамочку: time.sleep(delay) _cp_boundary(scr_hdc, x - 1, y - 1, mem_hdc, 0, 0, w + 2, h + 2) #win32gui.ReleaseDC(scr_hdc, 0) win32gui.DeleteDC(scr_hdc) win32gui.DeleteDC(mem_hdc) win32gui.DeleteObject(new_bitmap_h)
def drawRect(pos, pred, colors): hwnd = win32gui.GetDesktopWindow() # 定义框颜色 hPen = win32gui.CreatePen(win32con.PS_SOLID, 3, win32api.RGB(colors[pred][0], colors[pred][1], colors[pred][2])) win32gui.InvalidateRect(hwnd, None, True) win32gui.UpdateWindow(hwnd) win32gui.RedrawWindow(hwnd, None, None, win32con.RDW_FRAME | win32con.RDW_INVALIDATE | win32con.RDW_UPDATENOW | win32con.RDW_ALLCHILDREN) # 根据窗口句柄获取窗口的设备上下文DC(Divice Context) hwndDC = win32gui.GetDC(hwnd) win32gui.SelectObject(hwndDC, hPen) # 定义透明画刷,这个很重要! hbrush = win32gui.GetStockObject(win32con.NULL_BRUSH) prebrush = win32gui.SelectObject(hwndDC, hbrush) # 左上到右下的坐标 win32gui.Rectangle(hwndDC, pos[0] - 240, pos[1] - 135, pos[0] + 240, pos[1] + 135) win32gui.SaveDC(hwndDC) win32gui.SelectObject(hwndDC, prebrush) # 回收资源 win32gui.DeleteObject(hPen) win32gui.DeleteObject(hbrush) win32gui.DeleteObject(prebrush) win32gui.ReleaseDC(hwnd, hwndDC)
def WinMain(self): hInstance = win32api.GetModuleHandle() wndClass = win32gui.WNDCLASS() wndClass.hInstance = hInstance wndClass.lpszClassName = self.szClassName wndClass.lpfnWndProc = self.WndProc wndClass.style = win32con.CS_DBLCLKS wndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndClass.cbWndExtra = 0 wndClass.hbrBackground = win32gui.GetStockObject(win32con.BLACK_BRUSH) tWnd = win32gui.FindWindow(0, self.TargetName) if tWnd == 0: exit(1) # 시작시 타켓윈도우 없으면 종료 # while tWnd == 0: # tWnd = win32gui.FindWindow(0, self.TargetName) # 타겟윈도우 찾을때까지 반복 tRect = win32gui.GetWindowRect(tWnd) wWidth = tRect[2] - tRect[0] wHeight = tRect[3] - tRect[1] if not tWnd: wWidth = 400 wHeight = 350 wndClassAtom = None try: wndClassAtom = win32gui.RegisterClass(wndClass) except Exception as e: print(e) raise e self.hWnd = win32gui.CreateWindowEx( win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT, wndClassAtom, "Overlay", win32con.WS_POPUP, 100, 90, wWidth, wHeight, 0, 0, hInstance, None) #hWnd # Show & update the window win32gui.ShowWindow(self.hWnd, win32con.SW_SHOW) win32gui.UpdateWindow(self.hWnd) win32gui.SetWindowLong( self.hWnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(self.hWnd, win32con.GWL_EXSTYLE) ^ win32con.WS_EX_LAYERED) win32gui.SetLayeredWindowAttributes(self.hWnd, win32api.RGB(0, 0, 0), 0, win32con.LWA_COLORKEY) # win32ui.CreateThread(SetWindow, hWnd) t = threading.Thread(target=self.SetWindow, args=(self.hWnd, )) t.daemon = True t.start() #msg pump msg = win32gui.GetMessage(self.hWnd, 0, 0) msg = msg[1] while msg[1]: win32gui.TranslateMessage(msg) win32gui.DispatchMessage(msg) msg = win32gui.GetMessage(self.hWnd, 0, 0) msg = msg[1] if self.flag == True: exit(0)
def _win_stock_font(id): h = gui.GetStockObject(id) lf = gui.GetObject(h) return Font._from_win_logfont(lf)
pPixelData = np.zeros([size, size, 4], np.uint8) glPixelStorei(GL_PACK_ALIGNMENT, 1) glReadPixels(0, 0, size, size, GL_RGBA, GL_UNSIGNED_BYTE, pPixelData) image = cv2.flip(pPixelData, 0, dst=None) # return image path = './Tsinghua_examples/' + name cv2.imwrite(path, image) for i in range(len(realx)): hInstance = win32api.GetModuleHandle(None) wndClass = win32gui.WNDCLASS() # wndClass.lpfnWndProc = win32gui.DefWindowProc() wndClass.hInstance = hInstance wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndClass.lpszClassName = str(uuid.uuid4()) wndClass.style = win32con.CS_OWNDC wndClassAtom = win32gui.RegisterClass(wndClass) # 隐藏窗口的宽和高 width, height = size, size hWnd = win32gui.CreateWindow(wndClassAtom, '', win32con.WS_POPUP, 0, 0, width, height, 0, 0, hInstance, None) # Ok, window created, now we can create OpenGL context PFD_TYPE_RGBA = 0 PFD_MAIN_PLANE = 0 PFD_DOUBLEBUFFER = 0x00000001
print( "client point:", win32gui.GetClientRect(hwnd)) parent = win32gui.GetParent( hwnd ) active = win32gui.GetActiveWindow() print("window placement: ", win32gui.GetWindowPlacement(hwnd)) childwindow = win32gui.ChildWindowFromPoint(hwnd, win32gui.GetCursorPos()) print( "capture : ", win32gui.GetCapture()) window = win32gui.GetWindow(hwnd, 1) win32gui.SendMessage( hwnd, 1, None, None) desktop = win32gui.GetDesktopWindow() menu = win32gui.GetMenu(hwnd) cursorinfo = win32gui.GetCursorInfo() foreground = win32gui.GetForegroundWindow() hdc = win32gui.GetDC(hwnd) #win32gui.PolylineTo(hdc, ((w0, h0), (w1, h0), (w1, h1), (w0, h1), (w0,h0))) obj = win32gui.GetCurrentObject(hdc, win32con.OBJ_BITMAP) stockobj = win32gui.GetStockObject(obj) sysmenu = win32gui.GetSystemMenu(hwnd, 1) print("1. desktop : ", desktop) print("2. window : ", window) print("3. hwnd : ", hwnd ) print("4. parent : ", parent) print("5. child : ", childwindow) print("6. active : ", active) print("7. focus : ", focus) print("8. getMenu : ", menu) print("9. CursorInfo : ", cursorinfo)
def mouse_pro(nCode, wParam, lParam): print('mouse_pro') """ 函数功能:鼠标钩子函数,当有鼠标事件,此函数被回调 """ if nCode == win32con.HC_ACTION: MSLLHOOKSTRUCT_p = POINTER(MSLLHOOKSTRUCT) param = cast(lParam, MSLLHOOKSTRUCT_p) # 鼠标左键点击 if wParam == win32con.WM_LBUTTONDOWN: hForeWnd = win32gui.GetForegroundWindow() dwSelfThreadId = win32api.GetCurrentThreadId() print(dwSelfThreadId) dwForeThreadId = win32process.GetWindowThreadProcessId(hForeWnd) print(dwForeThreadId) win32process.AttachThreadInput(dwForeThreadId[1], dwSelfThreadId, True) hw = win32api.GetFocus() win32process.AttachThreadInput(dwForeThreadId[1], dwSelfThreadId, False) # hw = win32gui.WindowFromPoint(win32api.GetCursorPos()) print("%s#######################%s" % (hw, mouse_hd)) print(win32gui.GetClassName(hw)) left, top, right, bottom = win32gui.GetWindowRect(hw) print(left, top, right, bottom) windowDc = win32gui.GetWindowDC(hw) if windowDc: prevPen = win32gui.SelectObject(windowDc, rectanglePen) prevBrush = win32gui.SelectObject(windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH)) win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top) win32gui.SelectObject(windowDc, prevPen) win32gui.SelectObject(windowDc, prevBrush) win32gui.ReleaseDC(hw, windowDc) # hw = win32gui.WindowFromPoint((int(param.contents.pt.x),int(param.contents.pt.y))) # print("%s#######################%s" % (hw, mouse_hd)) # print(win32gui.GetClassName(hw)) # left, top, right, bottom = win32gui.GetWindowRect(hw) # print(left, top, right, bottom) # print("左键点击,坐标:x:%d,y:%d" % (param.contents.pt.x,param.contents.pt.y)) # hwndChildList = get_child_windows(hw) # for hwndChild in hwndChildList: # hw = hwndChild # print("%s#######################%s" % (hw, mouse_hd)) # print(win32gui.GetClassName(hw)) # left, top, right, bottom = win32gui.GetWindowRect(hw) # print(left, top, right, bottom) # windowDc = win32gui.GetWindowDC(hw) # if windowDc: # prevPen = win32gui.SelectObject(windowDc, rectanglePen) # prevBrush = win32gui.SelectObject(windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH)) # # win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top) # win32gui.SelectObject(windowDc, prevPen) # win32gui.SelectObject(windowDc, prevBrush) # win32gui.ReleaseDC(hw, windowDc) # elif wParam == win32con.WM_LBUTTONUP: # print("左键抬起,坐标:x:%d,y:%d" % (param.contents.pt.x, param.contents.pt.y)) # elif wParam == win32con.WM_MOUSEMOVE: # hw = win32gui.WindowFromPoint(win32api.GetCursorPos()) # print("%s#######################%s" % (hw, mouse_hd)) # print(win32gui.GetClassName(hw)) # left, top, right, bottom = win32gui.GetWindowRect(hw) # print(left, top, right, bottom) # windowDc = win32gui.GetWindowDC(hw) # if windowDc: # prevPen = win32gui.SelectObject(windowDc, rectanglePen) # prevBrush = win32gui.SelectObject(windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH)) # # win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top) # win32gui.SelectObject(windowDc, prevPen) # win32gui.SelectObject(windowDc, prevBrush) # win32gui.ReleaseDC(hw, windowDc) # # # hw = win32gui.WindowFromPoint((int(param.contents.pt.x),int(param.contents.pt.y))) # # print("%s#######################%s" % (hw, mouse_hd)) # # print(win32gui.GetClassName(hw)) # # left, top, right, bottom = win32gui.GetWindowRect(hw) # # print(left, top, right, bottom) # # print("鼠标移动,坐标:x:%d,y:%d" % (param.contents.pt.x, param.contents.pt.y)) # elif wParam == win32con.WM_RBUTTONDOWN: # print("右键点击,坐标:x:%d,y:%d" % (param.contents.pt.x, param.contents.pt.y)) # elif wParam == win32con.WM_RBUTTONUP: # print("右键抬起,坐标:x:%d,y:%d" % (param.contents.pt.x, param.contents.pt.y)) return CallNextHookEx(mouse_hd, nCode, wParam, lParam)
def createWindow(self): #get instance handle hInstance = win32api.GetModuleHandle() # the class name className = 'Cooldown Overlay' # create and initialize window class wndClass = win32gui.WNDCLASS() wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wndClass.hInstance = hInstance wndClass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wndClass.lpszClassName = className wndClass.lpfnWndProc = self.wndProc # register window class wndClassAtom = None try: wndClassAtom = win32gui.RegisterClass(wndClass) except Exception as e: print(e) raise e # 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 hWindow = win32gui.CreateWindowEx( exStyle, wndClassAtom, #it seems message dispatching only works with the atom, not the class name 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) # Show & update the window win32gui.ShowWindow(hWindow, win32con.SW_SHOWNORMAL) win32gui.UpdateWindow(hWindow) return hWindow
def run(self): def wnd_proc(h_wnd, message, w_param, l_param): """Displays a transparent window with some graphic elements Displays a transparent window with some graphic elements :param h_wnd: an input argument :returns: nothing """ if message == win32con.WM_QUIT: # print("Closing the window overlay") win32gui.PostQuitMessage(0) return 0 if message == win32con.WM_PAINT: hdc, paint_struct = win32gui.BeginPaint(h_wnd) win32gui.SetGraphicsMode(hdc, win32con.GM_ADVANCED) win32gui.BringWindowToTop(h_wnd) for r in self.graphical_elements: if 'geometry' in r: geometry = r['geometry'] else: geometry = None if 'x' in r: x = r['x'] - self.x_min else: x = 0 if 'y' in r: y = r['y'] - self.y_min else: y = 0 if 'width' in r: width = r['width'] else: width = 100 if 'height' in r: height = r['height'] else: height = 100 if 'xyrgb_array' in r: xyrgb_array = r['xyrgb_array'] else: xyrgb_array = ((15, 15, 255, 0, 0), (15, 45, 0, 255, 0), (45, 30, 0, 0, 255)) if 'color' in r: color_r = r['color'][0] color_g = r['color'][1] color_b = r['color'][2] else: color_r = 255 color_g = 0 color_b = 0 if 'thickness' in r: thickness = r['thickness'] else: thickness = 0 if 'angle' in r: angle = r['angle'] else: angle = 0 if 'geometry' in r and r['geometry'] is Shape.triangle: vertices = () for xyrgb in xyrgb_array: vertices = vertices + ( { 'x': int(round(xyrgb[0])) - self.x_min, 'y': int(round(xyrgb[1])) - self.y_min, 'Red': xyrgb[2] * 256, 'Green': xyrgb[3] * 256, 'Blue': xyrgb[4] * 256, 'Alpha': 0 }, ) mesh = ((0, 1, 2), ) win32gui.GradientFill(hdc, vertices, mesh, win32con.GRADIENT_FILL_TRIANGLE) if 'brush_color' in r: brush_color_r = r['brush_color'][0] brush_color_g = r['brush_color'][1] brush_color_b = r['brush_color'][2] else: brush_color_r = 255 brush_color_g = 255 brush_color_b = 255 if thickness == 0: color_r = brush_color_r color_g = brush_color_g color_b = brush_color_b if 'font_size' in r: font_size = r['font_size'] else: font_size = 18 if 'font_name' in r: font_name = r['font_name'] else: font_name = "Arial" my_brush = None if 'brush' in r and width > 1 and height > 1: brush = r['brush'] brush_color = win32api.RGB(brush_color_r, brush_color_g, brush_color_b) if brush is Brush.solid: my_brush = win32gui.CreateSolidBrush(brush_color) elif brush is Brush.b_diagonal: my_brush = win32gui.CreateHatchBrush( win32con.HS_BDIAGONAL, brush_color) elif brush is Brush.cross: my_brush = win32gui.CreateHatchBrush( win32con.HS_CROSS, brush_color) elif brush is Brush.diag_cross: my_brush = win32gui.CreateHatchBrush( win32con.HS_DIAGCROSS, brush_color) elif brush is Brush.f_diagonal: my_brush = win32gui.CreateHatchBrush( win32con.HS_FDIAGONAL, brush_color) elif brush is Brush.horizontal: my_brush = win32gui.CreateHatchBrush( win32con.HS_HORIZONTAL, brush_color) elif brush is Brush.vertical: my_brush = win32gui.CreateHatchBrush( win32con.HS_VERTICAL, brush_color) old_brush = win32gui.SelectObject(hdc, my_brush) pen = win32gui.CreatePen( win32con.PS_GEOMETRIC, thickness, win32api.RGB(color_r, color_g, color_b)) old_pen = win32gui.SelectObject(hdc, pen) if 'center_of_rotation' in r: center_of_rotation_x = r['center_of_rotation'][0] center_of_rotation_y = r['center_of_rotation'][1] else: center_of_rotation_x = 0 center_of_rotation_y = 0 if angle != 0: r_angle = angle * (math.pi / 180) Py_XFORM = win32gui.GetWorldTransform(hdc) win32gui.SetWorldTransform( hdc, { 'M11': math.cos(r_angle), 'M12': math.sin(r_angle), 'M21': math.sin(r_angle) * -1, 'M22': math.cos(r_angle), 'Dx': x, 'Dy': y }) x, y = -center_of_rotation_x, -center_of_rotation_y if 'text_format' in r: text_format = eval(r['text_format']) else: text_format = win32con.DT_CENTER | win32con.DT_SINGLELINE | win32con.DT_VCENTER if 'geometry' in r: if r['geometry'] is Shape.rectangle: win32gui.Rectangle(hdc, int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height))) elif r['geometry'] is Shape.ellipse: win32gui.Ellipse(hdc, int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height))) elif r['geometry'] is Shape.arrow: a = thickness t = ((x - int(a * 1.4), y), (x - a * 4, y + a * 3), (x, y), (x - a * 4, y - a * 3), (x - int(a * 1.4), y), (x - a * 9, y)) win32gui.Polyline(hdc, t) elif r['geometry'] is Shape.image: hicon = r['hicon'] win32gui.DrawIconEx(hdc, x, y, hicon, 0, 0, 0, None, 0x0003) elif r['geometry'] is Shape.triangle and thickness > 0: t = () for xyrgb in xyrgb_array: t = t + ((int(round( xyrgb[0])), +int(round(xyrgb[1]))), ) t = t + ((int(round(xyrgb_array[0][0])), int(round(xyrgb_array[0][1]))), ) win32gui.Polyline(hdc, t) if angle != 0: win32gui.SetWorldTransform(hdc, Py_XFORM) win32gui.SelectObject(hdc, old_pen) if 'brush' in r and width > 1 and height > 1: win32gui.SelectObject(hdc, old_brush) if 'text' in r: text = r['text'] lf = win32gui.LOGFONT() lf.lfFaceName = font_name lf.lfHeight = font_size lf.lfWeight = win32con.FW_NORMAL lf.lfQuality = win32con.ANTIALIASED_QUALITY hf = win32gui.CreateFontIndirect(lf) old_font = win32gui.SelectObject(hdc, hf) if 'text_color' in r: text_color_r = r['text_color'][0] text_color_g = r['text_color'][1] text_color_b = r['text_color'][2] else: text_color_r = 0 text_color_g = 0 text_color_b = 0 win32gui.SetTextColor( hdc, win32api.RGB(text_color_r, text_color_g, text_color_b)) if 'text_bg_color' in r: text_bg_color_r = r['text_bg_color'][0] text_bg_color_g = r['text_bg_color'][1] text_bg_color_b = r['text_bg_color'][2] else: text_bg_color_r = brush_color_r text_bg_color_g = brush_color_g text_bg_color_b = brush_color_b win32gui.SetBkMode(hdc, win32con.TRANSPARENT) win32gui.SetBkColor( hdc, win32api.RGB(text_bg_color_r, text_bg_color_g, text_bg_color_b)) tuple_r = tuple([ int(round(x)), int(round(y)), int(round(x + width)), int(round(y + height)) ]) win32gui.DrawTextW(hdc, text, -1, tuple_r, text_format) win32gui.SelectObject(hdc, old_font) win32gui.EndPaint(h_wnd, paint_struct) return 0 else: return win32gui.DefWindowProc(h_wnd, message, w_param, l_param) h_instance = win32api.GetModuleHandle() wnd_class = win32gui.WNDCLASS() wnd_class.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wnd_class.lpfnWndProc = wnd_proc wnd_class.hInstance = h_instance wnd_class.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW) wnd_class.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH) wnd_class.lpszClassName = self.class_name wnd_class_atom = win32gui.RegisterClass(wnd_class) ex_style = 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 self.h_window = win32gui.CreateWindowEx( ex_style, wnd_class_atom, 'OverlayWindow', style, win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN), # x win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN), # y win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN), # width win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN), # height None, # hWndParent None, # hMenu h_instance, None # lpParam ) monitors = win32api.EnumDisplayMonitors() self.x_min = min([ win32api.GetMonitorInfo(monitor[0])['Work'][0] for monitor in monitors ]) self.y_min = min([ win32api.GetMonitorInfo(monitor[0])['Work'][1] for monitor in monitors ]) win32gui.SetLayeredWindowAttributes( self.h_window, 0x00ffffff, self.transparency, win32con.LWA_COLORKEY | win32con.LWA_ALPHA) win32gui.SetWindowPos( self.h_window, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW) if self.period > 0: self.auto_refresh() #print("PumpMessages start") win32gui.PumpMessages()
def create_window(self): def wndProc(hWnd, message, wParam, lParam): if message == self.MESSAGE: if lParam == win32con.WM_RBUTTONUP: self.show_menu() return 0 elif message == win32con.WM_COMMAND: self.execute_menu_item(win32gui.LOWORD(wParam)) return 0 elif message == self.__MESSAGE_TC: # TaskbarCreated message indicates all tray icons have been removed, # so we should add our tray icon again, not updating it. self.__NOTIFY_ID = None self.update_tray_icon() return 0 elif message == win32con.WM_PAINT: hdc, paintStruct = win32gui.BeginPaint(hWnd) if self.font is None: self.init_font(hdc, paintStruct) # Set the font win32gui.SelectObject(hdc, self.font) text = str(self.key_count) # Clear window content win32gui.DefWindowProc(hWnd, message, wParam, lParam) # Dynamically change window size & position if necessary text_extent = win32gui.GetTextExtentPoint32(hdc, text) window_rect = win32gui.GetClientRect(hWnd) window_width = window_rect[2] - window_rect[0] window_height = window_rect[3] - window_rect[1] if window_width != text_extent[0]\ or window_height != text_extent[1]: pass _, _, screen_width, screen_height = get_workarea_rect() win32gui.SetWindowPos( self.HWND, None, screen_width - text_extent[0], # x screen_height - text_extent[1], # y text_extent[0], # width text_extent[1], # height 0) # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx win32gui.DrawText( hdc, text, len(text), # somehow -1 does not work tuple(win32gui.GetClientRect(hWnd)), (win32con.DT_BOTTOM | win32con.DT_NOCLIP | win32con.DT_SINGLELINE | win32con.DT_RIGHT)) self.__last_text_extent = text_extent win32gui.EndPaint(hWnd, paintStruct) return 0 elif message == win32con.WM_CLOSE: self.log('Window is closing, saving data now') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) # The operating system wants to end the session elif message == win32con.WM_QUERYENDSESSION: self.log('Session might end soon, saving data now') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) # The operating system is ending the session elif message == win32con.WM_ENDSESSION: self.log('Session %s ending', 'is' if wParam == win32con.TRUE else 'is not') super(KeyCounter, self).stop() return win32gui.DefWindowProc(hWnd, message, wParam, lParam) else: return win32gui.DefWindowProc(hWnd, message, wParam, lParam) hInstance = win32api.GetModuleHandle() className = 'TransparentWindow' # 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) # 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 | win32con.WS_EX_TOOLWINDOW) # 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 _, _, screen_width, screen_height = get_workarea_rect() # We'll update window size if we need more space init_width = 1 init_height = 1 hWindow = win32gui.CreateWindowEx( exStyle, wndClassAtom, None, # WindowName style, screen_width - init_width, # x screen_height - init_height, # y init_width, # width init_height, # height None, # hWndParent None, # hMenu hInstance, None # lpParam ) self.HWND = hWindow # Foreground transparency of 208 looks good # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx win32gui.SetLayeredWindowAttributes( self.HWND, 0x00ffffff, 208, # foreground transparency, 255 means opaque win32con.LWA_COLORKEY | win32con.LWA_ALPHA) # Transparent background win32gui.SetBkMode(hWindow, win32con.TRANSPARENT) # 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, screen_width - init_width, # x screen_height - init_height, # y init_width, # width init_height, # height (win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW))
class ComponentWrapper(AbstractWrapper): # mixin class, implementing the backend methods #_height = -1 # -1 means default size in wxPython #_width = -1 #_x = -1 #_y = -1 _win_style_ex = 0 _hfont = win32gui.GetStockObject(win32con.ANSI_VAR_FONT) def __init__(self, *args, **kws): self._width = 0 self._height = 0 AbstractWrapper.__init__(self, *args, **kws) self.setConstraints('container', 'x', 'y', 'width', 'height', 'text', 'selection', 'geometry', 'visible') def widgetFactory(self, *args, **kws): app = application() if hasattr(self.proxy.container, 'wrapper'): parent = self.proxy.container.wrapper.widget else: parent = 0 widget = win32gui.CreateWindowEx( self._win_style_ex, self._wndclass, "", self._win_style, 0, 0, 10, 10, parent, 0, # hMenu 0, # hInstance None) app.widget_map[widget] = self return widget def widgetSetUp(self): self.proxy.container.wrapper.widget_map[self.widget] = self win32gui.SendMessage(self.widget, win32con.WM_SETFONT, self._hfont, 0) self.setVisible(1) def internalProd(self): self.proxy.push(blocked=['container']) def getGeometry(self): l, t, r, b = win32gui.GetWindowRect(self.widget) w = r - l h = b - t try: l, t = win32gui.ScreenToClient(self.proxy.container.wrapper.widget, (l, t)) except AttributeError: pass except: import traceback traceback.print_exc(1) return l, t, w, h def setX(self, x): if not self.widget: return ox, y, w, h = self.getGeometry() self.setGeometry(x, y, w, h) def setY(self, y): if not self.widget: return x, oy, w, h = self.getGeometry() self.setGeometry(x, y, w, h) def setWidth(self, width): if not self.widget: return x, y, ow, h = self.getGeometry() self.setGeometry(x, y, width, h) def setHeight(self, height): if not self.widget: return x, y, w, oh = self.getGeometry() self.setGeometry(x, y, w, height) def setSize(self, width, height): if not self.widget: return x, y, w, h = self.getGeometry() self.setGeometry(x, y, width, height) def setPosition(self, x, y): if not self.widget: return ox, oy, w, h = self.getGeometry() self.setGeometry(x, y, w, h) def setGeometry(self, x, y, width, height): if not self.widget: return win32gui.SetWindowPos(self.widget, 0, x, y, width, height, win32con.SWP_NOACTIVATE | win32con.SWP_NOZORDER) def setVisible(self, visible): if not self.widget: return if visible: win32gui.ShowWindow(self.widget, win32con.SW_SHOWNORMAL) else: win32gui.ShowWindow(self.widget, win32con.SW_HIDE) def setEnabled(self, enabled): if not self.widget: return if enabled: win32gui.EnableWindow(self.widget, 1) else: win32gui.EnableWindow(self.widget, 0) def destroy(self): # @@@ if self.proxy.container: try: del self.proxy.container.wrapper.widget_map[self.widget] except: pass if self.widget: try: win32gui.DestroyWindow(self.widget) except: pass self.widget = None def setText(self, text): if not self.widget: return win32gui.SetWindowText(self.widget, str(text)) def getText(self): return win32gui.GetWindowText(self.widget) def setContainer(self, container): if container is None: try: self.destroy() except: pass return parent = container.wrapper.widget if parent: self.destroy() self.create(parent) self.proxy.push(blocked=['container']) def enterMainLoop(self): self.proxy.push() def _WM_PAINT(self, hwnd, msg, wParam, lParam): return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)