Beispiel #1
0
        private static IntPtr HeartbeatHwnd;               // 心跳消息窗口句柄

        /// <summary>
        /// 工厂方法,创建sender时,必须明确需要发送到哪些窗口。工厂函数负责依据提供的窗口名查找当前系统的QQ窗口。
        /// </summary>
        /// <param name="wndNameRegex">窗口名正则串</param>
        /// <param name="form">程序主窗口</param>
        /// <returns></returns>
        public static QQSender CreateSender(string wndNameRegex, Form form)
        {
            QQSender instance = null;

            SenderApi.StartQQFindHwnd(wndNameRegex);
            IntPtr hwnd    = SenderApi.GetFindHwnd();
            string wndName = SenderApi.GetFindWndName();

            if (Win32Api.IsWindow(hwnd))
            {
                instance               = new QQSender();
                instance.mHwnd         = hwnd;
                instance.form          = form;
                instance.mWndNameRegex = wndNameRegex;
                instance.mWndName      = wndName;
                if (heartbeatTimer == null)
                {
                    heartbeatTimer          = new System.Timers.Timer(5 * 60 * 1000);
                    heartbeatTimer.Elapsed += HeartbeatTimer_Elapsed;
                    HeartbeatHwnd           = hwnd;
                    heartbeatTimer.Start();
                }
            }
            return(instance);
        }
Beispiel #2
0
        public void readConfig()
        {
            Win32Api ini        = Win32Api.getInstance().setPath(".\\config.ini");
            string   cookiePath = ini.ReadValue("robot", "cookie");
            string   windowText = ini.ReadValue("robot", "windows");
            string   tokenText  = ini.ReadValue("robot", "weibo_sender_tokens");

            interval = ini.ReadValue("robot", "interval");
            uid      = ini.ReadValue("robot", "uid");
            topCount = ini.ReadValue("robot", "top");
            try
            { ifLog = bool.Parse(ini.ReadValue("robot", "iflog")); }
            catch (Exception) { }
            try
            { ifHeader = bool.Parse(ini.ReadValue("robot", "show_user_info", "true")); }
            catch (Exception) { }
            try
            { ifFooter = bool.Parse(ini.ReadValue("robot", "show_source", "true")); }
            catch (Exception) { }
            windows     = string.IsNullOrEmpty(windowText)? new string[0] : windowText.Split(',');
            weiboTokens = string.IsNullOrEmpty(tokenText) ? new string[0] : tokenText.Split(',');
            if (File.Exists(cookiePath))
            {
                try
                {
                    cookie = File.ReadAllText(cookiePath);
                }catch (Exception e)
                {
                    cookie = "";
                }
            }
            proxy           = ini.ReadValue("robot", "proxy");
            takerKind       = ini.ReadValue("robot", "taker", "weibo");
            autoStartServer = bool.Parse(ini.ReadValue("robot", "auto_start", "false"));
            textBox1.Text   = cookie;
            listBox1.Items.Clear();
            textBox3.Text     = uid;
            textBox4.Text     = interval;
            textBox5.Text     = topCount;
            textBox7.Text     = takerKind;
            textBox8.Text     = proxy;
            checkBox1.Checked = ifHeader;
            checkBox2.Checked = ifFooter;
            for (int i = 0; i < windows.Length; i++)
            {
                listBox1.Items.Add(windows[i]);
            }
            for (int i = 0; i < weiboTokens.Length; i++)
            {
                listBox1.Items.Add(weiboTokens[i]);
            }
            handle.senders.Clear();
            handle.ifLog      = ifLog;
            handle.showHeader = ifHeader;
            handle.showFooter = ifFooter;
            listBox2.Items.Clear();
            bool qqWindowReady = windows.Length <= 0 ? true : false;

            foreach (string win in windows)
            {
                QQSender sender = QQSender.CreateSender(win, this);
                if (sender != null)
                {
                    handle.senders.AddLast(sender);
                    listBox2.Items.Add(sender.getName());
                    qqWindowReady = true;
                }
            }
            foreach (string token in weiboTokens)
            {
                WeiboSender sender = WeiboSender.CreateSender(token);
                if (sender != null)
                {
                    handle.senders.AddLast(sender);
                    listBox2.Items.Add(sender.getName());
                }
            }


            if (autoStartServer && handle.senders.Count > 0)
            {
                if (qqWindowReady)
                {
                    SynchronizationContext.Current.Post(new SendOrPostCallback(t => { button2_Click(null, null); }), null);
                    //this.Invoke((Action) delegate{ button2_Click(null, null); });
                }
                else
                {
                    Thread.Sleep(15000);
                    readConfig();
                }
            }
        }