def HookMessages(self):
self.HookMessage(self.OnLButtonUp, win32con.WM_LBUTTONUP)
self.HookMessage(self.OnLButtonDown, win32con.WM_LBUTTONDOWN)
self.HookMessage(self.OnLButtonDblClk, win32con.WM_LBUTTONDBLCLK)
self.HookMessage(self.OnNcLButtonDown, win32con.WM_NCLBUTTONDOWN)
self.HookMessage(self.OnNcLButtonDblClk, win32con.WM_NCLBUTTONDBLCLK)
self.HookMessage(self.OnMouseMove, win32con.WM_MOUSEMOVE)
self.HookMessage(self.OnNcPaint, win32con.WM_NCPAINT)
self.HookMessage(self.OnCaptureChanged, win32con.WM_CAPTURECHANGED)
self.HookMessage(self.OnWindowPosChanged, win32con.WM_WINDOWPOSCHANGED)
# self.HookMessage(self.OnSize, win32con.WM_SIZE)
python类WM_LBUTTONDOWN的实例源码
def HookMessages(self):
self.HookMessage(self.OnLButtonUp, win32con.WM_LBUTTONUP)
self.HookMessage(self.OnLButtonDown, win32con.WM_LBUTTONDOWN)
self.HookMessage(self.OnLButtonDblClk, win32con.WM_LBUTTONDBLCLK)
self.HookMessage(self.OnNcLButtonDown, win32con.WM_NCLBUTTONDOWN)
self.HookMessage(self.OnNcLButtonDblClk, win32con.WM_NCLBUTTONDBLCLK)
self.HookMessage(self.OnMouseMove, win32con.WM_MOUSEMOVE)
self.HookMessage(self.OnNcPaint, win32con.WM_NCPAINT)
self.HookMessage(self.OnCaptureChanged, win32con.WM_CAPTURECHANGED)
self.HookMessage(self.OnWindowPosChanged, win32con.WM_WINDOWPOSCHANGED)
# self.HookMessage(self.OnSize, win32con.WM_SIZE)
def click(hwnd):
"""
????????
:param hwnd: ???????????
:return:
"""
win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDOWN, None, None)
time.sleep(0.2)
win32gui.PostMessage(hwnd, win32con.WM_LBUTTONUP, None, None)
time.sleep(0.2)
ctrl_waveQoE.py 文件源码
项目:Automation-Framework-for-devices
作者: tok-gogogo
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def Mouse_LB_click(self,hwnd):
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
time.sleep(1)
#-----------------------------------------------------------------------------
# Name: click_CurrentPlace
# purpose: click on the current place.
# explain:
# Author: yuanwen
#
# Created: 2013/07/5
#-----------------------------------------------------------------------------
def Find_Gui_title(self,str_app='Edit Action',find_control_class='0',control_class='WindowsForms10.Window.8.app.0.2e0c681',text='10',control_name=''):
time.sleep(1)
print "*********Find_Gui_title function**********"
#self.Mousepos_print()
print 'control_name:',str_app,find_control_class
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
print 'hwnd :',hwnd,str_app
win32gui.SetForegroundWindow(hwnd)
comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None)
print 'comboHwnd',comboHwnd
while comboHwnd:
cla = win32gui.GetClassName(comboHwnd)
print "control_class_name:",cla
bufLen=1024
buf =win32gui.PyMakeBuffer(bufLen)
n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf)
str = buf[:n]
print 'buf:',str
if str.find(find_control_class)>-1:
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0)
time.sleep(0.05)
#win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
time.sleep(0.05)
time.sleep(1)
break
comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT)
return True
def Find_Gui_button_hwnd(self,str_app='´ò¿ª',control_class='Button',control_name='´ò¿ª(&O)'):
time.sleep(1)
result = False
print "*********Find_Gui_button function**********"
#self.Mousepos_print()
#print 'control_name:',str_app,',',control_name
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None)
#print 'comboHwnd',comboHwnd
while comboHwnd:
#print "control_class:",comboHwnd,control_name
cla = win32gui.GetClassName(comboHwnd)
#print 'cla:' ,cla
bufLen=1024
buf =win32gui.PyMakeBuffer(bufLen)
if control_class in cla:
n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf)
str = buf[:n]
#print 'buttonname ,len,n,find :',str,len(control_name ),n,str.find(control_name)
if str.find(control_name)>-1:
result = True
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0)
time.sleep(0.05)
#win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
time.sleep(0.05)
time.sleep(1)
print 'find control_name',result
break
comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT)
if result ==False:
comboHwnd = -1
return comboHwnd
def Find_Gui_button(self,str_app='´ò¿ª',control_class='Button',control_name='´ò¿ª(&O)'):
time.sleep(1)
result = False
print "*********Find_Gui_button function**********"
#self.Mousepos_print()
#print 'control_name:',str_app,',',control_name
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
#print 'hwnd :',hwnd,str_app
win32gui.SetForegroundWindow(hwnd)
comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None)
#print 'comboHwnd',comboHwnd
while comboHwnd:
print "control_class:",comboHwnd,control_name
cla = win32gui.GetClassName(comboHwnd)
print 'cla:' ,cla
bufLen=1024
buf =win32gui.PyMakeBuffer(bufLen)
if control_class in cla:
n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf)
str = buf[:n]
print 'buttonname ,len,n,find :',str,len(control_name ),n,str.find(control_name)
if (len(control_name ) ==0 and n==0) or ((len(control_name )>0 and str.find(control_name)>-1 )):
#win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0)
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0)
time.sleep(0.05)
#win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
time.sleep(0.05)
#win32gui.SendMessage(win32con.WM_CLOSE,0,0)
#print '111'
result = True
break
comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT)
time.sleep(1)
return result
ctrl_omnipeek.py 文件源码
项目:Automation-Framework-for-devices
作者: tok-gogogo
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def Mouse_LB_click(self,hwnd):
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
self.sleep('1')
ctrl_pingtest.py 文件源码
项目:Automation-Framework-for-devices
作者: tok-gogogo
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def Mouse_LB_click(self,hwnd):
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
time.sleep(1)
def icon_wndproc(hwnd, msg, wp, lp):
""" Window proc for the tray icons """
if lp==win32con.WM_LBUTTONDOWN:
## popup menu won't disappear if you don't do this
win32gui.SetForegroundWindow(hwnd)
curr_desktop=win32service.OpenInputDesktop(0,True,win32con.MAXIMUM_ALLOWED)
curr_desktop_name=win32service.GetUserObjectInformation(curr_desktop,win32con.UOI_NAME)
winsta=win32service.GetProcessWindowStation()
desktops=winsta.EnumDesktops()
m=win32gui.CreatePopupMenu()
desktop_cnt=len(desktops)
## *don't* create an item 0
for d in range(1, desktop_cnt+1):
mf_flags=win32con.MF_STRING
## if you switch to winlogon yourself, there's nothing there and you're stuck
if desktops[d-1].lower() in ('winlogon','disconnect'):
mf_flags=mf_flags|win32con.MF_GRAYED|win32con.MF_DISABLED
if desktops[d-1]==curr_desktop_name:
mf_flags=mf_flags|win32con.MF_CHECKED
win32gui.AppendMenu(m, mf_flags, d, desktops[d-1])
win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt+1, 'Create new ...')
win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt+2, 'Exit')
x,y=win32gui.GetCursorPos()
d=win32gui.TrackPopupMenu(m,win32con.TPM_LEFTBUTTON|win32con.TPM_RETURNCMD|win32con.TPM_NONOTIFY,
x,y, 0, hwnd, None)
win32gui.PumpWaitingMessages()
win32gui.DestroyMenu(m)
if d==desktop_cnt+1: ## Create new
get_new_desktop_name(hwnd)
elif d==desktop_cnt+2: ## Exit
win32gui.PostQuitMessage(0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, window_info[hwnd])
del window_info[hwnd]
origin_desktop.SwitchDesktop()
elif d>0:
hdesk=win32service.OpenDesktop(desktops[d-1],0,0,win32con.MAXIMUM_ALLOWED)
hdesk.SwitchDesktop()
return 0
else:
return win32gui.DefWindowProc(hwnd, msg, wp, lp)
def icon_wndproc(hwnd, msg, wp, lp):
""" Window proc for the tray icons """
if lp==win32con.WM_LBUTTONDOWN:
## popup menu won't disappear if you don't do this
win32gui.SetForegroundWindow(hwnd)
curr_desktop=win32service.OpenInputDesktop(0,True,win32con.MAXIMUM_ALLOWED)
curr_desktop_name=win32service.GetUserObjectInformation(curr_desktop,win32con.UOI_NAME)
winsta=win32service.GetProcessWindowStation()
desktops=winsta.EnumDesktops()
m=win32gui.CreatePopupMenu()
desktop_cnt=len(desktops)
## *don't* create an item 0
for d in range(1, desktop_cnt+1):
mf_flags=win32con.MF_STRING
## if you switch to winlogon yourself, there's nothing there and you're stuck
if desktops[d-1].lower() in ('winlogon','disconnect'):
mf_flags=mf_flags|win32con.MF_GRAYED|win32con.MF_DISABLED
if desktops[d-1]==curr_desktop_name:
mf_flags=mf_flags|win32con.MF_CHECKED
win32gui.AppendMenu(m, mf_flags, d, desktops[d-1])
win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt+1, 'Create new ...')
win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt+2, 'Exit')
x,y=win32gui.GetCursorPos()
d=win32gui.TrackPopupMenu(m,win32con.TPM_LEFTBUTTON|win32con.TPM_RETURNCMD|win32con.TPM_NONOTIFY,
x,y, 0, hwnd, None)
win32gui.PumpWaitingMessages()
win32gui.DestroyMenu(m)
if d==desktop_cnt+1: ## Create new
get_new_desktop_name(hwnd)
elif d==desktop_cnt+2: ## Exit
win32gui.PostQuitMessage(0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, window_info[hwnd])
del window_info[hwnd]
origin_desktop.SwitchDesktop()
elif d>0:
hdesk=win32service.OpenDesktop(desktops[d-1],0,0,win32con.MAXIMUM_ALLOWED)
hdesk.SwitchDesktop()
return 0
else:
return win32gui.DefWindowProc(hwnd, msg, wp, lp)
def Find_Gui_Tree(self,str_app='New Scenario',control_class='Button',filename='test_baidu',control_name='AfxOleControl'):
print "*********Find_Gui_Tree function**********"
time.sleep(1)
#self.Mousepos_print()
print 'str_app',str_app
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
print 'hwnd',hwnd
win32gui.SetForegroundWindow(hwnd)
print 'hwnd :',hwnd,str_app
win32gui.SetForegroundWindow(hwnd)
#self.Mousepos_print('5')
comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None)
print '111111111111comboHwnd',comboHwnd
while comboHwnd:
print "control_class:",comboHwnd
cla = win32gui.GetClassName(comboHwnd)
print "control_class_NAME:",cla
bufLen=1024
buf =win32gui.PyMakeBuffer(bufLen)
n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf)
str = buf[:n]
print str
'''
if control_class in cla:
n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf)
str = buf[:n]
print str
if (len(control_name ) ==0 and n==0) or ((len(control_name )>0 and str.find(control_name)>-1 )):
win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0)
time.sleep(0.05)
win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0)
time.sleep(0.05)
win32gui.SendMessage(win32con.WM_CLOSE,0,0)
break
'''
comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT)
time.sleep(1)
return True
def click(self, coords, button="left",hold=False):
"""
Args:
coords (touple): coords takes two arguments, either both float
or int. If float is supplied, it will try to treat them as
percentages. X, Y
button (string): either "left","right" or "middle". Decides what button that
will be sent to the running program.
Returns:
bool: True if successful, False otherwise.
Raises:
SyntaxError: The button param does not contain "left","right og "middle"
"""
hwnd = self.win_handler.get_hwnd()
if all(isinstance(elem, float) for elem in coords):
coords = self.to_pixel(coords)
logging.debug("Trying to click on:" + str(coords) + " with " + button + " button")
x = coords[0]
y = coords[1]
if "right" in button.lower():
_button_state = win32con.MK_RBUTTON
_button_down = win32con.WM_RBUTTONDOWN
_button_up = win32con.WM_RBUTTONUP
elif "left" in button.lower():
_button_state = win32con.MK_LBUTTON
_button_down = win32con.WM_LBUTTONDOWN
_button_up = win32con.WM_LBUTTONUP
elif "middle" in button.lower():
_button_state = win32con.MK_MBUTTON
_button_down = win32con.WM_MBUTTONDOWN
_button_up = win32con.WM_MBUTTONUP
else:
raise SyntaxError('"Button" needs to contain "left", "right" or "middle"')
l_param = win32api.MAKELONG(x, y)
win32api.SendMessage(hwnd, win32con.WM_MOUSEMOVE,0,l_param)
time.sleep(0.2)
win32api.SendMessage(hwnd,_button_down, _button_state, l_param)
time.sleep(0.1)
if not hold: #Do not release the button if hold is true
win32api.SendMessage(hwnd, _button_up, 0, l_param)
self._last_x = x
self._last_y = y
return True
def hold_and_drag(self,start,end,steps,button="left"):
hwnd = self.win_handler.get_hwnd()
if all(isinstance(elem, float) for elem in start):
start = self.to_pixel(start)
if all(isinstance(elem, float) for elem in end):
end = self.to_pixel(end)
step_x = (float(end[0] - start[0])) / steps
step_y = (float(end[1] - start[1])) / steps
if "right" in button.lower():
_button_state = win32con.MK_RBUTTON
_button_down = win32con.WM_RBUTTONDOWN
_button_up = win32con.WM_RBUTTONUP
elif "left" in button.lower():
_button_state = win32con.MK_LBUTTON
_button_down = win32con.WM_LBUTTONDOWN
_button_up = win32con.WM_LBUTTONUP
elif "middle" in button.lower():
_button_state = win32con.MK_MBUTTON
_button_down = win32con.WM_MBUTTONDOWN
_button_up = win32con.WM_MBUTTONUP
else:
raise SyntaxError('"Button" needs to contain "left", "right" or "middle"')
self.move(start)
l_param = win32api.MAKELONG(start[0], start[1])
time.sleep(0.1)
win32api.SendMessage(hwnd,_button_down,_button_state,l_param)
time.sleep(0.1)
x, y = start
for step in range(0,steps):
x += step_x
y += step_y
self.move((int(x),int(y)), button=button)
time.sleep(0.01)
l_param = win32api.MAKELONG(int(x), int(y))
win32api.SendMessage(hwnd,_button_up,0,l_param)
self._last_x = x
self._last_y = y