我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用win32gui.PyMakeBuffer()。
def UnpackNMITEMACTIVATE(lparam): format = _nmhdr_fmt + _nmhdr_align_padding if is64bit: # the struct module doesn't handle this correctly as some of the items # are actually structs in structs, which get individually aligned. format = format + "iiiiiiixxxxP" else: format = format + "iiiiiiiP" buf = win32gui.PyMakeBuffer(struct.calcsize(format), lparam) return _MakeResult("NMITEMACTIVATE hwndFrom idFrom code iItem iSubItem uNewState uOldState uChanged actionx actiony lParam", struct.unpack(format, buf)) # MENUITEMINFO struct # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Resources/Menus/MenuReference/MenuStructures/MENUITEMINFO.asp # We use the struct module to pack and unpack strings as MENUITEMINFO # structures. We also have special handling for the 'fMask' item in that # structure to avoid the caller needing to explicitly check validity # (None is used if the mask excludes/should exclude the value)
def doubleClickStatic(hwnd): """Simulates a double mouse click on a static Parameters ---------- hwnd Window handle of the required static. Usage example: TODO """ _sendNotifyMessage(hwnd, win32con.STN_DBLCLK) # def getEditText(hwnd): # bufLen = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) + 1 # print(bufLen) # buffer = win32gui.PyMakeBuffer(bufLen) # win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, bufLen, buffer) # # text = buffer[:bufLen] # return text
def GetChildapp(self,str_app): print '******** GetChildapp fuction ********' hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) if hwnd>1: hChild = win32gui.GetWindow(hwnd,win32con.GW_CHILD) bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) totalnum = 1 while hChild : hChild = win32gui.GetWindow(hChild,win32con.GW_HWNDNEXT) n = win32gui.SendMessage(hChild,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print '@@@@@@@@@@@' print win32gui.GetWindowText(hChild) print str ''' if totalnum ==3: win32gui.SendMessage(hChild,win32con.WM_SETTEXT,None,'Realtek 10/100/1000 Ethernet NIC') ''' print totalnum,hChild totalnum = totalnum + 1 print '******** GetChildapp fuction ********',totalnum
def get_expect_sec_window(self): '''return the expect window hwnd''' # control = win32gui.FindWindowEx(window, 0, 'static', None) # buffer = win32gui.PyMakeBuffer(20) # length = win32gui.SendMessage(control, win32con.WM_GETTEXT, 20, buffer) # # result = buffer[:length] # print result # time.sleep(1) return self.expect_sec_window
def dumpWindow_pos(hwnd): '''Dump all controls from a window into a nested list Useful during development, allowing to you discover the structure of the contents of a window, showing the text and class of all contained controls. Arguments: The window handle of the top level window to dump. Returns A nested list of controls. Each entry consists of the control's hwnd, its text, its class, and its sub-controls, if any. Usage example: replaceDialog = findTopWindow(wantedText='Replace') pprint.pprint(dumpWindow(replaceDialog)) ''' windows = [] try: win32gui.EnumChildWindows(hwnd, _windowEnumerationHandler, windows) except win32gui.error: # No child windows return windows = [list(window) for window in windows] for window in windows: childHwnd, windowText, windowClass = window window_content = dumpWindow(childHwnd) if window_content: window.append(window_content) print '###################' print childHwnd,'pos:',win32gui.GetWindowRect(childHwnd),'window_content:',window_content tuple_t = win32gui.GetWindowRect(childHwnd) if tuple_t[0]==179 and tuple_t[2]==561: print window_content[0][0],'pos:',win32gui.GetWindowRect(window_content[0][0]) if window_content[0][0]: bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) n = win32gui.SendMessage(window_content[0][0],win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print str print getEditText(window_content[0][0]) time.sleep(1) #win32gui.SendMessage(window_content[0][0],win32con.WM_SETTEXT,None,'Realtek 10/100/1000 Ethernet NIC') print '###################' return windows
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_edit(self,str_app='´ò¿ª',control_class='ComboBox',filename='wtp_cuo1.pcap',control_name='',stop_flag='0'): print "*********Find_Gui_edit function**********",str_app time.sleep(1) #self.Mousepos_print() print 'str_app',str_app hwnd = win32gui.FindWindow(None, str_app) print 'hwnd',hwnd win32gui.SetForegroundWindow(hwnd) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) bufLen=256 buf =win32gui.PyMakeBuffer(bufLen) while comboHwnd: if stop_flag=='1': win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break print "control_class:",comboHwnd cla = win32gui.GetClassName(comboHwnd) print cla if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] if (len(control_name ) ==0 and n==0) or (len(control_name )>0 and str.find(control_name)>-1 ): win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return True
def get_msg(self,hwd_n): hwd_edit = self.found_hwnd(hwd_n) bufLen = 1024 buf = win32gui.PyMakeBuffer(bufLen) n = win32gui.SendMessage(hwd_edit, win32con.WM_GETTEXT, bufLen, buf) #print 'Get the message:',buf[0:n] return buf[0:n]
def _window_enum_dialog_callback_(self, hwnd, extra): '''Call back func which checks each open window and matches the name of window using reg ex''' #self._handle = None matchtext = extra logging.debug("call _window_enum_dialog_callback") classname = win32gui.GetClassName(hwnd) title_text = win32gui.GetWindowText(hwnd) title_text = title_text.decode('gbk').encode('utf-8') if classname == '#32770': matchtext = matchtext.encode('utf-8') logging.debug("msg: " + matchtext) logging.debug("Title is: " + title_text) # buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0) # buffer_text = win32gui.PyMakeBuffer(buf_size) # win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buffer_text) # # logging.debug("Buffer_text: " + buffer_text) # logging.debug("Buffer_text decode(gbk).encode(utf-8): " + # buffer_text.decode('gbk').encode('utf-8')) # windowText = buffer_text[:buf_size] # # try: # windowText = windowText.decode('gbk').encode('utf-8') #unicode(windowText, 'utf-8') # except: # logging.debug("_window_enum_dialog_callback unicode exception") # pass # # message = ['Handle:\t' + str(hwnd), # 'Class Name:\t' + classname, # 'Window Text:\t' + windowText] # # logging.debug("Print the message: " + str(message)) # # #if re.match(wildcard, windowText) != None: if (matchtext.strip() == title_text.strip()): logging.debug("!!!!BINGO!!!!") self.hwnd = hwnd return False else: logging.debug("No matched .....") return True
def get_expect_window_label_text(self, _expect_sec_window): ''' Try to get window label text ''' # label_text = u"????" # label_text = label_text.encode('utf-8') # return label_text label_text = '' window = self.expect_sec_window child_control = None last_child = 0 while True: logging.debug("Find the child controls for parent window") child_control = win32gui.FindWindowEx(window, last_child, 'static', None) if not child_control: logging.debug("The child is None") break; else: logging.debug("The child is not None, ") buffer = win32gui.PyMakeBuffer(200) length = win32gui.SendMessage(child_control, win32con.WM_GETTEXT, 200, buffer) result = buffer[:length] result = result.decode('gbk').encode('utf-8') logging.debug("Got the child text is :" + result) last_child = child_control label_text = result time.sleep(0.5) def _winfun(hwnd, lparam): s = win32gui.GetWindowText(hwnd) s = s.decode('gbk').encode('utf-8') logging.debug("winfun, child_hwnd: %d txt: %s" % (hwnd, s)) return 1 if window: logging.debug("To enumerate all the child windows") win32gui.EnumChildWindows(self.expect_sec_window, _winfun, None) #bufferlength = struct.pack('i', 255) #count = win32gui.SendMessage(self.expect_sec_window, win32con.get, 0, 0) #for itemIndex in range(count): # value = array.array('c', bufferlength +str().ljust(253)) # valueLength = win32gui.SendMessage(self.expect_sec_window, getValueMessage, itemIndex, value) # yield value.tostring()[:valueLength] return label_text
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