由于在学校是校园网,所以访问外网需要代理,一般而言是直接使用搜狗浏览器自带的代理加速,但是搜狗的浏览器做得实在不尽人意,再加之也需要访问国外网,因此就需要一款实时切换的代理。
由于注册表修改代理的办法具有很大的局限性,包含LAN代理和宽带拨号代理,而且需要准确定位到是哪个拨号现在正在进行拨号,还需要费劲一番心思
经过我到国外网站寻找的资料加上自己的ipconfig获取关键字字段的新创意,最终完成了这个一键切换。
首先是实现UI类,在屏幕中显示GUI绘制的椭圆形透明框
class InstanceGUI { [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool RedrawWindow(IntPtr hwnd, Rectangle rcUpdate, IntPtr hrgnUpdate, int flags); [DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags); [DllImport("user32.dll")] public static extern bool InvalidateRect(IntPtr hwnd, IntPtr lpRect, bool bErase); static System.Timers.Timer t = new System.Timers.Timer(3000); public static void Instance(string text) { Screen screen = Screen.PrimaryScreen; IntPtr desk = GetDesktopWindow(); IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403); RedrawWindow(desk, new Rectangle(), IntPtr.Zero, 0x85); Graphics g = Graphics.FromHdc(deskDC); string tiptext = text; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; System.Drawing.Color Mycolor = System.Drawing.Color.FromArgb(180, Color.White);//说明:1-(128/255)=1-0.5=0.5 透明度为0.5,即50% System.Drawing.SolidBrush sb1 = new System.Drawing.SolidBrush(Mycolor); FillRoundRectangle(g, sb1, new Rectangle(screen.Bounds.Width / 2 - (tiptext.Length / 2) * 20, screen.Bounds.Height - 100, tiptext.Length * 27, 35),7); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;//开启抗锯齿 g.DrawString(tiptext, new Font("黑体", 19, FontStyle.Regular), Brushes.OrangeRed, new PointF(screen.Bounds.Width / 2 - (tiptext.Length / 2) * 20, screen.Bounds.Height - 95)); // DrawRoundRectangle(g, Pens.Yellow, new Rectangle(screen.Bounds.Width / 2 - (tiptext.Length / 2) * 20, screen.Bounds.Height - 100, tiptext.Length * 20, 40), 10); g.Dispose(); //到达时间的时候执行事件; t.Elapsed += new System.Timers.ElapsedEventHandler(DoRefresh); t.AutoReset = false;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } public static void DoRefresh(object sender, ElapsedEventArgs e) { t.Enabled = false ;//是否执行System.Timers.Timer.Elapsed事件; InvalidateRect(IntPtr.Zero, IntPtr.Zero, true); } public static void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius) //或者外圈 { using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) { g.DrawPath(pen, path); } } public static void FillRoundRectangle(Graphics g, Brush brush, Rectangle rect, int cornerRadius) //填充内圈 { using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius)) { g.FillPath(brush, path); } } internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius) { GraphicsPath roundedRect = new GraphicsPath(); roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90); roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2); roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom); roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2); roundedRect.CloseFigure(); return roundedRect; } }
主要的代理设置代码:
public void ProxySet(string IPandPort) { System.Int32 dwFlag = new int(); if (!InternetGetConnectedState(ref dwFlag, 0)) { //TODO LIST 如果未联网 MessageBox.Show("你当前尚未连接网络!"); } else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0) //LAN上网代理设置 { //打开注册表键 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); //设置代理可用 rk.SetValue("ProxyEnable", 1); //设置代理IP和端口 rk.SetValue("ProxyServer", IPandPort); rk.Close(); Reflush(); } else if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0) //拨号上网代理设置 { Regex emailregex = new Regex("(?<=PPP .*? ).*?(?=:)"); String s = GetIPConfigReturns(); Match m = emailregex.Match(s); PPPConName = m.Value; if (PPPConName.Equals("")) { MessageBox.Show("找不到宽带连接名!"); return; } //出错 int i = (IPandPort).Length; byte[] key = new byte[50]; char[] source = (IPandPort).ToCharArray(); key[0] = 60; key[4] = 3; key[8] = 3; key[12] = (byte)i; for (int ii = 0; ii < source.Length; ii++) { key[16 + ii] = ChangeTobyte(source[ii]); } string sDirectX = ""; for (int k = 0; k < key.Length; k++) { if (key[k] != 0) { sDirectX += key[k] + " "; } } //MessageBox.Show(sDirectX); RegistryKey pregkey; pregkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", true); if (pregkey == null) { Console.WriteLine("键值不存在"); } else { pregkey.SetValue(PPPConName, key, RegistryValueKind.Binary); //激活代理设置 Reflush(); } pregkey.Close(); }
有了这个之后 上网巨方便~所谓自己动手 丰衣足食。
两个代理
一个搜狗是调出早期搜狗浏览器的代理核心,免费使用搜狐的强大免费代理服务器
另一个是Goagent,同样是强大的免费谷歌服务器,速度暴快~看Youtube比优酷还快