def show_menu(self):
menu = win32gui.CreatePopupMenu()
self.create_menu(menu, self.menu_options)
# win32gui.SetMenuDefaultItem(menu, 1000, 0)
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu,
win32con.TPM_LEFTALIGN,
pos[0],
pos[1],
0,
self.hwnd,
None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
python类SetForegroundWindow()的实例源码
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
if lparam == win32con.WM_LBUTTONUP:
print "You clicked me."
elif lparam == win32con.WM_LBUTTONDBLCLK:
print "You double-clicked me - goodbye"
win32gui.DestroyWindow(self.hwnd)
elif lparam == win32con.WM_RBUTTONUP:
print "You right clicked me."
menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, gbk("????"))
win32gui.AppendMenu(menu, win32con.MF_STRING, 1024, gbk("????"))
win32gui.AppendMenu(menu, win32con.MF_STRING, 1025, gbk("????"))
win32gui.AppendMenu(menu, win32con.MF_STRING, 1026, gbk("????"))
pos = win32gui.GetCursorPos()
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
return 1
def enter():
time.sleep(2)
appname = "?????"
enter = 0x0D
window = win32gui.FindWindow(None, appname)
try:
win32gui.SetForegroundWindow(window)
except:
pass
win32api.keybd_event(enter,0,0,0)
time.sleep(0.1)
win32api.keybd_event(enter,0,0,0)
time.sleep(0.1)
win32api.keybd_event(enter,0,0,0)
time.sleep(0.1)
win32api.keybd_event(enter,0,0,0)
time.sleep(0.1)
win32api.keybd_event(enter,0,0,0)
time.sleep(0.1)
def hawk_enter():
appname = "?????"
enter = 0x0D
count = 0
while True:
if count % 10 == 0:
window = win32gui.FindWindow(None, appname)
try:
win32gui.SetForegroundWindow(window)
except:
pass
if window != 0:
win32api.keybd_event(enter,0,0,0)
time.sleep(0.05)
win32api.keybd_event(enter,0 ,win32con.KEYEVENTF_KEYUP ,0)
time.sleep(1)
count += 1
def show_menu(self):
menu = win32gui.CreatePopupMenu()
self.create_menu(menu, self.menu_options)
#win32gui.SetMenuDefaultItem(menu, 1000, 0)
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu,
win32con.TPM_LEFTALIGN,
pos[0],
pos[1],
0,
self.hwnd,
None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
if lparam==win32con.WM_LBUTTONUP:
print "You clicked me."
elif lparam==win32con.WM_LBUTTONDBLCLK:
print "You double-clicked me - goodbye"
win32gui.DestroyWindow(self.hwnd)
elif lparam==win32con.WM_RBUTTONUP:
print "You right clicked me."
menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu( menu, win32con.MF_STRING, 1023, "Display Dialog")
win32gui.AppendMenu( menu, win32con.MF_STRING, 1024, "Say Hello")
win32gui.AppendMenu( menu, win32con.MF_STRING, 1025, "Exit program" )
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
return 1
def do_close_session():
hwnd = QQ_checkTargetWindow()
if hwnd == 0:
print "Window doesn't exist, no need to close session."
return
else:
print "move the window to front."
win32gui.SetForegroundWindow(hwnd)
time.sleep(1.0)
for try_time in range(0, 10, 1):
print "try to close the current session, try_time = " + str(try_time)
QQ_CloseWindow()
time.sleep(1.0)
if QQ_checkTargetWindow() != 0:
QQ_CloseWindow()
print "close fails, probably the file is still in transmission."
time.sleep(5.0)
else:
print "session closed."
return
print "can't close the current session, abort."
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam):
if lparam==win32con.WM_LBUTTONUP:
print("You clicked me.")
elif lparam==win32con.WM_LBUTTONDBLCLK:
print("You double-clicked me - goodbye")
win32gui.DestroyWindow(self.hwnd)
elif lparam==win32con.WM_RBUTTONUP:
print("You right clicked me.")
menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu( menu, win32con.MF_STRING, 1023, "Display Dialog")
win32gui.AppendMenu( menu, win32con.MF_STRING, 1024, "Say Hello")
win32gui.AppendMenu( menu, win32con.MF_STRING, 1025, "Exit program" )
pos = win32gui.GetCursorPos()
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
return 1
_webkeywords.py 文件源码
项目:robotframework-weblibrary
作者: Netease-AutoTest
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def web_upload_file(self, filepath):
"""????(??flash????)
filepath ?????????????????????????${CURDIR}${/}Res${/}Plus_Web${/}pic0.jpg
${CURDIR}????????????
RF?filepath???????unicode
??????????????????????????????????
| Web Upload File | ${filepath} |
| Web Upload File | ${CURDIR}${/}Res${/}Plus_Web${/}pic0.jpg |
"""
filepath=os.path.abspath(filepath)
change = str(filepath)
time.sleep(1)
self._handle = win32gui.FindWindow(None, u"??")#??“??”?????
win32gui.SetForegroundWindow(self._handle)#??????
SendKeys.SendKeys(change)
time.sleep(1)
SendKeys.SendKeys("{ENTER}")
def _show_menu(self):
self.FIRST_ID = 1023
self.menu_options = (('Show', None, self.onShow),('Run', None, self.onRun),('Quit', None, self.onQuit))
self._next_action_id = self.FIRST_ID
self.menu_actions_by_id = set()
self.menu_options = self._add_ids_to_menu_options(list(self.menu_options))
self.menu_actions_by_id = dict(self.menu_actions_by_id)
del self._next_action_id
menu = win32gui.CreatePopupMenu()
self.create_menu(menu, self.menu_options)
pos = win32gui.GetCursorPos()
win32gui.SetForegroundWindow(self.hwnd)
win32gui.TrackPopupMenu(menu,
win32con.TPM_LEFTALIGN,
pos[0],
pos[1],
0,
self.hwnd,
None)
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
ctrl_omnipeek.py 文件源码
项目:Automation-Framework-for-devices
作者: tok-gogogo
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def omnipeek_close(self):
print 'Close the omnipeek application software'
hwnd = win32gui.FindWindow('OmniPeek',None)
win32gui.SetForegroundWindow(hwnd)
self.shortcut_keys(['Alt','F'])
self.sleep('1')
self.shortcut_keys(['X'])
self.sleep('1')
hwd = win32gui.FindWindow(None,'OmniPeek')
#print hwd
#The window exist
if hwd:
win32gui.SetForegroundWindow(hwd) #ö
self.sleep('1')
self.shortcut_keys(['Alt','Y'])
self.sleep('5')
return True
def OnInitDialog(self, hwnd, msg, wparam, lparam):
self.hwnd = hwnd
desktop = win32gui.GetDesktopWindow()
dt_l, dt_t, dt_r, dt_b = win32gui.GetWindowRect(desktop)
centre_x, centre_y = win32gui.ClientToScreen( desktop, ( (dt_r-dt_l)/2, (dt_b-dt_t)/2) )
bmCtrl = win32gui.GetDlgItem(self.hwnd, IDC_BITMAP)
win32gui.SendMessage(bmCtrl, win32con.STM_SETIMAGE, win32con.IMAGE_BITMAP, self.hSplash)
win32gui.SetWindowPos(self.hwnd, win32con.HWND_TOPMOST,
centre_x-(self.bmWidth/2), centre_y-(self.bmHeight/2),
self.bmWidth, self.bmHeight, win32con.SWP_HIDEWINDOW)
win32gui.SetForegroundWindow(self.hwnd)
def show_window(pid, delay = 0.5):
"""set a windows as principal based on it's process id"""
time.sleep(delay)
for hwnd in get_hwnds(pid):
win32gui.SetForegroundWindow(hwnd)
win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
def set_foreground(self):
win32gui.SetForegroundWindow(self.hwnd)
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 set_foreground(self):
win32gui.SetForegroundWindow(self.hwnd)
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 set_foreground(self):
"""Get the focus on the desired open window"""
logging.debug("set the window foreground")
if self.hwnd:
logging.debug("Find the window and set it as foreground")
win32gui.SetForegroundWindow(self.hwnd)
def getstate(self):
win32gui.SetForegroundWindow(self.hwnd)
bbox = win32gui.GetWindowRect(self.hwnd)
img = ImageGrab.grab(bbox)
img = img.resize([84, 84], Image.ANTIALIAS)
pilim = np.array(img, dtype=np.uint8)
pilim = rgb2gray(pilim)
return pilim
def QQ_SendTextWithAt(str):
os.system(qq_shortcut)
try_time = 0
while True:
time.sleep(0.5)
hwnd = win32gui.FindWindow(None, '??&??')
# hwnd = win32gui.FindWindow(None, 'OSVT?O?')
print('try_time = %d, hwnd = %d' % (try_time, hwnd))
if hwnd != 0:
break
elif try_time >= 60:
print ('SendTextToQQ Error.')
return
else:
try_time = try_time + 1
win32gui.SetForegroundWindow(hwnd)
QQ_PrintTextWithAt(str)
QQ_Enter()
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('v'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('v'), 0)
#win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, 'aaa')
#win32gui.SetWindowText(hwnd, 'aaa')
#win32gui.ReplaceSel()
#win32gui.PostMessage(hwnd, win32con.WM_CHAR, '', 3)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
def QQ_OpenActiveWindow():
hwnd = QQ_checkTargetWindow()
if hwnd == 0:
return False
win32gui.SetForegroundWindow(hwnd)
return True
# Press the "Alt + H' key.
def focus_window(hwnd):
"""
Focuses the given window.
Args:
hwnd (int): The window handler.
"""
wg.ShowWindow(hwnd, wc.SW_SHOW)
wg.ShowWindow(hwnd, wc.SW_SHOWNOACTIVATE)
wg.SetForegroundWindow(hwnd)
def foreach_window(self):
def callback(hwnd, lparam):
title = win32gui.GetWindowText(hwnd).lower()
for window in self.to_close:
if window in title:
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
print "Closed window ({})".format(title)
for window in self.clicks:
if window in title:
self._windows[hwnd] = {
"matches": self.clicks[window],
"to_click": [],
"buttons": []
}
try:
win32gui.EnumChildWindows(hwnd, self.foreach_child(), hwnd)
except:
print "EnumChildWindows failed, moving on."
for button_toclick in self._windows[hwnd]['to_click']:
for button in self._windows[hwnd]['buttons']:
if button_toclick in button['text']:
win32gui.SetForegroundWindow(button['handle'])
win32gui.SendMessage(button['handle'], win32con.BM_CLICK, 0, 0)
print "Clicked on button ({} / {})".format(title, button['text'])
del self._windows[hwnd]
return True
return callback
def restoreFocusWindow(hwnd):
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
win32gui.SetForegroundWindow(hwnd)
time.sleep(0.2)
def focusWindow(hwnd):
"""
??????
:param hwnd: ????
:return:
"""
win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED)
win32gui.SetForegroundWindow(hwnd)
def SetAsForegroundWindow(cls, hwnd):
#??ALT??ALT???%???
cls.shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)
def SetForegroundWindow(cls, whd):
cls.SetAsForegroundWindow(whd)
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 set_foreground(self):
"""Get the focus on the desired open window"""
win32gui.SetForegroundWindow(self._handle)
def _set_foreground_window(hwnd):
shell = win32com.client.Dispatch('WScript.Shell')
shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)