def __get_order_msg(self): # time.sleep(0.005) # 根据 弹出窗口大小判断更快,所以按大小判断 def call_back(handle, dialog_l): _left, _top, _right, _bottom = win32gui.GetWindowRect(handle) # (_right - _left == 300) and (_bottom - _top == 195) # print(win32gui.GetParent(handle)) if win32gui.GetClassName(handle) == "#32770" and \ win32gui.GetWindow(handle, win32con.GW_OWNER) == self.__parent_trade: if (_right - _left == 362) and (_bottom - _top == 204): dialog_l.append(handle) """ 下单 时不论成功失败,肯定在最后有一个 提示 弹框 """ while True: dialog_list = [] win32gui.EnumWindows(call_back, dialog_list) # 获得 每个 dialog 句柄的子句柄,判断出是 提示 弹出窗 prompt_list = [] for dialog in dialog_list: li = [] win32gui.EnumChildWindows( dialog, lambda handle, param: param.append(handle), li) for l in li: txt = win32gui.GetWindowText(l) if txt == "提示": prompt_list.append(dialog) if len(prompt_list) > 1: exit(-1) # 如果没有提示信息窗口,而存在委托窗口,判断无误下单后 退出 if len(prompt_list) == 0: time.sleep(0.01) continue if len(prompt_list) == 1: prompt = prompt_list[0] prompt_info = {} prompt_sons = [] win32gui.EnumChildWindows( prompt, lambda handle, param: param.append(handle), prompt_sons) for prompt_son in prompt_sons: txt = win32gui.GetWindowText(prompt_son) left, top, right, bottom = win32gui.GetWindowRect( prompt_son) if right - left == 332 and bottom - top == 129: prompt_info.update(info=get_item_text(prompt_son)) elif txt == "确定" or txt == "终止": prompt_info.update(confirm_btn=prompt_son) # 提示信息弹出框 发送取消 后,直接退出 win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONDOWN, None, None) win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONUP, None, None) return prompt_info["info"]
def win_is_msg(hand): text = "" sons = [] win32gui.EnumChildWindows( hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return '检测到您正在拷贝数据' in text and "提示" in text and '确定' in text
def win_is_verify_code(hand, parent_trade_hwnd) -> bool: if win32gui.GetClassName(hand) != "#32770" or win32gui.GetWindow(hand, win32con.GW_OWNER) != parent_trade_hwnd: return False text = "" sons = [] win32gui.EnumChildWindows(hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return '检测到您正在拷贝数据' in text and "提示" in text and '确定' in text
def win_is_msg(hand): text = "" sons = [] win32gui.EnumChildWindows( hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return "提示" in text and ("确定" in text or "终止" in text) and ("成功" in text or "失败" in text)
def __send_msg(self, stock_code, price, lot): # 使用 windows 消息机制 登录 win32gui.SendMessage(self.__edit_set["code"], win32con.WM_SETTEXT, None, stock_code) # WM_SETTEXT 不管用,使用 WM_CHAR 消息,先删除原来的内容 text = get_item_text(self.__edit_set["price"]) if text: for i in range(0, len(text)): win32api.PostMessage(self.__edit_set["price"], win32con.WM_CHAR, win32con.VK_BACK, 0) content = str(price) for char in list(content): win32api.PostMessage(self.__edit_set["price"], win32con.WM_CHAR, ord(char), 0) win32api.SendMessage(self.__edit_set["lot"], win32con.WM_SETTEXT, None, str(lot * 100)) win32api.PostMessage(self.__edit_set["buy_btn"], win32con.WM_LBUTTONDOWN, None, None) win32api.PostMessage(self.__edit_set["buy_btn"], win32con.WM_LBUTTONUP, None, None)
def __get_order_msg(self): # 判断窗口是不是提示窗口,是,就返回true def win_is_msg(hand): text = "" sons = [] win32gui.EnumChildWindows( hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return "提示" in text and ("确定" in text or "终止" in text) and ("成功" in text or "失败" in text) # # 根据 弹出窗口大小判断更快,所以按大小判断 def call_back(handle, dialog_l): _left, _top, _right, _bottom = win32gui.GetWindowRect(handle) # (_right - _left == 300) and (_bottom - _top == 195) # print(win32gui.GetParent(handle)) if win32gui.GetClassName(handle) == "#32770" and \ win32gui.GetWindow(handle, win32con.GW_OWNER) == self.__parent_trade: # if (_right - _left == 362) or (_right - _left == 341): # print(handle) # dialog_l.append(handle) if win_is_msg(handle): dialog_l.append(handle) # todo """ 下单 时不论成功失败,肯定在最后有一个 提示 弹框 """ while True: dialog_list = [] win32gui.EnumWindows(call_back, dialog_list) # 获得 每个 dialog 句柄的子句柄,判断出是 提示 弹出窗 if len(dialog_list) > 1: exit(-1) # 如果没有提示信息窗口,而存在委托窗口,判断无误下单后 退出 if len(dialog_list) == 0: time.sleep(0.01) continue if len(dialog_list) == 1: prompt = dialog_list[0] prompt_info = {} prompt_sons = [] win32gui.EnumChildWindows( prompt, lambda handle, param: param.append(handle), prompt_sons) for prompt_son in prompt_sons: txt = win32gui.GetWindowText(prompt_son) if txt == "": txt = get_item_text(prompt_son) if txt and (("[" in txt and "]" in txt) or "成功" in txt): prompt_info.update(info=str(txt).replace("\r\n", "")) elif txt == "确定" or txt == "终止": prompt_info.update(confirm_btn=prompt_son) # 提示信息弹出框 发送取消 后,直接退出 win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONDOWN, None, None) win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONUP, None, None) return prompt_info["info"]
def __get_order_msg(self): # 判断窗口是不是提示窗口,是,就返回true def win_is_msg(hand): text = "" sons = [] win32gui.EnumChildWindows( hand, lambda handle, param: param.append(handle), sons) for son in sons: if win32gui.GetClassName(son) in ["Static", "Button"]: t = get_item_text(son) if t: text = text + t return '检测到您正在拷贝数据' in text and "提示" in text and '确定' in text # # 1. 可以根据 验证码弹窗的 OWNER 句柄 = 父句柄 判断 # 2. 可以根据 弹出窗口大小判断更快,所以按大小判断 def call_back(handle, dialog_l): _left, _top, _right, _bottom = win32gui.GetWindowRect(handle) # (_right - _left == 300) and (_bottom - _top == 195) # print(win32gui.GetParent(handle)) if win32gui.GetClassName(handle) == "#32770" and \ win32gui.GetWindow(handle, win32con.GW_OWNER) == self.__parent_trade: # if (_right - _left == 362) or (_right - _left == 341): # print(handle) # dialog_l.append(handle) if win_is_msg(handle): dialog_l.append(handle) # todo """ 下单 时不论成功失败,肯定在最后有一个 提示 弹框 """ while True: dialog_list = [] win32gui.EnumWindows(call_back, dialog_list) # 获得 每个 dialog 句柄的子句柄,判断出是 提示 弹出窗 if len(dialog_list) > 1: exit(-1) # 如果没有提示信息窗口,而存在委托窗口,判断无误下单后 退出 if len(dialog_list) == 0: time.sleep(0.01) continue if len(dialog_list) == 1: prompt = dialog_list[0] prompt_info = {} prompt_sons = [] _left, _top, _right, _bottom = win32gui.GetWindowRect(prompt) input_pos = [ _left + (_right - _left) * 0.3991, _top + (_bottom - _top) * 0.5504 ] image_pos = [ _left + (_right - _left) * 0.6362, _top + (_bottom - _top) * 0.5504 ] win32gui.EnumChildWindows( prompt, lambda handle, param: param.append(handle), prompt_sons) for prompt_son in prompt_sons: if win32gui.GetClassName( prompt_son) == 'Static' and pos_in_window_rect( image_pos, win32gui.GetWindowRect(prompt_son)): prompt_info.update(image=prompt_son) elif win32gui.GetClassName( prompt_son) == 'Edit' and pos_in_window_rect( input_pos, win32gui.GetWindowRect(prompt_son)): prompt_info.update(input=prompt_son) txt = win32gui.GetWindowText(prompt_son) if txt == "": txt = get_item_text(prompt_son) elif txt == "确定": prompt_info.update(confirm_btn=prompt_son) # 提示 弹出框, 使用ocr识别 identify_code = ocr_string_from_hwnd(prompt_info["image"], expand=10) win32gui.SendMessage(prompt_info["input"], win32con.WM_SETTEXT, None, identify_code) win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONDOWN, None, None) win32api.PostMessage(prompt_info["confirm_btn"], win32con.WM_LBUTTONUP, None, None) return