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类PostMessage()的实例源码
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 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 Save():
try:
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???'),None,'Button','??(&S)'),win32con.BM_CLICK,0,0)
time.sleep(1)
global handle
handle = None
win32gui.EnumChildWindows(win32gui.FindWindow('#32770','?????'),handle_window,'?(&Y)')
if handle!= None:
win32gui.PostMessage(handle,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 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 OnCommand(self, hwnd, msg, wparam, lparam):
id = win32api.LOWORD(wparam)
if id == IDC_BUTTON_SEARCH:
self.ClearListItems()
def fill_slowly(q, hwnd):
import time
for i in range(20):
q.put(("whatever", str(i+1), "Search result " + str(i) ))
win32gui.PostMessage(hwnd, WM_SEARCH_RESULT, 0, 0)
time.sleep(.25)
win32gui.PostMessage(hwnd, WM_SEARCH_FINISHED, 0, 0)
import threading
self.result_queue = Queue.Queue()
thread = threading.Thread(target = fill_slowly, args=(self.result_queue, self.hwnd) )
thread.start()
elif id == IDC_BUTTON_DISPLAY:
print "Display button selected"
sel = win32gui.SendMessage(self.hwndList, commctrl.LVM_GETNEXTITEM, -1, commctrl.LVNI_SELECTED)
print "The selected item is", sel+1
# These function differ based on how the window is used, so may be overridden
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 OnCommand(self, hwnd, msg, wparam, lparam):
id = win32api.LOWORD(wparam)
if id == IDC_BUTTON_SEARCH:
self.ClearListItems()
def fill_slowly(q, hwnd):
import time
for i in range(20):
q.put(("whatever", str(i+1), "Search result " + str(i) ))
win32gui.PostMessage(hwnd, WM_SEARCH_RESULT, 0, 0)
time.sleep(.25)
win32gui.PostMessage(hwnd, WM_SEARCH_FINISHED, 0, 0)
import threading
self.result_queue = queue.Queue()
thread = threading.Thread(target = fill_slowly, args=(self.result_queue, self.hwnd) )
thread.start()
elif id == IDC_BUTTON_DISPLAY:
print("Display button selected")
sel = win32gui.SendMessage(self.hwndList, commctrl.LVM_GETNEXTITEM, -1, commctrl.LVNI_SELECTED)
print("The selected item is", sel+1)
# These function differ based on how the window is used, so may be overridden
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)
def Export_plate():
try:
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????'),None,'#32770','???????'),None,'Button','????'),win32con.BM_CLICK,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 IsOK():
try:
#??
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','TdxW'),None,'Button','??'),win32con.BM_CLICK,0,0)
time.sleep(1)
#?????????
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','???????'),None,'Button','??'),win32con.BM_CLICK,0,0)
except Exception as e:
ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
#???????
def Info():
try:
win32gui.PostMessage(win32gui.FindWindow('#32770','?????'),win32con.WM_CLOSE,0,0)
time.sleep(5)
except Exception as e:
ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
#???? Ctrl+T
def EXE():
try:
while win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),None,'Static','????. ') == 0:
zs = win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),None,'Button','????')
print(zs)
print(type(zs))
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),None,'Button','????'),win32con.BM_CLICK,0,0)
time.sleep(7)
print("????")
break
except Exception as e:
ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
#???
def Complement_data():
try:
time.sleep(3)
if win32gui.FindWindowEx(win32gui.FindWindow('#32770','TdxW'),None,'Button','?(&Y)') != 0:
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','TdxW'),None,'Button','?(&Y)'),win32con.BM_CLICK,0,0)
time.sleep(5)
print("???")
except Exception as e:
ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
#?????
def Close_Stockpickingmachine():
time.sleep(5)
try:
#while win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),None,'Static','????. ') == 0:
print("????")
time.sleep(1)
#if win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),None,'Static','????. ') != 0:
win32gui.PostMessage(win32gui.FindWindow('#32770','????'),win32con.WM_CLOSE,0,0)
time.sleep(3)
print("????")
#break
except Exception as e:
ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e))
def Close_TDX():
try:
time.sleep(3)
lstj=win32gui.FindWindow('TdxW_MainFrame_Class','???????V7.35 - [???-?????]')
print(hex(lstj))
win32gui.PostMessage(lstj,win32con.WM_CLOSE,0,0)
time.sleep(3)
win32gui.PostMessage(win32gui.FindWindowEx(win32gui.FindWindow('#32770','????'),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 __close__(self, hwnd, dummy):
"""
EnumWindows callback - sends WM_CLOSE to any window
owned by this process.
"""
TId, PId = win32process.GetWindowThreadProcessId(hwnd)
if PId == self.PId:
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
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 __close__(self, hwnd, dummy):
"""
EnumWindows callback - sends WM_CLOSE to any window
owned by this process.
"""
TId, PId = win32process.GetWindowThreadProcessId(hwnd)
if PId == self.PId:
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def close_window(hwnd):
"""
Sends a message to close the given window.
Args:
hwnd (int): The window handler.
"""
wg.PostMessage(hwnd, wc.WM_CLOSE, 0, 0)
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 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)
def sendKeyMsg(hwnd, key_code):
"""
????
:param hwnd: ????
:param key_code: ?????win32con????win32con.VK_F1
:return:
"""
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, key_code, 0) # ????
time.sleep(0.2)
win32gui.PostMessage(hwnd, win32con.WM_KEYUP, key_code, 0)
time.sleep(0.2)
def run(self, edit):
setSettingIsOpen(False)
try:
global windowsWindow
if windowsWindow:
win32gui.PostMessage(windowsWindow, win32con.WM_CLOSE, 0, 0)
windowsWindow = None
except Exception as e:
pass
def show_menu(self):
if self.MENU is None:
self.create_menu()
position = win32gui.GetCursorPos()
win32gui.SetForegroundWindow(self.HWND)
win32gui.TrackPopupMenu(
self.MENU,
win32con.TPM_LEFTALIGN,
position[0],
position[1],
0,
self.HWND,
None
)
win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)
def stop(self):
if getattr(self, 'hook', None) is not None:
del self.hook
if self.HWND is not None:
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, (self.HWND, 0))
# win32gui.PostMessage(self.HWND, win32con.WM_CLOSE, 0, 0)
win32gui.DestroyWindow(self.HWND)
self.HWND = None
self.clear_instance_check_event()
super(KeyCounter, self).stop()
raise SystemExit(0)
ctrl_waveQoE.py 文件源码
项目:Automation-Framework-for-devices
作者: tok-gogogo
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def init_PDF(self):
try:
hwnd1 = win32gui.FindWindow(PDF_CLASS,None)
except:
log_public(ERR_NO_0003)
self.m_ERROR_MSG = ERR_NO_0003
hwnd1 = 0
print 'hwnd1:',hwnd1
while hwnd1 != 0:
#close PDF window
time.sleep(2)
win32gui.PostMessage(hwnd1, win32con.WM_SYSCOMMAND, win32con.SC_CLOSE, 0);
print 'close_PDF'
try:
hwnd1 = win32gui.FindWindow(PDF_CLASS,None)
print 'new hwnd1---',hwnd1
except:
log_public(ERR_NO_0004)
self.m_ERROR_MSG = ERR_NO_0004
hwnd1 = 0
print 'new hwnd1:',hwnd1
else:
print 'hwnd1=0,init_PDF has been done'
return True
#-----------------------------------------------------------------------------
# Name: open_waveQoE
# purpose: open waveQoE.
# explain:
# Author: gongke
#
# Created: 2013/05/20
#-----------------------------------------------------------------------------
def _postNotifyMessage(hwnd, nofifyMessage):
'''Send a notify message to a control.'''
win32gui.PostMessage(win32gui.GetParent(hwnd),
win32con.WM_COMMAND,
_buildWinLong(nofifyMessage,
win32api.GetWindowLong(hwnd,
win32con.GWL_ID)),
hwnd)
def Find_Gui_key_run(self,str_app='´ò¿ª'):
time.sleep(1)
print "*********Find_Gui_button function**********"
#self.Mousepos_print()
print 'control_name:',str_app
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
print 'hwnd :',hwnd,str_app
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_F5, 0)
return True