def LeftClick(self, pos): client_pos = win32gui.ScreenToClient(self._handle, pos) tmp = win32api.MAKELONG(client_pos[0], client_pos[1]) win32api.PostMessage(self._handle, win32con.WM_MOUSEMOVE, 0, tmp) win32api.SendMessage(self._handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) win32api.SendMessage(self._handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp) self.MoveToDefault()
def slide(self, cx1, cy1, pos, hwnd=None): if not hwnd: hwnd = self.handle if pos == 'up': cx2 = cx1 cy2 = cy1 - 200 elif pos == 'down': cx2 = cx1 cy2 = cy1 + 200 elif pos == 'right': cx2 = cx1 + 200 cy2 = cy1 elif pos == 'left': cx2 = cx1 - 200 cy2 = cy1 else: cx2 = pos[0] cy2 = pos[1] long_position1 = win32api.MAKELONG(cx1, cy1) long_position2 = win32api.MAKELONG(cx2, cy2) win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position1) # 模拟鼠标按下 time.sleep(0.5) win32api.SendMessage(hwnd, win32con.WM_MOUSEMOVE, win32con.MK_LBUTTON, long_position2) # 移动到终点 time.sleep(0.5) win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position2) # 模拟鼠标弹起 tick()
def screenshot(self): # Save the screenshot self.saveDC.BitBlt((0, 0), (self.width, self.height), self.mfcDC, (0, 0), win32con.SRCCOPY) bmpinfo = self.saveBitMap.GetInfo() bmpstr = self.saveBitMap.GetBitmapBits(True) # Save to image im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) screen = np.array(im) # Generate random point and sleep_time from the screenshot x, y, sleep_time, battle_count = self.rand_point(screen) if x != None: l_param = win32api.MAKELONG(x, y) win32api.SendMessage(self.hwnd, win32con.WM_MOUSEMOVE, 0, l_param) #win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, # win32con.MK_LBUTTON, l_param) win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, 0, l_param) time.sleep(0.01 + random.random() * 0.02) win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, 0, l_param) sys.stdout.flush() # Sleep for random time time.sleep(sleep_time) # tqdm update cannot take negative input in windows # Every two update cannot be within 10s (to detect end of battle) if battle_count > 0 and time.time() - self.pbar_time > 10: self.pbar.update(battle_count) self.pbar_time = time.time()
def query(self, queryString, queryType = ML_IPC_DB_RUNQUERY): """Queries Winamp's media library and returns a list of items matching the query. The query should include filters like '?artist has \'alice\''. For more information, consult your local Winamp forums or media library. """ queryStringAddr = self.__copyDataToWinamp(queryString) # create query structs and copy to winamp recordList = self.itemRecordList(0, 0, 0) queryStruct = self.mlQueryStruct(cast(queryStringAddr, c_char_p), 0, recordList) queryStructAddr = self.__copyDataToWinamp(queryStruct) # run query win32api.SendMessage(self.__mediaLibraryHWND, self.WM_ML_IPC, queryStructAddr, queryType) receivedQuery = self.__readDataFromWinamp(queryStructAddr, self.mlQueryStruct) items = [] buf = create_string_buffer(sizeof(self.itemRecord) * receivedQuery.itemRecordList.Size) windll.kernel32.ReadProcessMemory(self.__hProcess, receivedQuery.itemRecordList.Items, buf, sizeof(buf), 0) for i in xrange(receivedQuery.itemRecordList.Size): item = self.__readDataFromWinamp(receivedQuery.itemRecordList.Items + (sizeof(self.itemRecord) * i), self.itemRecord) self.__fixRemoteStruct(item) items.append(item) # free results win32api.SendMessage(self.__mediaLibraryHWND, self.WM_ML_IPC, queryStructAddr, self.ML_IPC_DB_FREEQUERYRESULTS) return items
def lock(): import win32gui, win32con, win32api from ctypes import windll import time user = windll.LoadLibrary('user32.dll') user.LockWorkStation() ''' 切换用户 username = '******' password = '' domain = '' token = win32security.LogonUser ( username, domain, password, win32con.LOGON32_LOGON_SERVICE, win32con.LOGON32_PROVIDER_DEFAULT ) win32security.ImpersonateLoggedOnUser(token) ''' time.sleep(3) win32api.keybd_event(13, 0, 0, 0) win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0) handle = win32gui.GetForegroundWindow() print(win32gui.GetWindowText(win32gui.GetForegroundWindow())) win32api.SendMessage(handle, win32con.WM_KEYDOWN, ord('A'), 0) temp = win32api.PostMessage(handle, win32con.WM_CHAR, ord('A'), 0) win32api.SendMessage(handle, win32con.WM_KEYUP, ord('A'), 0)
def Write(self, spell): for i in spell: win32api.SendMessage(self._handle, win32con.WM_CHAR, ord(i), 0) win32api.SendMessage(self._handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32api.SendMessage(self._handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
def winamp_track_info(sep:str=' ')->str: h = win32gui.FindWindow('Winamp v1.x', None) if not h: return 'Winamp not found' samplerate = win32api.SendMessage(h, win32con.WM_USER, 0, 126) bitrate = win32api.SendMessage(h, win32con.WM_USER, 1, 126) channels = win32api.SendMessage(h, win32con.WM_USER, 2, 126) return f'{samplerate}kHz{sep}{bitrate}kbps{sep}{channels}ch'
def press_key(hdl, key): ''' Simulate press key event.[Finally Not used in the script] ''' win32api.SendMessage(hdl, win32con.WM_KEYDOWN, key, 0) time.sleep(0.2) win32api.SendMessage(hdl, win32con.WM_KEYUP, key, 0)
def wrapper(*args, **kwargs): if win32api.LoadKeyboardLayout('0x0409', win32con.KLF_ACTIVATE) == None: return Exception('加载键盘失败') # 语言代码 # https://msdn.microsoft.com/en-us/library/cc233982.aspx LID = { 0x0804: "Chinese (Simplified) (People's Republic of China)", 0x0409: 'English (United States)' } # 获取前景窗口句柄 hwnd = win32gui.GetForegroundWindow() # 获取前景窗口标题 title = win32gui.GetWindowText(hwnd) # 获取键盘布局列表 im_list = win32api.GetKeyboardLayoutList() im_list = list(map(hex, im_list)) print(im_list) oldKey = hex(win32api.GetKeyboardLayout()) # 设置键盘布局为英文 result = win32api.SendMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, 0x4090409) if result == 0: print('设置英文键盘成功!') inner(*args, *kwargs) result = win32api.SendMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, oldKey) if result == 0: print('还原键盘成功!')
def get_icon_from_window(hwnd): """Create an icon file in the temp directory for the window handle and return its path. Actually, it is not unclear how the Windows API works to retrieve the icon info and save it as icon... """ hicon = win32api.SendMessage(hwnd, win32con.WM_GETICON, win32con.ICON_BIG) if hicon == 0: hicon = win32api.SendMessage(hwnd, win32con.WM_GETICON, win32con.ICON_SMALL) if hicon == 0: hicon = win32gui.GetClassLong(hwnd, win32con.GCL_HICON) if hicon == 0: hicon = win32gui.GetClassLong(hwnd, win32con.GCL_HICONSM) if hicon == 0: hicon = get_hicon_from_exe(hwnd) if hicon == 0: return None ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON) # creating a destination memory DC hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0)) hbmp = win32ui.CreateBitmap() hbmp.CreateCompatibleBitmap(hdc, ico_x, ico_x) hdc = hdc.CreateCompatibleDC() hdc.SelectObject(hbmp) hdc.DrawIcon((0, 0), hicon) # file_path = TEMP_DIR + "\Icontemp" + str(hwnd) + ".bmp" file_path = ICONFILE_TEMP_STR.format(str(hwnd)) hbmp.SaveBitmapFile(hdc, file_path) return file_path
def click(): win32api.SendMessage(win, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, win32api.MAKELONG(10, 10)) win32api.SendMessage(win, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, win32api.MAKELONG(10, 10)) win32api.SendMessage(win, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, win32api.MAKELONG(10, 10))
def click_position(hwd, x_position, y_position, sleep): long_position = win32api.MAKELONG(x_position, y_position) win32api.SendMessage(hwd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position) win32api.SendMessage(hwd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position) time.sleep(int(sleep))
def click(self, point, clicks=1, interval=0.0, button="left", pause=0.0): """ Perform a click on the given window in the background. Sending a message to the specified window so the click takes place whether the window is visible or not. """ globals.failsafe() evt_d = self.SUPPORTED_CLICK_EVENTS[button][0] evt_u = self.SUPPORTED_CLICK_EVENTS[button][1] param = win32api.MAKELONG(point[0], point[1] + self.y_padding) # Loop through all clicks that should take place. for x in range(clicks): globals.failsafe() win32api.SendMessage(self.hwnd, evt_d, 1, param) win32api.SendMessage(self.hwnd, evt_u, 0, param) # Interval sleeping? if interval: time.sleep(interval) # Pausing after clicks are finished? if pause: time.sleep(pause)
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 setWindowIcon(window_handle, icon): global BROWSER # 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) cef.WindowUtils.SetIcon(BROWSER, icon="inherit")
def clickClient(handle, pos): coordinate = win32api.MAKELONG(pos[0], pos[1]) win32gui.SendMessage(handle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0) win32api.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, coordinate) win32api.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, coordinate)
def click_combination_keys(hwd, *args): """模拟键盘组合按键""" for arg in args: print(arg) win32api.SendMessage(hwd, win32con.WM_SYSKEYDOWN, arg, 0) for arg in args: win32api.SendMessage(hwd, win32con.WM_SYSKEYUP, arg, 0)
def is_running(): for proc in psutil.process_iter(): proc_list.append(proc.name()) if 'EndlessWorld.exe' in proc_list: print('game was found running already!') print('go to game window within 5 seconds') time.sleep(6) game_handle = win32gui.GetForegroundWindow() global handle handle = game_handle win32gui.ShowWindow(game_handle, 3) print(game_handle) time.sleep(10) win32api.SetCursorPos((509, 343)) win32api.SendMessage(game_handle, win32con.WM_MBUTTONDOWN, win32con.MOUSEEVENTF_LEFTDOWN) win32api.SendMessage(game_handle, win32con.WM_MBUTTONUP, win32con.MOUSEEVENTF_LEFTUP) elif 'EndlessWorld.exe' not in proc_list: print('Game not running, attempting to start now.') subprocess.call( 'C:\Program Files (x86)\Steam\steam.exe -applaunch 840260') time.sleep(3) print(win32gui.GetForegroundWindow()) game_handle = win32gui.GetForegroundWindow() win32gui.ShowWindow(game_handle, 3) time.sleep(10) win32api.SetCursorPos((509, 343)) win32api.SendMessage(game_handle, win32con.WM_MBUTTONDOWN, win32con.MOUSEEVENTF_LEFTDOWN) win32api.SendMessage(game_handle, win32con.WM_MBUTTONUP, win32con.MOUSEEVENTF_LEFTUP)
def mouse_click(x, y, hwnd): import time long_position = win32api.MAKELONG(x, y) win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, long_position) time.sleep(0.02) win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, long_position)
def send_input_hax(hwnd, msg): for c in msg: if c == "\n": win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) else: win32api.SendMessage(hwnd, win32con.WM_CHAR, ord(c), 0) return None
def drug(windowName=FGO窗口名): hwnd = win32gui.FindWindow(None, windowName) win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 1) sleep(0.01) win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, 0x51, 0) sleep(0.01) win32api.SendMessage(hwnd, win32con.WM_KEYUP, 0x51, 0) sleep(0.02 + randint(0, 10) / 1000.0)
def PressHotkey(self, Option, Key): win32api.keybd_event(KeyToHex.get(Option, ""), 0, 0, 0) win32api.SendMessage(self.hwnd, win32con.WM_KEYDOWN, KeyToHex.get(Key, ""), 0) win32api.SendMessage(self.hwnd, win32con.WM_KEYUP, KeyToHex.get(Key, ""), 0) win32api.keybd_event(KeyToHex.get(Option, ""), 0, win32con.KEYEVENTF_KEYUP, 0)
def mouse_left_click(self, x, y): hwnd = self.source y += 36 time.sleep(0.05) win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, win32api.MAKELONG(x, y)) time.sleep(0.05) win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, win32api.MAKELONG(x, y)) time.sleep(0.05)
def send_msg(s): for c in s: win32api.SendMessage(hwnd, win32con.WM_CHAR, ord(c), 0) time.sleep(0.02) print(s) win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32api.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) print(s)
def queue_click(): while global_.workFlag: point = global_.queue_.get() make_long = win32api.MAKELONG(point[0], point[1]) hwnd = global_.param.hwnd win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, make_long) # 模拟鼠标按下 win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, make_long) # 模拟鼠标弹起 time.sleep(0.2)
def keypress(key, handle): global prevkey if prevkey: if key != prevkey: win32api.SendMessage(handle, win32con.WM_KEYUP, ord(prevkey), None) win32api.SendMessage(handle, win32con.WM_KEYDOWN, ord(key), None) prevkey = key
def MouseMove(handle, pos): client_pos = win32gui.ScreenToClient(handle, pos) tmp = win32api.MAKELONG(client_pos[0], client_pos[1]) win32gui.SendMessage(handle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0) win32api.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) win32api.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)
def open_chest(): x1 = int(41) y1 = int(198) win32api.SetCursorPos((x1, y1)) win32api.SendMessage(handle, win32con.WM_MBUTTONDOWN, win32con.MOUSEEVENTF_LEFTDOWN) win32api.SendMessage(handle, win32con.WM_MBUTTONUP, win32con.MOUSEEVENTF_LEFTUP)
def mouse_click(hwnd, position): hwnd = int(hwnd) p = win32api.MAKELONG(position[0], position[1]) win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0) win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, p) sleep(0.1) win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, p) sleep(0.1)
def DragTo(self, From, To): ClientFrom = win32gui.ScreenToClient(self.hwnd, From) FromPosition = win32api.MAKELONG(ClientFrom[0], ClientFrom[1]) ClientTo = win32gui.ScreenToClient(self.hwnd, To) ToPosition = win32api.MAKELONG(ClientTo[0], ClientTo[1]) win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, FromPosition) win32api.SendMessage(self.hwnd, win32con.WM_MOUSEMOVE, 0, ToPosition) win32api.SendMessage(self.hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, ToPosition)