admin
2025-06-10 568c763084b926a6f2d632b7ac65b9ec8280752f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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())