import time # 获取同花顺副屏幕1句柄 import win32api import win32con import win32_util import win32gui def get_ths_second_screen_menu_hwnd(): hwnds = win32_util.search_window("副屏1") if hwnds: hwnd = hwnds[0] hwnd = win32gui.FindWindowEx(hwnd, None, "AfxFrameOrView100s", None) temp = None for i in range(0, 100): temp = win32gui.FindWindowEx(hwnd, temp, "AfxWnd100s", None) if win32gui.IsWindowVisible(temp): left, top, right, bottom = win32gui.GetWindowRect(temp) if bottom - top > right - left: break if temp is None: return None hwnd = temp hwnd = win32gui.FindWindowEx(hwnd, None, "#32770", None) hwnd = win32gui.FindWindowEx(hwnd, None, "block_list_page", None) if hwnd: return hwnd return None def get_trade_refesh_hwnd(): hwnds = win32_util.search_window("网上股票交易系统") if hwnds: for hwnd in hwnds: content_hwnd = win32gui.FindWindowEx(hwnd, None, "AfxMDIFrame140s", None) temp = win32gui.FindWindowEx(content_hwnd, None, "#32770", None) for i in range(10): if win32gui.IsWindowVisible(temp): break temp = win32gui.FindWindowEx(content_hwnd, temp, "#32770", None) if not temp: continue t1 = win32gui.FindWindowEx(temp, None, None, "过滤") if not win32gui.IsWindowVisible(t1): continue tool_hwnd = win32gui.FindWindowEx(hwnd, None, "ToolbarWindow32", None) temp = win32gui.FindWindowEx(tool_hwnd, None, "#32770", None) if temp: nq = win32gui.FindWindowEx(temp, None, None, "内嵌") if nq and win32gui.IsWindowVisible(nq): continue return tool_hwnd return None # 获取交易句柄 def get_trade_hwnd(): hwnds = win32_util.search_window("网上股票交易系统") if hwnds: hwnd = hwnds[0] return hwnd return None def get_flash_trade_hwnds(): """ 获取闪电交易句柄 :return: """ buy_win_list = [] hWndList = [] win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList) for hwnd in hWndList: clsname = win32gui.GetClassName(hwnd) if clsname == '#32770' and win32gui.IsWindowVisible(hwnd): pos = win32gui.GetWindowRect(hwnd) width = pos[2] - pos[0] height = pos[3] - pos[1] if 500 > width > 100 and 500 > height > 50: # 查找确定按钮 try: buy_win = win32gui.GetDlgItem(hwnd, 0x000003EE) if buy_win > 0: win_name = win32_util.getText(buy_win) if win_name == '一键买入[B]' or win_name == '一键卖出[S]': buy_win_list.append(hwnd) except Exception as e: print(e) return buy_win_list def get_ths_main_content_hwnd(): hwnds = win32_util.search_window("同花顺", max_count=1) if hwnds: hwnd = hwnds[0] child = None for i in range(0, 20): child = win32gui.FindWindowEx(hwnd, child, None, None) if child: left, top, right, bottom = win32gui.GetWindowRect(child) if right - left > 1000 and bottom - top > 800: return child return None __cache_hwnd = {} def get_ths_main_content_hwnd_cached(): """ 获取同花顺主页句柄 :return: """ target_hwnd = __cache_hwnd.get("ths_main_content_hwnd") if target_hwnd and win32_util.is_visible(target_hwnd): return target_hwnd target_hwnd = get_ths_main_content_hwnd() if target_hwnd: __cache_hwnd["ths_main_content_hwnd"] = target_hwnd return target_hwnd # 批量点击事件 def betch_click(hwnd, ps, space_time=0.5): for p in ps: win32_util.visual_click(hwnd, p) time.sleep(space_time) # 将代码加入自选 def input_code(hwnd, num_str): for c in num_str: code = -1 if c == '0': code = 48 elif c == '1': code = 49 elif c == '2': code = 50 elif c == '3': code = 51 elif c == '4': code = 52 elif c == '5': code = 53 elif c == '6': code = 54 elif c == '7': code = 55 elif c == '8': code = 56 elif c == '9': code = 57 win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, code, 0) win32gui.PostMessage(hwnd, win32con.WM_KEYUP, code, 0) time.sleep(0.1) def add_code_to_zixuan(num_str): hwnds = win32_util.search_window("看盘页面") if hwnds: origin_hwnd = hwnds[0] for w in hwnds: title = win32_util.getText(w) if title.find("同花顺") >= 0: origin_hwnd = w break input_code(origin_hwnd, num_str[:1]) # Afx:00400000:0 hwnds = [] try: hwnd = win32gui.GetDesktopWindow() temp = None while True: if temp and win32gui.IsWindowVisible(temp): classname = win32gui.GetClassName(temp) if classname and classname.find("Afx:") == 0 and classname.endswith(":0"): hwnds.append(temp) temp = win32gui.FindWindowEx(hwnd, temp, None, None) if not temp: break except: pass hwnd = win32gui.FindWindowEx(hwnds[0], None, "Edit", "") print(hwnd) input_code(hwnd, num_str[1:]) time.sleep(0.5) win32gui.SendMessage(hwnds[0], win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) win32gui.PostMessage(hwnds[0], win32con.WM_KEYUP, win32con.VK_RETURN, 0) # time.sleep(0.5) # win32gui.SetForegroundWindow(origin_hwnd) # win32api.keybd_event(win32con.VK_INSERT, 0, 0, 0) # 按下键 # win32api.keybd_event(win32con.VK_INSERT, 0, win32con.KEYEVENTF_KEYUP, 0) if __name__ == "__main__": print(get_flash_trade_hwnds())