[AHK]联动通达信其实很简单

77 篇文章 25 订阅
54 篇文章 26 订阅

大智慧浏览股票的时候  通达信跟着变动,反之也可以。

或者同花顺浏览股票的时候,通达信跟着变动  。

通达信有很多自定义消息,通过传递消息来完成某个小功能将大大简化编程,提高效率.就是win32调用.比如显示某支股票可以向通达信发送消息来实现。若无消息接口,模拟键盘输入来联动,效率低稳定性差。注意股票代码:沪市代码前加7,其它市场股票代码前面加6.

C代码

UINT UWM_STOCK = RegisterWindowMessage(_T("Stock"));

::PostMessage(HWND_BROADCAST,UWM_STOCK,7580019,0);
//就是查看 580019 的页面,
::PostMessage(HWND_BROADCAST,UWM_STOCK,6031007,0);
//是查看031007页面


Python代码

#用pywin32,去掉_T()就ok。

UWM_STOCK = RegisterWindowMessage("Stock");
PostMessage(HWND_BROADCAST,UWM_STOCK,7600027,0);


AutoHotkey代码

;这里指定0xFFFF就是HWND_BROADCAST,是对多个通达信窗口广播消息;
;如果指定具体窗口的句柄,则是向特定窗口发送消息。

active_id:=0xFFFF
UWM_STOCK := DllCall("RegisterWindowMessage", Str,"Stock")
PostMessage,UWM_STOCK,7600050,0,,ahk_id %active_id%

--- c#实现--- 一大坨

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WgServer
{
    public class TdxWHelp   //关联打开通达信K线图用的
    {
        private static Hashtable processWnd = new Hashtable();
        [StructLayout(LayoutKind.Sequential)]
        public struct ProcessEntry32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;
        }
        public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

        [DllImport("KERNEL32.DLL")]
        public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
        [DllImport("KERNEL32.DLL")]
        public static extern int CloseHandle(IntPtr handle);
        [DllImport("KERNEL32.DLL")]
        public static extern int Process32First(IntPtr handle, ref ProcessEntry32 pe);
        [DllImport("KERNEL32.DLL")]
        public static extern int Process32Next(IntPtr handle, ref ProcessEntry32 pe);

        [DllImport("user32.dll")]
        private static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

        [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

        [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(uint dwErrCode);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool GetGUIThreadInfo(IntPtr hTreadID, ref ProcessEntry32 lpgui);

        [DllImport("user32.dll")]
        public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public extern static int RegisterWindowMessage(string stock);

        public static void SendStockCode(string StockCode)
        {
            if (StockCode == null) { return; }
            if (StockCode.Length != 6) { return; }
            int SendStock;
            if (StockCode.Substring(0, 1) == "6" || StockCode.Substring(0, 2) == "11")
            {
                SendStock = int.Parse("7" + StockCode);
            }
            else if (StockCode == "999999")
            {
                SendStock = int.Parse("7" + StockCode);
            }
            else
            {
                SendStock = int.Parse("6" + StockCode);
            }

            //if (StockCode.Substring(0, 1) == "6")
            //{
            //    SendStock = int.Parse( StockCode);
            //}
            //else if (StockCode == "999999")
            //{
            //    SendStock = int.Parse(StockCode);
            //}
            //else
            //{
            //    SendStock = int.Parse(StockCode);
            //}

            IntPtr hh = GetHandleByProcessName("TdxW.exe");

            // StringBuilder s = new StringBuilder(512);
            //  int i = GetWindowText(hh, s, s.Capacity);  
            int UWM_STOCK = RegisterWindowMessage("Stock");
            // int UWM_STOCK = RegisterWindowMessage("通达信金融终端");
            PostMessage(hh, UWM_STOCK, SendStock, 0);
        }

 public static IntPtr GetHandleByProcessName(string ProcessName)
        {
            List<ProcessEntry32> list = new List<ProcessEntry32>();
            IntPtr handle = CreateToolhelp32Snapshot(0x2, 0);
            IntPtr hh = IntPtr.Zero;
            if ((int)handle > 0)
            {
                ProcessEntry32 pe32 = new ProcessEntry32();
                pe32.dwSize = (uint)Marshal.SizeOf(pe32);
                int bMore = Process32First(handle, ref pe32);
                while (bMore == 1)
                {
                    IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
                    Marshal.StructureToPtr(pe32, temp, true);
                    ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32));
                    Marshal.FreeHGlobal(temp);
                    list.Add(pe);
                    if (pe.szExeFile.ToUpper() == ProcessName.ToUpper())
                    {
                        bMore = 2;
                        hh = GetCurrentWindowHandle(pe.th32ProcessID);
                        break;
                    }
                    bMore = Process32Next(handle, ref pe32);
                }
            }
            return hh;
        }
        public static IntPtr GetCurrentWindowHandle(uint proid)
        {
            IntPtr ptrWnd = IntPtr.Zero;
            uint uiPid = proid;
            object objWnd = processWnd[uiPid];
            if (objWnd != null)
            {
                ptrWnd = (IntPtr)objWnd;
                if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄
                {
                    return ptrWnd;
                }
                else
                {
                    ptrWnd = IntPtr.Zero;
                }
            }
            bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
            // 枚举窗口返回 false 并且没有错误号时表明获取成功
            if (!bResult && Marshal.GetLastWin32Error() == 0)
            {
                objWnd = processWnd[uiPid];
                if (objWnd != null)
                {
                    ptrWnd = (IntPtr)objWnd;
                }
            }
            return ptrWnd;
        }
        private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
        {
            uint uiPid = 0;
            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref uiPid);
                if (uiPid == lParam)                                              // 找到进程对应的主窗口句柄
                {
                    if (!processWnd.Contains(uiPid))
                    {
                        processWnd.Add(uiPid, hwnd);                    // 把句柄缓存起来
                        SetLastError(0);                                             // 设置无错误
                    }
                    return false;                                                     // 返回 false 以终止枚举窗口
                }
            }
            return true;
        }

    }
}

  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
目标用户群:习惯用通达信看盘,或习惯用大智慧看盘,但又喜欢用同花顺单独交易程序(xiadan.exe)来交易。 配置方式:打开压缩包中的配置文件,配置 "下单程序"的路径属性,即将path后面的路径改成你本机xiadan.exe的真实路径。(务必保持配置文件和本脚本名字一致) 使用步骤: 1、配置好上面说的路径 2、启动看盘软件,如通达信 3、启动本工具 4、本工具会根据上面所配置路径启动xiadan.exe 5、手动登录xiadan.exe 6、程序会发送F1、F2检查下单功能 7、无误后会在屏幕上方看到下单助手工具条。(点住左侧的矩形可以移动工具条的位置) 8、在通达信(大智慧)上浏览某股票,会同步更新到工具条上。 9、点击买或卖单选框可以切换到买模式或卖模式。(展示相应按钮,屏蔽无关按钮,避免操作出错。核买按钮就是涨停买,核卖就是跌停卖。) 10、选择仓位或者输入手数 11、选择价格或输入价格 12、点击下单按钮 13、本程序会将:股票代码、下单数量、下单价格、自动填写到 xiadan.exe 中。(并会核算下单数量是否符合可下单额度) 14、如选择了闪电复选框,那么在上面第13步下单确认窗口会自动被点击确认。(初期测试不建议选择闪电,另请务必在模拟账户上进行测试,本脚本只做为学习交流,不建议实盘应用,稳定性尚需打磨,欢迎交流探讨。)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值