标签:
原理很简单,就是检测迅雷登陆对话框,然后发送“确定”消息,再被挤下线后再次 登陆。方法不是很科学但很有效,希望园友提供更好的方法。
直接上代码。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Runtime.InteropServices; 10 using System.Threading; 11 12 namespace WatchThunderService 13 { 14 public partial class MainForm : Form 15 { 16 Thread t1; 17 Thread t2; 18 public MainForm() 19 { 20 InitializeComponent(); 21 btnStart_Click(btnStart, new EventArgs()); 22 } 23 static uint count1, count2; 24 25 private void button1_Click(object sender, EventArgs e) 26 { 27 IntPtr thunder = API32.FindWindow("XLUEFrameHostWnd", "迅雷极速版"); 28 IntPtr handle1 = API32.FindWindow("XLUEModalHostWnd", "MessageBox"); 29 IntPtr handle2 = API32.FindWindow("XLUEModalHostWnd", "迅雷7登录"); 30 IntPtr handle12 = API32.FindWindowEx(thunder, IntPtr.Zero, "XLUEModalHostWnd", "MessageBox"); 31 //API32.SetForegroundWindow(thunder); 32 //SendMessage(handle1, 0, 0, 13); 33 API32.PostMessage(handle2, 0x0104, (13), 0); 34 API32.PostMessage(handle2, 0x0105, (13), 0); 35 //PostMessage(handle1, WM_KEYDOWN, VK_RETURN, 0); 36 //PostMessage(handle1, WM_KEYUP, VK_RETURN, 0); 37 38 39 //SendMessage(hwnd, 0x0104, 0x00000012, 0x20380001);//(0x00000012 == VK_MENU(ALT键)) 40 //SendMessage(hwnd, 0x0104, 0x00000046, 0x20210001);//这一行可以省略(0x00000046 == ‘F‘) 41 42 //SendMessage(hwnd, 0x0106, 0x00000066, 0x20210001);//发送一个char ‘f‘ 43 //SendMessage(hwnd, 0x0105, 0x00000046, 0xE0210001);//这一行可以省略(0x00000046 == ‘F‘) 44 //SendMessage(hwnd, 0x0105, 0x00000012, 0xC0380001);//(0x00000012 == VK_MENU(ALT键)) 45 //过程是这样的: 46 47 //首先 发送了一个 ALT按下 //(WM_SYSKEYDOWN == 0x0104) 48 49 //然后 发送了一个 ‘F‘键 //这里省略可以实现同样功能 系统兼容性问题 建议发送这个键 50 51 //然后 发送了一个 字符‘f‘ //(WM_SYSCHAR == 0x0106) 52 53 //然后 抬起按键 ‘F‘键 //这里省略可以实现同样功能 系统兼容性问题 建议发送这个键 54 55 //最后 抬起按键 ALT键 //(WM_SYSKEYUP == 0x0105) 56 } 57 public void WatchMessageBox() 58 { 59 while (true) 60 { 61 IntPtr thunder = API32.FindWindow("XLUEFrameHostWnd", "迅雷极速版"); 62 IntPtr handle1 = API32.FindWindow("XLUEModalHostWnd", "MessageBox"); 63 if (thunder != IntPtr.Zero && handle1 != IntPtr.Zero) 64 { 65 API32.PostMessage(handle1, 0x0104, (13), 0); 66 API32.PostMessage(handle1, 0x0105, (13), 0); 67 UpdateText((++count1).ToString() + "==>【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 被踢下线"); 68 } 69 Thread.Sleep(1000); 70 } 71 } 72 73 74 public void WatchThunderLogion() 75 { 76 while (true) 77 { 78 IntPtr thunder = API32.FindWindow("XLUEFrameHostWnd", "迅雷极速版"); 79 IntPtr handle1 = API32.FindWindow("XLUEModalHostWnd", "迅雷7登录"); 80 if (thunder != IntPtr.Zero && handle1 != IntPtr.Zero) 81 { 82 API32.PostMessage(handle1, 0x0104, (13), 0); 83 API32.PostMessage(handle1, 0x0105, (13), 0); 84 UpdateText((++count2).ToString() + "==>【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 重新登陆"); 85 } 86 Thread.Sleep(1000); 87 } 88 } 89 private void UpdateText(string str) 90 { 91 if (InvokeRequired) 92 { 93 BeginInvoke(new Action<string>(UpdateText), str); 94 return; 95 } 96 textBox1.Text = str + "\r\n" + textBox1.Text; 97 } 98 99 private void btnStart_Click(object sender, EventArgs e) 100 { 101 if (btnStart.Text == "开始监视") 102 { 103 t1 = new Thread(new ThreadStart(WatchMessageBox)); 104 t1.IsBackground = true; 105 t2 = new Thread(new ThreadStart(WatchThunderLogion)); 106 t2.IsBackground = true; 107 t1.Start(); 108 t2.Start(); 109 btnStart.Text = "停止监视"; 110 UpdateText("【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 开始监视"); 111 } 112 else 113 { 114 btnStart.Text = "开始监视"; 115 StopWatch(); 116 UpdateText("【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 停止监视"); 117 } 118 } 119 120 private void StopWatch() 121 { 122 if (t1 != null && t1.IsAlive) 123 { 124 t1.Abort(); 125 t1 = null; 126 } 127 if (t2 != null && t2.IsAlive) 128 { 129 t2.Abort(); 130 t2 = null; 131 } 132 } 133 protected override void OnClosing(CancelEventArgs e) 134 { 135 base.OnClosing(e); 136 137 e.Cancel = (MessageBox.Show(this,"你确定要关闭本软件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No); 138 if (!e.Cancel) 139 StopWatch(); 140 } 141 142 private void btnHide_Click(object sender, EventArgs e) 143 { 144 this.Hide(); 145 this.ShowInTaskbar = false; 146 this.WindowState = FormWindowState.Minimized; 147 } 148 149 private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) 150 { 151 if (WindowState == FormWindowState.Minimized) 152 { 153 this.ShowInTaskbar = true; 154 this.WindowState = FormWindowState.Normal; 155 this.Show(); 156 API32.SetForegroundWindow(this.Handle); 157 } 158 else 159 { 160 this.Hide(); 161 this.ShowInTaskbar = false; 162 this.WindowState = FormWindowState.Minimized; 163 } 164 } 165 166 } 167 public static class API32 168 { 169 [DllImport("user32.dll", SetLastError = true)] 170 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 171 [DllImport("User32.dll", EntryPoint = "SendMessage")] 172 public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); 173 174 [DllImport("user32.dll", EntryPoint = "SendMessage")] 175 public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 176 177 [DllImport("user32.dll")] 178 public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 179 180 [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] 181 public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam); 182 [DllImport("USER32.DLL")]//激活窗体 183 public static extern bool SetForegroundWindow(IntPtr hWnd); 184 185 /// <summary> 186 /// 该函数设置由不同线程产生的窗口的显示状态。 187 /// </summary> 188 /// <param name="hWnd">窗口句柄</param> 189 /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。</param> 190 /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns> 191 [DllImport("User32.dll")] 192 public static extern bool ShowWindow(IntPtr hWnd, int cmdShow); 193 } 194 }
标签:
原文地址:http://www.cnblogs.com/milman/p/5177078.html