def SetWaitCursor(wait): import win32gui, win32con if wait: hCursor = win32gui.LoadCursor(0, win32con.IDC_WAIT) else: hCursor = win32gui.LoadCursor(0, 0) win32gui.SetCursor(hCursor)
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 __init__(self): message_map = {win32con.WM_DEVICECHANGE: self.onDeviceChange} wc = win32gui.WNDCLASS() hinst = wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = "DeviceChangeDemo" wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = message_map classAtom = win32gui.RegisterClass(wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(classAtom, "Device Change Demo", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) #devIF = DEV_BROADCAST_DEVICEINTERFACE() #devIF.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE) #devIF.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE devIF = DEV_BROADCAST_VOLUME() devIF.dbcc_size = sizeof(DEV_BROADCAST_VOLUME) devIF.dbcc_devicetype = DBT_DEVTYP_VOLUME devIF.dbcv_unitmask = 0xFFFF devIF.dbcv_flags = DBTF_MEDIA RegisterDeviceNotification = windll.user32.RegisterDeviceNotificationW RegisterDeviceNotification.restype = HANDLE RegisterDeviceNotification.argtypes = [HANDLE, c_void_p, DWORD] #self.devNotifyHandle = RegisterDeviceNotification(hinst, byref(devIF), 0) self.devNotifyHandle = RegisterDeviceNotification( self.hwnd, byref(devIF), 0)
def __init__(s, icon, hover_text, menu_options, on_quit=None, default_menu_index=None, window_class_name=None, ): s.icon = icon s.hover_text = hover_text s.on_quit = on_quit menu_options = menu_options + (('Exit', None, s.QUIT),) s._next_action_id = s.FIRST_ID s.menu_actions_by_id = set() s.menu_options = s._add_ids_to_menu_options(list(menu_options)) s.menu_actions_by_id = dict(s.menu_actions_by_id) del s._next_action_id s.default_menu_index = (default_menu_index or 0) s.window_class_name = window_class_name or "SysTrayIconPy" message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): s.refresh_icon, win32con.WM_DESTROY: s.destroy, win32con.WM_COMMAND: s.command, win32con.WM_USER + 20: s.notify, } # 注册窗口类。 window_class = win32gui.WNDCLASS() window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = s.window_class_name window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map # 也可以指定wndproc. s.classAtom = win32gui.RegisterClass(window_class)
def __init__(self): #threading.Thread.__init__(self) # def run(self): message_map = { win32con.WM_DEVICECHANGE : self.onDeviceChange } wc = win32gui.WNDCLASS () hinst = wc.hInstance = win32api.GetModuleHandle (None) wc.lpszClassName = "DeviceChangeDemo" wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW; wc.hCursor = win32gui.LoadCursor (0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = message_map classAtom = win32gui.RegisterClass (wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow ( classAtom, "Device Change Demo", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None ) self.event.clear()
def __init__(self, stopThreadFlag, yesDeviceFlag): message_map = { win32con.WM_DEVICECHANGE: self.onDeviceChange, win32con.WM_DESTROY: self.onDestroy } Notification.stopThread = stopThreadFlag Notification.yesDevice = yesDeviceFlag wc = win32gui.WNDCLASS() hinst = wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = "DeviceChangeDemo" wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = message_map classAtom = win32gui.RegisterClass(wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow( classAtom, "Device Change Demo", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None )
def __init__(self, tip, ico, items): if not ico: raise self._callbacks = dict() self._keys = [] self._tip = tip self._ico = ico self._items = items windowClass = win32gui.WNDCLASS() windowClass.hInstance = win32gui.GetModuleHandle(None) windowClass.lpszClassName = "5ea86490-d4ec-11e1-9b23-0800200c9a66" windowClass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW windowClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) windowClass.hbrBackground = win32con.COLOR_WINDOW windowClass.lpfnWndProc = { win32con.WM_COMMAND: self._commandCallback, win32con.WM_USER + 20: self._notifyCallback } self._hwnd = win32gui.CreateWindow( win32gui.RegisterClass(windowClass), windowClass.lpszClassName, win32con.WS_OVERLAPPED | win32con.WS_SYSMENU, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, windowClass.hInstance, None) win32gui.Shell_NotifyIcon( win32gui.NIM_ADD, (self._hwnd, 0, win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, win32con.WM_USER + 20, win32gui.LoadImage( win32gui.GetModuleHandle(None), os.path.abspath(self._ico), win32con.IMAGE_ICON, 0, 0, win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE), self._tip))
def initialize(self): message_map = { win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart, win32con.WM_DESTROY: self.destroy, win32con.WM_COMMAND: self.command, win32con.WM_USER + 20: self.notify, } # Register the Window class. window_class = win32gui.WNDCLASS() hinst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map # could also specify a wndproc. classAtom = win32gui.RegisterClass(window_class) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow( classAtom, self.window_class_name, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None, ) win32gui.UpdateWindow(self.hwnd) self.notify_id = None self.refresh_icon()
def _RegisterWndClass(self): className = "PythonDocSearch" message_map = {} wc = win32gui.WNDCLASS() wc.SetDialogProc() # Make it a dialog class. wc.hInstance = self.hinst wc.lpszClassName = className wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW + 1 wc.lpfnWndProc = message_map # could also specify a wndproc. # C code: wc.cbWndExtra = DLGWINDOWEXTRA + sizeof(HBRUSH) + (sizeof(COLORREF)); wc.cbWndExtra = win32con.DLGWINDOWEXTRA + struct.calcsize("Pi") icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE ## py.ico went away in python 2.5, load from executable instead this_app = win32api.GetModuleHandle(None) try: wc.hIcon = win32gui.LoadIcon(this_app, 1) ## python.exe and pythonw.exe except win32gui.error: wc.hIcon = win32gui.LoadIcon(this_app, 135) ## pythonwin's icon try: classAtom = win32gui.RegisterClass(wc) except win32gui.error, err_info: if err_info.winerror != winerror.ERROR_CLASS_ALREADY_EXISTS: raise
def new_icon(hdesk,desktop_name): """ Runs as a thread on each desktop to create a new tray icon and handle its messages """ global id id=id+1 hdesk.SetThreadDesktop() ## apparently the threads can't use same hinst, so each needs its own window class windowclassname='PythonDesktopManager'+desktop_name wc = win32gui.WNDCLASS() wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = windowclassname wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW | win32con.CS_GLOBALCLASS wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW ) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = icon_wndproc windowclass = win32gui.RegisterClass(wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU hwnd = win32gui.CreateWindow(windowclass, 'dm_'+desktop_name, win32con.WS_SYSMENU, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, wc.hInstance, None) win32gui.UpdateWindow(hwnd) flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP notify_info = (hwnd, id, flags, win32con.WM_USER+20, hicon, 'Desktop Manager (%s)' %desktop_name) window_info[hwnd]=notify_info ## wait for explorer to initialize system tray for new desktop tray_found=0 while not tray_found: try: tray_found=win32gui.FindWindow("Shell_TrayWnd",None) except win32gui.error: traceback.print_exc time.sleep(.5) win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, notify_info) 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 create_window(self, window_name='Dummy Window'): message_map = { win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart, win32con.WM_DESTROY: self.destroy, win32con.WM_COMMAND: self.command, self.message_id : self.notify, } # Register the Window class. window_class = win32gui.WNDCLASS() window_class.lpszClassName = 'geobox_client_tray_icon' window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW ) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map class_atom = win32gui.RegisterClass(window_class) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU window_id = win32gui.CreateWindow( class_atom, window_name, style, 0, 0, 0, 0, 0, 0, None, None ) log.debug('Dummy window created') win32gui.UpdateWindow(window_id) return window_id
def __init__(self, icon, hover_text, menu_options=(), on_quit=None, window_class_name=None, gui=None): self.icon = icon self.hover_text = hover_text self.on_quit = on_quit self.gui = gui menu_options = menu_options + (('退出', None, self.QUIT), ) self._next_action_id = self.FIRST_ID self.menu_actions_by_id = set() self.menu_options = self._add_ids_to_menu_options(list(menu_options)) self.menu_actions_by_id = dict(self.menu_actions_by_id) del self._next_action_id self.window_class_name = window_class_name or "SysTrayIconPy" message_map = { win32gui.RegisterWindowMessage("TaskbarCreated"): self.refresh_icon, win32con.WM_DESTROY: self.destroy, win32con.WM_COMMAND: self.command, win32con.WM_USER + 20: self.notify, } window_class = win32gui.WNDCLASS() window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map self.classAtom = win32gui.RegisterClass(window_class)
def __init__(self, hoverText, icon): self.hotkeys = [] self.hoverText = hoverText self.window_class_name = "Notify icon" # Register the Window class. window_class = win32gui.WNDCLASS() hinst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.lpszClassName = self.window_class_name window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW classAtom = win32gui.RegisterClass(window_class) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hWindow = win32gui.CreateWindow(classAtom, self.window_class_name, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hWindow) self.notify_id = None self.draw_icon(icon)
def __init__(self, menu_options, tray_name="willSpeak"): # Create instance level variable self.menu_options = menu_options # Create task mappings for try events message_map = { win32gui.RegisterWindowMessage("TaskbarCreated"): self.OnRestart, win32con.WM_DESTROY: self.OnDestroy, win32con.WM_COMMAND: self.OnCommand, win32con.WM_USER + 20: self.OnTaskbarNotify, } # Register the Window class. window_class = win32gui.WNDCLASS() window_class.lpszClassName = tray_name hinst = window_class.hInstance = win32gui.GetModuleHandle(None) window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window_class.hbrBackground = win32con.COLOR_WINDOW window_class.lpfnWndProc = message_map classAtom = win32gui.RegisterClass(window_class) # Create the Window. style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(classAtom, tray_name, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hinst, None) win32gui.UpdateWindow(self.hwnd) self.notify_id = None self.create_icon() win32gui.PumpMessages()
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 create_window(title, class_name, width, height, window_proc, icon): # Register window class wndclass = win32gui.WNDCLASS() wndclass.hInstance = win32api.GetModuleHandle(None) wndclass.lpszClassName = class_name wndclass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wndclass.hbrBackground = win32con.COLOR_WINDOW wndclass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndclass.lpfnWndProc = window_proc atom_class = win32gui.RegisterClass(wndclass) assert (atom_class != 0) # Center window on screen. screenx = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screeny = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) xpos = int(math.floor((screenx * 0.2 - width) / 2)) ypos = int(math.floor((screeny - height) / 2)) if xpos < 0: xpos = 0 if ypos < 0: ypos = 0 # Create window window_style = (win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE) window_handle = win32gui.CreateWindow(class_name, title, window_style, xpos, ypos, width, height, 0, 0, wndclass.hInstance, None) assert (window_handle != 0) win32gui.SetWindowPos(window_handle, win32con.HWND_TOPMOST, xpos, ypos, width, height, win32con.SWP_SHOWWINDOW) # win32con.HWND_TOPMOST窗口置顶 # Window icon icon = os.path.abspath(icon) if not os.path.isfile(icon): icon = None if icon: # Load small and big icon. # WNDCLASSEX (along with hIconSm) is not supported by pywin32, # we need to use WM_SETICON message after window creation. # Ref: # 1. http://stackoverflow.com/questions/2234988 # 2. http://blog.barthe.ph/2009/07/17/wmseticon/ bigx = win32api.GetSystemMetrics(win32con.SM_CXICON) bigy = win32api.GetSystemMetrics(win32con.SM_CYICON) big_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, bigx, bigy, win32con.LR_LOADFROMFILE) smallx = win32api.GetSystemMetrics(win32con.SM_CXSMICON) smally = win32api.GetSystemMetrics(win32con.SM_CYSMICON) small_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, smallx, smally, win32con.LR_LOADFROMFILE) win32api.SendMessage(window_handle, win32con.WM_SETICON, win32con.ICON_BIG, big_icon) win32api.SendMessage(window_handle, win32con.WM_SETICON, win32con.ICON_SMALL, small_icon) return window_handle
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() # 获取当前的实例句柄 # 定义窗口类 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 __init__(self): self.visible = 0 self.events = { win32con.WM_DESTROY: self.onDestroy, win32con.WM_USER + 20: self.onTaskbarNotify, win32con.WM_COMMAND: self.onCommand # win32con.WM_USER+5 : self.onBalloonClick } self.windowtext = "window text" self.windowclassname = "window class name" window = win32gui.WNDCLASS() self.handlerInstance = window.hInstance = win32api.GetModuleHandle( None) window.lpszClassName = self.windowclassname window.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW window.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) window.hbrBackground = win32con.COLOR_WINDOW window.lpfnWndProc = self.events classAtom = win32gui.RegisterClass(window) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow(classAtom, self.windowtext, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, self.handlerInstance, None) win32gui.UpdateWindow(self.hwnd) self.setIcon()
def _run(self): message_map = { win32con.WM_DEVICECHANGE: self._on_device_change } wc = win32gui.WNDCLASS() wc.hInstance = win32api.GetModuleHandle(None) wc.lpszClassName = u"udisk-monitor" wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW wc.lpfnWndProc = message_map class_atom = win32gui.RegisterClass(wc) style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU self.hwnd = win32gui.CreateWindow( class_atom, u"udisk monitor", style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, wc.hInstance, None ) win32gui.PumpMessages()
def FGWNDREG(): wc = win32gui.WNDCLASS() wc.hbrBackground = win32con.COLOR_BTNFACE + 1 wc.hCursor = win32gui.LoadCursor(0,win32con.IDI_APPLICATION) wc.lpszClassName = "Python DeskTop FG On Windows" wc.lpfnWndProc = self.FGWndProc reg = win32gui.RegisterClass(wc) return reg
def _register_class(self): # register a window class for toplevel windows. wndclass = win32gui.WNDCLASS() wndclass.hbrBackground = win32con.COLOR_BTNFACE + 1 wndclass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndclass.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wndclass.lpszClassName = "msw.anygui.PythonWindow" wndclass.lpfnWndProc = self._wndproc self.__class__._wndclass = win32gui.RegisterClass(wndclass)
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 __init__(self, title="Untitled", style=None, exstyle=None, pos=(0, 0), size=(400, 400), background=None, message_map={}, cursor=None): global _g_class_num if style is None: style = win32con.WS_OVERLAPPEDWINDOW if exstyle is None: style = win32con.WS_EX_LEFT if background is None: background = win32con.COLOR_WINDOW if cursor is None: cursor = win32con.IDC_ARROW self._instance = win32api.GetModuleHandle(None) self.message_map = {win32con.WM_DESTROY: self._on_destroy} self.message_map.update(message_map) _g_class_num += 1 class_name = "class_name%d" % _g_class_num wc = win32gui.WNDCLASS() wc.hInstance = self._instance wc.lpfnWndProc = self.message_map # could also specify a wndproc wc.lpszClassName = class_name wc.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW wc.hbrBackground = background wc.cbWndExtra = 0 wc.hCursor = win32gui.LoadCursor(0, cursor) wc.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) class_atom = win32gui.RegisterClass(wc) # C code: # wc.cbWndExtra = DLGWINDOWEXTRA + sizeof(HBRUSH) + (sizeof(COLORREF)); #wc.cbWndExtra = win32con.DLGWINDOWEXTRA + struct.calcsize("Pi") #wc.hIconSm = 0 self._handle = win32gui.CreateWindowEx( exstyle, class_atom, title, style, # win32con.WS_POPUP, # | win32con.WS_EX_TRANSPARENT, pos[0], pos[1], size[0], size[1], 0, # no parent 0, # no menu self._instance, None)
def getCursorType(): """返回当前鼠标图标类型 :rtype: MouseCursorType """ hcursor = win32gui.GetCursorInfo()[1] for key, flag in _cursor_flags.items(): if win32gui.LoadCursor(0, flag) == hcursor: return key return None
def get_hwnd(): wc = win32gui.WNDCLASS() wc.hbrBackground = COLOR_BTNFACE + 1 wc.hCursor = win32gui.LoadCursor(0,IDI_APPLICATION) wc.lpszClassName = "MBPython" wc.style = CS_GLOBALCLASS|CS_VREDRAW | CS_HREDRAW wc.lpfnWndProc = WndProc reg = win32gui.RegisterClass(wc) hwnd = win32gui.CreateWindow(reg,'MBPython-webview绑定测试-1191826896',WS_CLIPCHILDREN|WS_CLIPSIBLINGS|WS_POPUP|WS_MINIMIZEBOX,300,100,860,760,0,0,0,None) return hwnd
def create_window(title, class_name, width, height, window_proc): # Register window class wndclass = win32gui.WNDCLASS() wndclass.hInstance = win32api.GetModuleHandle(None) wndclass.lpszClassName = class_name wndclass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW wndclass.hbrBackground = win32con.COLOR_WINDOW wndclass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wndclass.lpfnWndProc = window_proc atom_class = win32gui.RegisterClass(wndclass) assert (atom_class != 0) windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) # 设置任务栏的图标为窗口的图标 xpos = cef_config['xpos'] typos = cef_config['typos'] # Create window window_style = (win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE) window_handle = win32gui.CreateWindow(class_name, title, window_style, xpos, typos, width, height, 0, 0, wndclass.hInstance, None) assert (window_handle != 0) win32gui.SetWindowPos(window_handle, win32con.HWND_TOPMOST, xpos, typos, width, height, win32con.SWP_SHOWWINDOW) # win32con.HWND_TOPMOST窗口置顶 cef_config['typos'] += 40 # Window icon icon = ico_path if not os.path.isfile(icon): icon = None if icon: # Load small and big icon. # WNDCLASSEX (along with hIconSm) is not supported by pywin32, # we need to use WM_SETICON message after window creation. # Ref: # 1. http://stackoverflow.com/questions/2234988 # 2. http://blog.barthe.ph/2009/07/17/wmseticon/ bigx = win32api.GetSystemMetrics(win32con.SM_CXICON) bigy = win32api.GetSystemMetrics(win32con.SM_CYICON) big_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, bigx, bigy, win32con.LR_LOADFROMFILE) smallx = win32api.GetSystemMetrics(win32con.SM_CXSMICON) smally = win32api.GetSystemMetrics(win32con.SM_CYSMICON) small_icon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, smallx, smally, win32con.LR_LOADFROMFILE) win32api.SendMessage(window_handle, win32con.WM_SETICON, win32con.ICON_BIG, big_icon) win32api.SendMessage(window_handle, win32con.WM_SETICON, win32con.ICON_SMALL, small_icon) return window_handle
def __init__(self, className, serverIp, serverPort, **opts): message_map = { win32con.WM_DESTROY: self.OnDestroy, win32con.WM_CLOSE: self.OnClose, win32con.WM_COPYDATA: self.OnCopyData, } wc = win32gui.WNDCLASS() wc.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) wc.hbrBackground = win32con.COLOR_WINDOW # wc.lpszClassName = "WinAdapter" wc.lpszClassName = className wc.lpfnWndProc = message_map self.hinst = wc.hInstance = win32api.GetModuleHandle(None) self.cAtom = win32gui.RegisterClass(wc) self.ConnectorHash = {} self.conList = [] self.busTh = None # busServer thread #print 'adapter: hinst: %s\n' % self.hinst self.hwnd = win32gui.CreateWindowEx( win32con.WS_EX_APPWINDOW, self.cAtom, className, #"WinAdapter App", win32con.WS_OVERLAPPED | win32con.WS_SYSMENU, win32con.CW_USEDEFAULT, 0, win32con.CW_USEDEFAULT, 0, 0, 0, self.hinst, None) global g_HWNDMAIN g_HWNDMAIN = self.hwnd ##win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWDEFAULT) #win32gui.UpdateWindow(self.hwnd) __LOG__.Trace("WINDOW :[%d]" % (g_HWNDMAIN), (logging.CRITICAL)) self.serverIp = serverIp self.serverPort = serverPort self.busTh = busThread(self.serverIp, self.serverPort, **opts) self.busTh.setDaemon(True) self.busTh.start() self.queueTh = queueThread() self.queueTh.setDaemon(True) self.queueTh.start()
def RegisterClass(self): WndProc = {WM_DESTROY: self.OnDestroy} wc = win32gui.WNDCLASS() wc.hInstance = self.hinst wc.hbrBackground = COLOR_BTNFACE + 1 wc.hCursor = win32gui.LoadCursor(0, IDC_ARROW) wc.hIcon = win32gui.LoadIcon(0, IDI_APPLICATION) wc.lpszClassName = 'FontFuzzer{}'.format(self.classNameRandom) wc.lpfnWndProc = WndProc reg = win32gui.RegisterClass(wc) return reg