Example #1
0
        public ControlPanelForm()
        {
            if (Instance != null)
            {
                Instance.WindowState = FormWindowState.Normal;
                Instance.Activate();
                return;
            }
            Instance = this;
            Global.BusyForms.Enqueue(busyForm);
            InitializeComponent();
            int interfaceHeight = 260;
            int interfaceWidth  = 750;
            // resize
            Size      minimumSize     = new Size(interfaceWidth + 26, (interfaceHeight + 6) * Global.NetworkInterfaces.Count + 78);
            Rectangle screenRectangle = RectangleToScreen(ClientRectangle);
            int       titleBarHeight  = screenRectangle.Top - Top;
            int       borderThickness = screenRectangle.Left - Left;
            Rectangle workingArea     = Screen.GetWorkingArea(this);
            Size      clientSize      = new Size();

            if (minimumSize.Width > workingArea.Width - 2 * borderThickness)
            {
                clientSize.Width = workingArea.Width - 2 * borderThickness;
            }
            else
            {
                clientSize.Width = minimumSize.Width;
            }
            if (minimumSize.Height > workingArea.Height - titleBarHeight - borderThickness)
            {
                clientSize.Height = workingArea.Height - titleBarHeight - borderThickness;
            }
            else
            {
                clientSize.Height = minimumSize.Height;
            }
            AutoScrollMinSize = new System.Drawing.Size(minimumSize.Width, minimumSize.Height);
            ClientSize        = new Size(clientSize.Width, clientSize.Height);

            // populate
            int i = 0;

            foreach (NetworkInterface nic in Global.NetworkInterfaces.Values)
            {
                GroupBox groupBox = new GroupBox();
                groupBox.Name        = "interface" + nic.Guid;
                groupBox.Tag         = nic.Guid;
                groupBox.Width       = interfaceWidth;
                groupBox.Height      = interfaceHeight;
                groupBox.Location    = new Point(13, 68 + interfaceHeight * i);
                groupBox.Anchor      = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right);
                groupBox.MinimumSize = new System.Drawing.Size(interfaceWidth, interfaceHeight);
                groupBox.Controls.Add(CreateLabel(nic.Name, 10, 15, 340, true, "name"));
                groupBox.Controls["name"].Anchor = AnchorStyles.Left;
                if (!Global.Config.Gadget.HiddenInterfaces.Contains(nic.Guid))
                {
                    groupBox.Controls["name"].Font = new System.Drawing.Font(DefaultFont, FontStyle.Bold);
                }
                if (Global.InternetInterface != Guid.Empty)
                {
                    if (nic.Guid == Global.InternetInterface)
                    {
                        groupBox.Controls["name"].ForeColor = Color.Blue;
                        toolTip1.SetToolTip(groupBox.Controls["name"], "Internet (remote network connections) goes through this interface");
                    }
                }
                groupBox.Controls.Add(CreateListView("ipProperties"));
                Button button = new Button();
                button.Name     = "interfaceTools";
                button.Text     = "Interface tools";
                button.Location = new Point(360, 15);
                button.Anchor   = AnchorStyles.Right;
                button.Width    = 100;
                button.Height   = 20;
                ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
                ToolStripItem    toolStripItem;
                toolStripItem        = contextMenuStrip.Items.Add("Configure");
                toolStripItem.Click += new EventHandler((s, e) => { new ConfigureInterface.ConfigureInterfaceForm(nic.Guid); });
                toolStripItem        = contextMenuStrip.Items.Add("Make primary");
                toolStripItem.Click += new EventHandler((s, e) => {
                    DialogResult result = MessageBox.Show(
                        "This will configure all network interfaces so that \"" + nic.Name + "\" has the smallest metrics values of all, which will cause remote connectios to go through this interface when there are multiple NICs with a default gateway.\n\nDo you want to continue ?",
                        "Make an interface primary",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        MakeInterfacePrimary(nic.Guid);
                    }
                });
                toolStripItem.MouseHover += new EventHandler((s, e) =>
                {
                    new BalloonTip(
                        "Make an interface primary",
                        "This will configure all network interfaces so that \"" + nic.Name + "\" has the smallest metrics values of all, which will cause remote connectios to go through this interface when there are multiple NICs with a default gateway."
                        , button, BalloonTip.ICON.INFO, 100000, false, false,
                        (short)(button.PointToScreen(Point.Empty).X + button.Width / 2),
                        (short)(button.PointToScreen(Point.Empty).Y + button.Height * 2.5));
                });
                toolStripItem.MouseLeave += new EventHandler((s, e) => { BalloonTip.CloseAll(); });

                // TODO: add speed, lattency test and network map
                //toolStripItem = contextMenuStrip.Items.Add("Test performance");
                //toolStripItem.Click += new EventHandler((s,e) => new InterfacePerformance.InterfacePerformanceForm(nic));
                //contextMenuStrip.Items.Add("Test lattency").Enabled = false;
                button.Click += new EventHandler((s, e) => { contextMenuStrip.Show((Control)s, 0, ((Control)s).Height); });
                groupBox.Controls.Add(button);
                groupBox.Controls.Add(CreateLabel("Adapter GUID:", 360, 40, 120));
                groupBox.Controls.Add(CreateLabel("Description:", 360, 55, 120));
                groupBox.Controls.Add(CreateLabel("Type:", 360, 70, 120));
                groupBox.Controls.Add(CreateLabel("Interface Index:", 360, 85, 120));
                groupBox.Controls.Add(CreateLabel("MAC Address", 360, 100, 120));
                groupBox.Controls.Add(CreateLabel("Interface Metric:", 360, 115, 120));
                groupBox.Controls.Add(CreateLabel("Lowest IPv4 metrics", 360, 145, 120, false, "lowestMetrics"));
                groupBox.Controls["lowestMetrics"].Font = new System.Drawing.Font(DefaultFont, FontStyle.Underline);
                groupBox.Controls.Add(CreateLabel("Gateway Metric:", 360, 160, 120));
                groupBox.Controls.Add(CreateLabel("Route Metric:", 360, 175, 120));
                groupBox.Controls.Add(CreateLabel("Public IPv4:", 360, 220, 120));
                groupBox.Controls.Add(CreateLabel("Public IPv6:", 360, 235, 120));
                groupBox.Controls.Add(CreateLabel(nic.Guid.ToString().ToUpper(), 480, 40, 265, true));
                groupBox.Controls.Add(CreateLabel(nic.Description, 480, 55, 265, true));
                groupBox.Controls.Add(CreateLabel(nic.Type.GetDescription(), 480, 70, 265, true));
                groupBox.Controls.Add(CreateLabel(nic.Index.ToString(), 480, 85, 120, true));
                groupBox.Controls.Add(CreateLabel(nic.Mac, 480, 100, 120, true));
                groupBox.Controls.Add(CreateLabel(nic.InterfaceMetric.ToString(), 480, 115, 120, true));
                groupBox.Controls.Add(CreateLabel(nic.IPv4Gateway.Count != 0 ? nic.IPv4Gateway.Min(x => x.GatewayMetric).ToString() : "", 480, 160, 120, true));
                groupBox.Controls.Add(CreateLabel(nic.IPv4Gateway.Count != 0 ? nic.IPv4Gateway.Min(x => x.GatewayMetric + nic.InterfaceMetric).ToString() : "", 480, 175, 120, true));
                groupBox.Controls.Add(CreateLabel(nic.PublicIPv4, 480, 220, 265, true, "publicIPv4"));
                groupBox.Controls.Add(CreateLabel(nic.PublicIPv6, 480, 235, 265, true, "publicIPv6"));
                groupBox.Controls.Add(CreateLabel("Download:", 600, 85, 65, false, "", Color.FromArgb(0x9b, 0x87, 0x0c)));
                groupBox.Controls.Add(CreateLabel("0 B/s", 600, 100, 65, false, "downByte", Color.FromArgb(0x9b, 0x87, 0x0c)));
                groupBox.Controls.Add(CreateLabel("0 b/s", 600, 115, 65, false, "downBit", Color.FromArgb(0x9b, 0x87, 0x0c)));
                groupBox.Controls.Add(CreateLabel(Unit.AutoScale(nic.IPv4BytesReceived, "B"), 600, 205, 65, false, "totalDown", Color.FromArgb(0x9b, 0x87, 0x0c)));
                groupBox.Controls.Add(CreateLabel("Upload:", 665, 85, 65, false, "", Color.Green, ContentAlignment.MiddleRight));
                groupBox.Controls.Add(CreateLabel("0 B/s", 665, 100, 65, false, "upByte", Color.Green, ContentAlignment.MiddleRight));
                groupBox.Controls.Add(CreateLabel("0 b/s", 665, 115, 65, false, "upBit", Color.Green, ContentAlignment.MiddleRight));
                groupBox.Controls.Add(CreateLabel(Unit.AutoScale(nic.IPv4BytesSent, "B"), 665, 205, 65, false, "totalUp", Color.Green, ContentAlignment.MiddleRight));
                groupBox.Controls.Add(CreateLabel("Total:", 650, 190, 65));
                groupBox.Controls.Add(CreateGraph("graph", 600, 145));
                // interface event subscriptions
                EventHandler <TextEventArgs> handler = new EventHandler <TextEventArgs>((s, e) =>
                {
                    try
                    {
                        Invoke(new Action(() =>
                        {
                            if (nic.Guid == ((WinLib.Network.NetworkInterface)s).Guid)
                            {
                                groupBox.Controls["publicIPv4"].Text = e.Text;
                            }
                        }));
                    }
                    catch { }
                });
                nic.PublicIPv4Changed += handler;
                publicIPv4Subscriptions.Add(nic, handler);

                handler = new EventHandler <TextEventArgs>((s, e) =>
                {
                    try
                    {
                        Invoke(new Action(() =>
                        {
                            if (nic.Guid == ((WinLib.Network.NetworkInterface)s).Guid)
                            {
                                groupBox.Controls["publicIPv6"].Text = e.Text;
                            }
                        }));
                    }
                    catch { }
                });
                nic.PublicIPv6Changed += handler;
                publicIPv6Subscriptions.Add(nic, handler);

                handler = new EventHandler <TextEventArgs>((s, e) =>
                {
                    try
                    {
                        Invoke(new Action(() =>
                        {
                            if (nic.IPv4Address.Any(a => a.Address == e.Text))
                            {
                                groupBox.Controls["name"].ForeColor = Color.Aqua;
                                toolTip1.SetToolTip(groupBox.Controls["name"], "Internet (remote network connections) goes through this interface");
                                Global.InternetInterface = nic.Guid;
                            }
                            else
                            {
                                groupBox.Controls["name"].ForeColor = Color.Black;
                                toolTip1.SetToolTip(groupBox.Controls["name"], "");
                            }
                        }));
                    }
                    catch { }
                });
                WinLib.Network.NetworkInterface.InternetInterfaceChanged += handler;
                internetInterfaceSubscriptions.Add(handler);

                Controls.Add(groupBox);
                i++;
            }
            ClientSize = new Size(ClientSize.Width + 70, ClientSize.Height);
            Location   = new Point(workingArea.Left + workingArea.Width / 2 - Width / 2, workingArea.Top + workingArea.Height / 2 - Height / 2);
            Show();
        }