通达信自动交易软件步骤分析

发布时间:2025-12-09 19:39:38 浏览次数:4

1、要善用spy++

2、不同的控件主要靠GetDlgCtrlID去区分

3、要获得另一个进程的焦点窗口(GetFocus)需要调用AttachThreadInput

4、尽量少用keybd_event模拟键盘输入,主要是该函数不能保证按键消息一定能被特定进程接收到。取而代之的SendMessage(hwnd, WM_IME_CHAR, ...)。而模拟回车按下要用PostMessage (hFocus, WM_KEYDOWN, VK_RETURN, 0),SendMessage不知为何不起作用

5、通达信在按下第一个数字键后会弹出键盘精灵,此时焦点窗口会转移到键盘精灵上,要重新调用GetFocus一次

6、GetWindow(hMainWnd, GW_ENABLEDPOPUP)可以获得当前的弹出窗口句柄

7、PostMessage(hPopup, WM_COMMAND, MAKEWPARAM(IDOK, BN_CLICKED), NULL),模拟“确定”键被按下

8、EnumChildWindows函数可以枚举一个窗口的所有子窗口:

class ThreadHandler{#region P/Invoking and constants definitionconst uint WM_GETTEXT = 0x000D;[DllImport("user32.dll")]static extern IntPtr SetFocus(IntPtr hWnd);[DllImport("user32.dll")]private static extern int GetWindowThreadProcessId(IntPtr hWnd, uint lpdwProcessId = 0);delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);[DllImport("user32.dll")]static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,IntPtr lParam);[DllImport("user32.dll")]static extern bool AttachThreadInput(int idAttach, int idAttachTo, bool fAttach);[DllImport("kernel32.dll")]static extern int GetCurrentThreadId();[DllImport("user32.dll", CharSet = CharSet.Auto)]static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam);#endregionpublic readonly string ProcessName, WindowName;protected readonly int TargetThreadID, CurrentThreadID;protected readonly IntPtr TargetWindowHandle;public ThreadHandler(string processName, string windowName){CurrentThreadID = GetCurrentThreadId();ProcessName = processName;WindowName = windowName;object[] objs = GetWindowThread(processName, windowName);if (objs == null){throw new ArgumentException("Could not find the specified process/window.");}TargetThreadID = (int)objs[0];TargetWindowHandle = (IntPtr)objs[1];}public ThreadHandler(string processName){CurrentThreadID = GetCurrentThreadId();ProcessName = processName;var processes = Process.GetProcessesByName(ProcessName);if (processes.Length == 0){throw new ArgumentException("Could not find the specified process.");}var appProc = processes[0];WindowName = appProc.MainWindowTitle;TargetThreadID = GetWindowThreadProcessId(appProc.MainWindowHandle);TargetWindowHandle = appProc.MainWindowHandle;}public bool AttachThreadInput(){return AttachThreadInput(CurrentThreadID, TargetThreadID, true);}public bool DetachThreadInput(){return AttachThreadInput(CurrentThreadID, TargetThreadID, false);}public void SetFocus(){SetFocus(TargetWindowHandle);}static object[] GetWindowThread(string processName, string windowName){var processes = Process.GetProcessesByName(processName);if (processes.Length > 0){//Fill a list of handlesvar handles = new List<IntPtr>();foreach (ProcessThread thread in processes[0].Threads)EnumThreadWindows(thread.Id,(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);//Create a stringbuilder to function as storage unitStringBuilder nameBuffer = new StringBuilder(64);foreach (var hWnd in handles){//And finally compare the caption of the window with the requested namenameBuffer.Clear();SendMessage(hWnd, WM_GETTEXT, nameBuffer.Capacity, nameBuffer);if (nameBuffer.ToString() == windowName){return new object[2] { GetWindowThreadProcessId(hWnd), hWnd };}}}return null;}

不过最终是否能实现,还需要再完善或者再进行接口程序优化,仅供大家参考。

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477