def leftUp(pos, hd): handle = hd 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) win32gui.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)
def screenshot(self, ): hwnd = win32gui.GetForegroundWindow() if hwnd: win32gui.SetForegroundWindow(hwnd) x, y, x1, y1 = win32gui.GetClientRect(hwnd) win32gui.ScreenToClient(hwnd,(x,y)) x, y = win32gui.ClientToScreen(hwnd, (x, y)) x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y)) print("x = ",x) print("y = ",y) print("x1 = ", x1) print("y1 = ",y1) im = pyautogui.screenshot(region=(x, y, x1, y1)) im = im.convert('RGB') im = numpy.array(im) im = im[:, :, ::-1].copy() self.i+=1 # im.save(str(self.i) + ".jpg","JPEG") return im else: print('Window not found!')
def getPixel(x, y): hwnd = win32gui.WindowFromPoint((x, y)) hdc = win32gui.GetDC(hwnd) x1, y1 = win32gui.ScreenToClient(hwnd, (x, y)) color = win32gui.GetPixel(hdc, x1, y1) win32gui.ReleaseDC(hwnd, hdc) return color % 256, color / 256 % 256, color / 256 / 256
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 get_sample(self): img_data = self.__window_instance.getScreen() if img_data is None: return None, None img_data = cv2.resize(img_data, (self.__frame_width, self.__frame_height)) pts = [] evts = parse_mouse_evts() if evts: LOG.info("mouse evts:%s" % evts) for evt in evts: msgid, pt, ts = evt x, y = pt hwnd = win32gui.WindowFromPoint((x, y)) if hwnd != self.__hwnd: continue c_x, c_y = win32gui.ScreenToClient(self.__hwnd, (x, y)) if c_x <= 0 or c_x >= self.__window_width: continue if c_y <= 0 or c_y >= self.__window_height: continue p_x = int(self.__frame_width * c_x / self.__window_width) p_y = int(self.__frame_height * c_y / self.__window_height) pts.append([HookEventType.MOUSE, msgid, [p_x, p_y]]) evts = parse_keyboard_evts() if evts: LOG.info("key evts:%s" % evts) for evt in evts: msgid, vk, ts = evt pts.append([HookEventType.KEYBOARD, msgid, vk]) return img_data, pts
def click_it(pos): #第三种 handle= win32gui.WindowFromPoint(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) win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN,win32con.MK_LBUTTON,tmp) win32gui.SendMessage(handle, win32con.WM_LBUTTONUP,win32con.MK_LBUTTON,tmp)
def move_click(self, window, All_window, click_time, time_l, time_r, hwnd): dx = random.randint(0, window[2] - window[0]) dy = random.randint(0, window[3] - window[1]) x = All_window[0] + window[0] + dx y = All_window[1] + window[1] + dy for i in range(click_time): dx = random.randint(-5, 5) dy = random.randint(-5, 5) nx = x + dx ny = y + dy nx, ny = self.check_bound(nx, ny, All_window, window) #win32api.SetCursorPos((nx, ny)) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, nx, ny, 0, 0) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, nx, ny, 0, 0) client_pos = win32gui.ScreenToClient(hwnd, (nx, ny)) tmp = win32api.MAKELONG(client_pos[0], client_pos[1]) win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) time.sleep(random.uniform(0.1, 0.15)) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp) time.sleep(random.uniform(time_l, time_r)) return 0
def send_nc_click(hwnd, x, y, flag=MouseFlag.LeftButton, click_type=MouseClickType.SingleClick): """在目标窗口的Non-Client区域通过发消息的方式产生鼠标点击的动作 :param hwnd: 目标窗口句柄 :type hwnd: 整数 :param x: 屏幕x坐标 :type x: 整数 :param y: 屏幕y坐标 :type y: 整数 :param flag: 鼠标键类型 :type flag: 枚举类型, MouseFlag.LeftButton|MouseFlag.MiddleButton|MouseFlag.RightButton :param click_type: 鼠标键点击方式 :type click_type: 枚举类型, MouseClickType.SingleClick | MouseClickType.DoubleClick """ win32api.SetCursorPos((x, y)) c_x, c_y = win32gui.ScreenToClient(hwnd, (x, y)) uHitTestResult = win32gui.SendMessage(hwnd, win32con.WM_NCHITTEST, 0, (c_y << 16) | c_x) if click_type == MouseClickType.SingleClick: win32gui.PostMessage(hwnd, _mouse_ncmsg_param[flag][0], uHitTestResult, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][1], 0, (c_y << 16) | c_x) else: win32gui.PostMessage(hwnd, _mouse_ncmsg_param[flag][0], uHitTestResult, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_ncmsg_param[flag][1], 0, (c_y << 16) | c_x) time.sleep(0.1) win32gui.PostMessage(hwnd, _mouse_ncmsg_param[flag][3], uHitTestResult, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_ncmsg_param[flag][1], 0, (c_y << 16) | c_x)
def click_it(pos, hd): handle = hd # moveCurPos(pos[0], pos[1]) 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) win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) win32gui.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)
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 client_pos(): ''' help to find click pos ''' handle = win32gui.FindWindow(None, '阴阳师-网易游戏') pos = win32gui.GetCursorPos() client_pos = win32gui.ScreenToClient(handle, pos) return client_pos
def send_click(hwnd, x, y, flag=MouseFlag.LeftButton, click_type=MouseClickType.SingleClick): """在目标窗口通过SendMessage方式产生鼠标点击的动作 :param hwnd: 目标窗口句柄 :type hwnd: 整数 :param x: 屏幕x坐标 :type x: 整数 :param y: 屏幕y坐标 :type y: 整数 :param flag: 鼠标键类型 :type flag: 枚举类型, MouseFlag.LeftButton|MouseFlag.MiddleButton|MouseFlag.RightButton :param click_type: 鼠标键点击方式 :type click_type: 枚举类型, MouseClickType.SingleClick | MouseClickType.DoubleClick """ keystate = 0 if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 != 0: keystate = keystate | win32con.MK_CONTROL if win32api.GetAsyncKeyState(win32con.VK_SHIFT) & 0x8000 != 0: keystate = keystate | win32con.MK_SHIFT win32api.SetCursorPos((x, y)) time.sleep(0.2) c_x, c_y = win32gui.ScreenToClient(hwnd, (x, y)) if click_type == MouseClickType.SingleClick: try: win32gui.SendMessageTimeout(hwnd, _mouse_msg_param[flag][0], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x, 0, 1000) except win32gui.error as e: if e.winerror == 1400: # 无效窗口句柄,有些窗口post第一个消息后就会消失 pass elif e.winerror == 1460: # timeout win32gui.SendMessageTimeout(hwnd, win32con.WM_NULL, 0, 0, 0, 30000) # 发送NULL确认上个消息已处理 else: raise e try: win32gui.SendMessageTimeout(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | c_x, 0, 1000) except win32gui.error as e: if e.winerror == 1400: # 无效窗口句柄,有些窗口post第一个消息后就会消失 pass elif e.winerror == 1460: # timeout win32gui.SendMessageTimeout(hwnd, win32con.WM_NULL, 0, 0, 0, 30000) # 发送NULL确认上个消息已处理 else: raise e else: win32gui.SendMessage(hwnd, _mouse_msg_param[flag][0], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x) win32gui.SendMessage(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | c_x) win32gui.SendMessage(hwnd, _mouse_msg_param[flag][3], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x) win32gui.SendMessage(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | c_x)
def getno(): #查询句柄 #第一个参数为父窗口类名,不知道类名时取None,在不同的系统环境下,r12可能变化 #第二个参数为窗口标题名,不知道标题时取None,FindWindow函数不能模糊查找 hld = win32gui.FindWindow( "#32770", "ArturDents CrackMe #2") HwndEx=None rect=None if hld>0: try: while rect!=(51, 39): #获取hld子窗口内的控件句柄,类名为edit,该函数只能查收下一级子窗口内控件的句柄,查询孙子窗口的句柄需要进行嵌套查询 #参数1:父窗口句柄 #参数2:子窗口句柄,从该子窗口开始向后查找,如果取None,则全部遍历一次 #参数3:查找窗口的类名,不知道类名时取None #参数4:查找窗口的的标题,不知道类名时取None,FindWindowEx函数不能模糊查找 HwndEx=win32gui.FindWindowEx(hld,HwndEx,"Edit",None) print("控件句柄:",HwndEx) #获取该句柄对应控件的左上角和右下角的坐标,获取的坐标为屏幕坐标 rect = win32gui.GetWindowRect(HwndEx) #将屏幕坐标转换为相对于父窗口的坐标 rect=win32gui.ScreenToClient(hld,(rect[0],rect[1])) print("控件左上角在父窗口内的坐标:",rect) #获取该句柄对应控件的长度和高度 ck = win32gui.GetClientRect(HwndEx) print("控件的长度和高度:",ck) print("\n") # 向控件内写入文本,第三个参数不使用,取0 win32gui.SendMessage(HwndEx, win32con.WM_SETTEXT, 0, "hello world") # 向控件内写入回车 win32gui.PostMessage(HwndEx, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32gui.PostMessage(HwndEx, win32con.WM_KEYUP, win32con.VK_RETURN, 0) # 获取控件内文本的长度,要加上截尾的字节 buf_size = win32gui.SendMessage(HwndEx, win32con.WM_GETTEXTLENGTH, 0, 0) + 1 print(buf_size) # 生成buffer对象 str_buffer = win32gui.PyMakeBuffer(buf_size*2) # 获取buffer,得到类似于16进制的东西,将结果传输给str_buffer,参数3控制获取长度 win32api.SendMessage(HwndEx, win32con.WM_GETTEXT, buf_size, str_buffer) # 将buffer转为字符串 string = bytes(str_buffer).decode('utf-16') print("目标控件内的文本为:",string) except BaseException as e: print('错误信息:',e) else: print("错误!!程序未启动")
def confirm_selection(self, pos): try: win32api.SetCursorPos(( int(self.cover_area.left + pos[0]), int(self.cover_area.top + pos[1]), )) except: pass # Not allowed, probably an administrator window has focus or something position = win32gui.ScreenToClient(self.window.handle, win32api.GetCursorPos()) if self.clickmode == 'left': pylewm.commands.delay_pyle_command( 0.1, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.15, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, position[0], position[1], 0, 0)) elif self.clickmode == 'double': pylewm.commands.delay_pyle_command( 0.1, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.15, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.2, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.25, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, position[0], position[1], 0, 0)) elif self.clickmode == 'right': pylewm.commands.delay_pyle_command( 0.1, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.15, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, position[0], position[1], 0, 0)) elif self.clickmode == 'middle': pylewm.commands.delay_pyle_command( 0.1, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEDOWN, position[0], position[1], 0, 0)) pylewm.commands.delay_pyle_command( 0.15, lambda: win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEUP, position[0], position[1], 0, 0)) self.close()
def OnMouseClick(self, event): if event.MessageName.startswith("mouse"): #logging.info("mouse event: %s" % event.MessageName) if event.MessageName.endswith("down"): if event.MessageName == 'mouse left down': self._mouse_botton_down["L"] = True elif event.MessageName == 'mouse middle down': self._mouse_botton_down["M"] = True elif event.MessageName == 'mouse right down': self._mouse_botton_down["R"] = True if self._modifier_down["ALT"]: guiti = GUITHREADINFO(cbSize=ctypes.sizeof(GUITHREADINFO)) user.GetGUIThreadInfo(0, ctypes.byref(guiti)) wi = WINDOWINFO() wi.cbSize = ctypes.sizeof(WINDOWINFO) win = win32gui.WindowFromPoint(event.Position) #win = guiti.hwndFocus win = get_top_window(win) user.GetWindowInfo(win, byref(wi)) #wp = win32gui.GetWindowPlacement(win) self._original_pos = event.Position self._client_pos = win32gui.ScreenToClient(win, self._original_pos) w_rect = win32gui.GetWindowRect(win) self._window_size = (w_rect[2] - w_rect[0], w_rect[3] - w_rect[1]) self._window_pos = w_rect[0:2] self._xrp = self._client_pos[0] / (self._window_size[0] / 2) self._yrp = self._client_pos[1] / (self._window_size[1] / 2) self._window_handle = win self.hm.MouseMove = self.OnMouseMove return False elif event.MessageName.endswith("up"): if event.MessageName == 'mouse left up': self._mouse_botton_down["L"] = False elif event.MessageName == 'mouse middle up': self._mouse_botton_down["M"] = False elif event.MessageName == 'mouse right up': self._mouse_botton_down["R"] = False #print win32gui.GetWindowRect(win32gui.GetDesktopWindow()) self._window_handle = 0 self.hm.MouseMove = None if self._modifier_down["ALT"]: self._modifier_down["ALT"] = False return True
def send_move(hwnd, toX, toY): win32api.SetCursorPos((toX, toY)) c_x, c_y = win32gui.ScreenToClient(hwnd, (toX, toY)) keystate = 0 if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 != 0: keystate = keystate | win32con.MK_CONTROL if win32api.GetAsyncKeyState(win32con.VK_SHIFT) & 0x8000 != 0: keystate = keystate | win32con.MK_SHIFT win32gui.SendMessage(hwnd, win32con.WM_MOUSEMOVE, keystate, (c_y << 16) | c_x)
def RightClick(): pos = get_curpos() handle = get_win_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_RBUTTONDOWN, win32con.MK_RBUTTON, tmp) win32api.SendMessage(handle, win32con.WM_RBUTTONUP, win32con.MK_RBUTTON, tmp)
def clickWindow(hwnd, x, y, screen2client=True): if screen2client: x, y = win32gui.ScreenToClient(hwnd, (x, y)) lparam = y << 16 | x win32gui.SendMessage(hwnd, win32con.WM_MOUSEMOVE, 0, lparam) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lparam) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lparam)
def RightClick(self, Position): ClientPosition = win32gui.ScreenToClient(self.hwnd, Position) PositionToClick = win32api.MAKELONG(ClientPosition[0], ClientPosition[1] + 23) win32api.PostMessage(self.hwnd, win32con.WM_MOUSEMOVE, 0, PositionToClick) win32api.SendMessage(self.hwnd, win32con.WM_RBUTTONDOWN, win32con.MK_RBUTTON, PositionToClick) win32api.SendMessage(self.hwnd, win32con.WM_RBUTTONUP, win32con.MK_RBUTTON, PositionToClick)
def postClick(hwnd, x, y, flag=MouseFlag.LeftButton, clicktype=MouseClickType.SingleClick): """在目标窗口通过PostMessage的方式产生鼠标点击的动作 :param hwnd: 目标窗口句柄 :type hwnd: 整数 :param x: 屏幕x坐标 :type x: 整数 :param y: 屏幕y坐标 :type y: 整数 :param flag: 鼠标键类型 :type flag: 枚举类型, MouseFlag.LeftButton|MouseFlag.MiddleButton|MouseFlag.RightButton :param clicktype: 鼠标键点击方式 :type clicktype: 枚举类型, MouseClickType.SingleClick | MouseClickType.DoubleClick """ sleep_time = (win32gui.GetDoubleClickTime() / 1000.0 + 0.05) - (time.time() - Mouse._last_click_time) if sleep_time > 0: time.sleep(sleep_time) keystate = 0 if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 != 0: keystate = keystate | win32con.MK_CONTROL if win32api.GetAsyncKeyState(win32con.VK_SHIFT) & 0x8000 != 0: keystate = keystate | win32con.MK_SHIFT win32api.SetCursorPos((x, y)) c_x, c_y = win32gui.ScreenToClient(hwnd, (x, y)) if clicktype == MouseClickType.SingleClick: try: win32gui.PostMessage(hwnd, win32con.WM_NULL, 0, 0) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][0], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | (c_x)) except win32gui.error as e: if e.winerror == winerror.ERROR_INVALID_WINDOW_HANDLE: #无效窗口句柄,有些窗口post第一个消息后就会消失 pass else: raise e else: win32gui.PostMessage(hwnd, _mouse_msg_param[flag][0], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | (c_x)) time.sleep(0.1) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][3], _mouse_msg_param[flag][2] | keystate, (c_y << 16) | c_x) win32gui.PostMessage(hwnd, _mouse_msg_param[flag][1], 0 | keystate, (c_y << 16) | (c_x)) Mouse._last_click_time = time.time()
def click_it(pos, hd): handle = hd moveCurPos(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) win32gui.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) # print("down,休眠1s") # time.sleep(0.1) # print("up") win32gui.SendMessage(handle, win32con.WM_, win32con.MK_LBUTTON, tmp)
def _move_map(self, map_win, dir): if dir == 'right': from_pos, to_pos = (self.cap_pos[2], self.cap_pos[1]), (self.cap_pos[0], self.cap_pos[1]) elif dir == 'left': from_pos, to_pos = (self.cap_pos[0], self.cap_pos[1]), (self.cap_pos[2], self.cap_pos[1]) elif dir == 'up': from_pos, to_pos = (self.cap_pos[0], self.cap_pos[1]), (self.cap_pos[0], self.cap_pos[3]) else: from_pos, to_pos = (self.cap_pos[0], self.cap_pos[3]), (self.cap_pos[0], self.cap_pos[1]) from_client_pos = win32gui.ScreenToClient(map_win, from_pos) to_client_pos = win32gui.ScreenToClient(map_win, to_pos) win32api.SetCursorPos(from_pos) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(0.5) win32api.SetCursorPos(to_pos) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(0.5)
def mouse_offset_fn(self, event: MoveEvent): event.x, event.y = win32gui.GetCursorPos() # use logical position xy2 = win32gui.ScreenToClient(self.screen.hwnd, (event.x, event.y)) xy2 = round(xy2[0] * 1.25), round(xy2[1] * 1.25) # adjust : #xy2 = xy2[0] - 140, xy2[1] - 76 event.x, event.y = xy2 if 0 <= event.y <= self.H and \ 0 <= event.x <= self.W : return event return None
def ReportValues(self, promptBeforeCopy): # update values doCopy = False useClient = False if promptBeforeCopy: message = 'Copy the parameters into the clipboard as the commandline switches needed to run the grid?' if CustomGridUtilities.messageBoxYesNo( message, "Custom Grid Results" ) == CustomGridUtilities.MESSAGEBOX_RETURN_YES: doCopy = True message = "Use screen coordinates (Yes) or client coordinates (No)" if CustomGridUtilities.messageBoxYesNo( message, "Custom Grid Results" ) == CustomGridUtilities.MESSAGEBOX_RETURN_NO: useClient = True else: doCopy = True self.CalculateGrid() if useClient: # Use client coordinates for location x and y newGridX, newGridY = win32gui.ScreenToClient( self.previousActiveWindowHwnd, (self.gridX, self.gridY)) self.gridX = newGridX self.gridY = newGridY # CustomGridUtilities.messageBoxOK(win32gui.GetWindowText(self.previousActiveWindowHwnd), "Test") data = "Grid location x: " + str(self.gridX) + \ "\nGrid location y: " + str(self.gridY) + \ "\nGrid width: " + str(self.gridWidth) + \ "\nGrid height: " + str(self.gridHeight) + \ "\nColumn width: " + str(self.columnWidth) + \ "\nRow height: " + str(self.rowHeight) + \ "\nNumber columns: " + str(self.numColumns) + \ "\nNumber rows: " + str(self.numRows) # Print to command line print data if doCopy: CustomGridUtilities.setClipboard('--width ' + str(self.gridWidth) + \ ' --height ' + str(self.gridHeight) + \ ' --locationx ' + str(self.gridX) + \ ' --locationy ' + str(self.gridY) + \ ' --rowheight ' + str(self.rowHeight) + \ ' --columnwidth ' + str(self.columnWidth) + \ ' --numrows ' + str(self.numRows) + \ ' --numcolumns ' + str(self.numColumns)) data += '\n\nCommandline switches for the above parameters have been copied into the clipboard.' CustomGridUtilities.messageBoxOK(data, "Custom Grid Results")
def _DoSize(self, cx, cy, repaint = 1): # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_SEARCHTEXT) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l,t) ) r, b = win32gui.ScreenToClient(self.hwnd, (r,b) ) win32gui.MoveWindow(ctrl, l, t, cx-l-5, b-t, repaint) # The button. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_BUTTON_DISPLAY) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l,t) ) r, b = win32gui.ScreenToClient(self.hwnd, (r,b) ) list_y = b + 10 w = r - l win32gui.MoveWindow(ctrl, cx - 5 - w, t, w, b-t, repaint) # The list control win32gui.MoveWindow(self.hwndList, 0, list_y, cx, cy-list_y, repaint) # The last column of the list control. new_width = cx - win32gui.SendMessage(self.hwndList, commctrl.LVM_GETCOLUMNWIDTH, 0) win32gui.SendMessage(self.hwndList, commctrl.LVM_SETCOLUMNWIDTH, 1, new_width)
def drop_inventory(spare_columns): handle = win32gui.FindWindow('D3 Main Window Class', 'Diablo III') if handle: item = transform_coordinates(handle, 1875, 585, rel='right') step = transform_coordinates(handle, 50, 50) x, y = win32gui.ScreenToClient(handle, win32api.GetCursorPos()) send_key(handle, 'c') for i in range(6): for j in range(10 - spare_columns): send_mouse(handle, 'LM', item[0] - j * step[0], item[1] + i * step[1]) send_mouse(handle, 'LM', x, y) send_key(handle, 'c')
def choose_cargo(left, top, loc, hwnd): # 选中货物 # 货物的坐标需要转换成在整个屏幕中的坐标,并绑定窗口句柄 start_pos = win32gui.ScreenToClient(hwnd, (loc[0] + left, loc[1] + top)) # test_pos = win32gui.ScreenToClient(hwnd, (left+210, top+424)) tmp = win32api.MAKELONG(start_pos[0], start_pos[1]) # 将坐标转换成SenfMessage()需要的一维数据坐标 win32gui.SendMessage(hwnd, win32con.WM_ACTIVATE, win32con.WM_ACTIVATE, 0) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0000, tmp) # 先将鼠标弹起,减少误触概率 #print('up', loc) time.sleep(0.2) win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp) # 在tmp坐标处按下鼠标左键
def screenToClient(hwnd: int, sx, sy): """ 客户区坐标转屏幕坐标 :param hwnd: 窗口句柄 :param sx: 屏幕坐标x :param sy: 屏幕坐标y :return: 元组(客户区坐标x,客户区坐标y) """ # 窗口句柄校验 if hwnd != 0 and not win32gui.IsWindow(hwnd): raise Exception(str(hwnd) + "无效的窗口句柄。") cx, cy = win32gui.ScreenToClient(hwnd, (sx, sy)) return (cx, cy)
def _DoSize(self, cx, cy, repaint=1): # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_STATIC_EXCEPTION) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) win32gui.MoveWindow(ctrl, 5, t, cx - l - 5, b - t, repaint) # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_EDIT_EXCEPTION) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) win32gui.MoveWindow(ctrl, 5, t, cx - l - 5, b - t, repaint) # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_STATIC_LOG) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) win32gui.MoveWindow(ctrl, 5, t, cx - l - 5, b - t, repaint) # right-justify the textbox. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_EDIT_LOG) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) win32gui.MoveWindow(ctrl, 5, t, cx - l - 5, cy - 190, repaint) # The button. ctrl = win32gui.GetDlgItem(self.hwnd, IDC_BUTTON_OK) l, t, r, b = win32gui.GetWindowRect(ctrl) l, t = win32gui.ScreenToClient(self.hwnd, (l, t)) r, b = win32gui.ScreenToClient(self.hwnd, (r, b)) list_y = b + 10 w = r - l win32gui.MoveWindow(ctrl, 5, cy - 40, cx - l - 5, 25, repaint)
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