我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用win32api.SetCursorPos()。
def Menu(): try: #?? pdhk=win32gui.GetWindowRect(win32gui.FindWindowEx(win32gui.FindWindow('TdxW_MainFrame_Class','???????V7.35 - [???-?????]'),None,'#32770',None)) win32api.SetCursorPos([pdhk[0]+330,pdhk[1]+10]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) #????? win32api.SetCursorPos([pdhk[0]+380,pdhk[1]+20+480]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #???????->?????
def Free_quotation(): time.sleep(3) try: Tab_handle = win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????V7.35'),None,'SysTabControl32','Tab1') #print(hex(Tab_handle)) #time.sleep(1) p=win32gui.GetWindowRect(Tab_handle) #print(p) #print(p[2]) #print(p[3]) win32api.SetCursorPos([p[0]+170,p[1]+7]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????V7.35'),None,'Button','??'),win32con.BM_CLICK,0,0) time.sleep(3) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #?????
def click_CurrentPlace(self,lb_dx,lb_dy): tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) win32api.SetCursorPos(tmp) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) #----------------------------------------------------------------------------- # Name: select_ssid # purpose: select ssid. # explain: # Author: yuanwen # # Created: 2013/07/5 #-----------------------------------------------------------------------------
def Mouse_LB_Double(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) print 'Mouse_RB str_app,hwnd ',str_app,hwnd if hwnd < 1: hwnd = self.find_main_window(str_app) win32api.SetCursorPos(tmp) time.sleep(1) #win32api.SetDoubleCIckTime() win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.005) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.005) return True
def Mouse_LB_D(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) print 'Mouse_RB str_app,hwnd ',str_app,hwnd if hwnd < 1: hwnd = self.find_main_window(str_app) win32api.SetCursorPos(tmp) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True
def Mouse_LB(self,str_app,lb_dx,lb_dy,Flag='1'): print "*********Mouse_LB function**********" time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) #win32gui.ShowWindow(hwnd, 0) win32api.SetCursorPos(tmp) print 'Mouse_LB tmp =',tmp if Flag == '1': time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True
def MouseLButtonClick(self): if self.element == None: win32api.SetCursorPos((self.posX, self.posY)) FX_MouseLClick(self.posX, self.posY) return True try: self.element.SetFocus() except: print('Focus error') pos = self.element.GetClickablePoint() if pos[1] == False: return False win32api.SetCursorPos((pos[0].x, pos[0].y)) #pos = win32api.GetCursorPos() FX_MouseLClick(pos[0].x, pos[0].y) return True
def CFQS(): try: e = win32gui.GetWindowRect (win32gui.FindWindowEx(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????'),None,'#32770','???????'),None,'SysListView32','CFQS')) win32api.SetCursorPos([e[0]+50,e[1]+25]) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) time.sleep(1) except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #????
def _input_left_mouse(self, x, y): left, top, right, bottom = self.rect width, height = right - left, bottom - top if x < 0 or x > width or y < 0 or y > height: return win32gui.SetForegroundWindow(self.hwnd) pos = win32gui.GetCursorPos() win32api.SetCursorPos((left+x, top+y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.Sleep(100) #ms win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) win32api.Sleep(100) #ms # win32api.SetCursorPos(pos)
def clickWindow(hwnd, offset): left, top, right, bottom = win32gui.GetWindowRect(hwnd) # print('left, top, right, bottom', left, top, right, bottom) win32api.SetCursorPos([left + offset, (bottom - top) // 2 + top]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) time.sleep(0.2) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) time.sleep(0.2)
def _click(x, y): handle = win32gui.GetForegroundWindow() x_old, y_old = win32api.GetCursorPos() # see https://github.com/asweigart/pyautogui/issues/23 try: pyautogui.click(x, y, 1) except FileNotFoundError: pass win32api.SetCursorPos((x_old, y_old)) win32gui.SetForegroundWindow(handle)
def _mouse_click(x, y): win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
def click(self, x, y): win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def Mousepos_set(self,lb_dx='224',lb_dy='366'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) win32api.SetCursorPos(tmp)
def Mouseclick_lb(self,tmp=(220,230)): win32api.SetCursorPos(tmp) print 'Mouseclick_lb tmp:',tmp time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.1) return True
def click_CurrentPlace(self,tmp=(259, 178)): win32api.SetCursorPos(tmp) self.sleep('1') win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) self.sleep('0.05') win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) self.sleep('0.05')
def Mouse_LB_D(self,str_app,lb_dx,lb_dy,Flag='1'): time.sleep(1) tmp=(string.atoi(lb_dx),string.atoi(lb_dy)) hwnd = win32gui.FindWindow(None, str_app) #msg = 'Mouse_LB str_app,hwnd '+str_app+' '+ str(hwnd) #log_print(msg) if hwnd > 0: win32api.SetCursorPos(tmp) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0], tmp[1]) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0], tmp[1]) time.sleep(0.05) return True return False
def FX_SetCursorPos(element): pos = element.GetClickablePoint() if pos[1] == False: return False win32api.SetCursorPos((pos[0].x, pos[0].y)) return True
def FX_MouseMove(x, y): win32api.SetCursorPos((int(x),int(y)))
def MouseLButtonDown(self): pos = self.element.GetClickablePoint() if pos[1] == False: return False win32api.SetCursorPos((pos[0].x, pos[0].y)) #pos = win32api.GetCursorPos() FX_MouseLDown(pos[0].x, pos[0].y) return True
def MouseMove(self): pos = self.element.GetClickablePoint() if pos[1] == False: return False win32api.SetCursorPos((pos[0].x, pos[0].y)) return True ## ## def MouseLButtonUp(self): ## pos = self.element.GetClickablePoint() ## if pos[1] == False: ## return False ## win32api.SetCursorPos((pos[0].x, pos[0].y)) ## #pos = win32api.GetCursorPos() ## FX_MouseLDown(pos[0].x, pos[0].y) ## return True
def Click_Element_None_Mouse(Element,Expand=None,Invoke=None,Select=None,Toggle=None): try: x = (int)(Element.Current.BoundingRectangle.Right - Element.Current.BoundingRectangle.Width / 2); y = (int)(Element.Current.BoundingRectangle.Bottom - Element.Current.BoundingRectangle.Height / 2); win32api.SetCursorPos((x,y)) print "clicking your element" patter_list = Element.GetSupportedPatterns() for each in patter_list: pattern_name = Automation.PatternName(each) if pattern_name == "ExpandCollapse": if Expand==True: #check to see if its expanded, if expanded, then do nothing... if not, expand it status = Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Current.ExpandCollapseState if status == 0: Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Expand() elif status == 1: print "Already Expanded" elif Expand == False: #check to see if its Collapsed, if Collapsed, then do nothing... if not, Collapse it status = Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Current.ExpandCollapseState if status == 1: Element.GetCurrentPattern(ExpandCollapsePattern.Pattern).Collapse() elif status == 0: print "Already Collapsed" elif pattern_name == "Invoke": if Invoke==True: print "invoking the button: %s" %Element.Current.Name time.sleep(2) Element.GetCurrentPattern(InvokePattern.Pattern).Invoke() elif pattern_name == "SelectionItem": Element.GetCurrentPattern(SelectionItemPattern.Pattern).Select() elif pattern_name == "Toggle": Element.GetCurrentPattern(TogglePattern.Pattern).Toggle() else: #x = int (Element.Current.BoundingRectangle.X) #y = int (Element.Current.BoundingRectangle.Y) x = (int)(Element.Current.BoundingRectangle.Right - Element.Current.BoundingRectangle.Width / 2); y = (int)(Element.Current.BoundingRectangle.Bottom - Element.Current.BoundingRectangle.Height / 2); win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) time.sleep(0.1) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) except Exception, e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print ((str(exc_type).replace("type ", "Error Type: ")) + ";" + "Error Message: " + str(exc_obj) +";" + "File Name: " + fname + ";" + "Line: "+ str(exc_tb.tb_lineno))