diff --git a/WifiScan/Form1.Designer.cs b/WifiScan/Form1.Designer.cs index e1a86c6..8e81112 100644 --- a/WifiScan/Form1.Designer.cs +++ b/WifiScan/Form1.Designer.cs @@ -49,6 +49,7 @@ // tvTree // this.tvTree.Dock = System.Windows.Forms.DockStyle.Fill; + this.tvTree.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll; this.tvTree.FullRowSelect = true; this.tvTree.HideSelection = false; this.tvTree.ImageIndex = 0; @@ -57,7 +58,10 @@ this.tvTree.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.tvTree.Name = "tvTree"; this.tvTree.SelectedImageIndex = 0; - this.tvTree.Size = new System.Drawing.Size(233, 285); + this.tvTree.ShowLines = false; + this.tvTree.ShowPlusMinus = false; + this.tvTree.ShowRootLines = false; + this.tvTree.Size = new System.Drawing.Size(238, 435); this.tvTree.StateImageList = this.imageListDevices; this.tvTree.TabIndex = 0; this.tvTree.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.tvTree_DrawNode); @@ -89,8 +93,8 @@ // scMain.Panel2 // this.scMain.Panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.splitContainer1_Panel2_Paint); - this.scMain.Size = new System.Drawing.Size(825, 369); - this.scMain.SplitterDistance = 233; + this.scMain.Size = new System.Drawing.Size(1078, 519); + this.scMain.SplitterDistance = 238; this.scMain.SplitterWidth = 10; this.scMain.TabIndex = 1; // @@ -102,10 +106,10 @@ this.pwinfo.Controls.Add(this.btColor); this.pwinfo.Controls.Add(this.pbSignal); this.pwinfo.Dock = System.Windows.Forms.DockStyle.Bottom; - this.pwinfo.Location = new System.Drawing.Point(0, 285); + this.pwinfo.Location = new System.Drawing.Point(0, 435); this.pwinfo.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.pwinfo.Name = "pwinfo"; - this.pwinfo.Size = new System.Drawing.Size(233, 84); + this.pwinfo.Size = new System.Drawing.Size(238, 84); this.pwinfo.TabIndex = 2; // // lbInfo2 @@ -150,7 +154,7 @@ // btColor // this.btColor.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btColor.Location = new System.Drawing.Point(210, 56); + this.btColor.Location = new System.Drawing.Point(215, 56); this.btColor.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.btColor.Name = "btColor"; this.btColor.Size = new System.Drawing.Size(21, 21); @@ -165,7 +169,7 @@ this.pbSignal.Location = new System.Drawing.Point(8, 56); this.pbSignal.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.pbSignal.Name = "pbSignal"; - this.pbSignal.Size = new System.Drawing.Size(198, 21); + this.pbSignal.Size = new System.Drawing.Size(203, 21); this.pbSignal.TabIndex = 0; // // WifiTimer @@ -178,7 +182,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(825, 369); + this.ClientSize = new System.Drawing.Size(1078, 519); this.Controls.Add(this.scMain); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); diff --git a/WifiScan/Form1.cs b/WifiScan/Form1.cs index 41f6fae..b4440a7 100644 --- a/WifiScan/Form1.cs +++ b/WifiScan/Form1.cs @@ -16,7 +16,7 @@ public partial class Form1 : Form { // Create a Font object for the node tags. - Font tagFont = new Font("Helvetica", 8, FontStyle.Bold); + Font tagFont = new Font("Helvetica", 7, FontStyle.Bold); public Form1() { @@ -38,7 +38,9 @@ objMethodInfo.Invoke(scMain.Panel1, objArgs); objMethodInfo.Invoke(scMain.Panel2, objArgs); - //objMethodInfo.Invoke(tvTree, objArgs); + objArgs = new object[] { ControlStyles.AllPaintingInWmPaint | + ControlStyles.OptimizedDoubleBuffer, true }; + objMethodInfo.Invoke(tvTree, objArgs); } #region Tree Management @@ -61,7 +63,7 @@ node.Tag = wifi; return node; } - TreeNode nnode = nodes.Add(""); + TreeNode nnode = nodes.Add(wifi.DisplayName); nnode.Tag = wifi; return nnode; } @@ -81,6 +83,7 @@ private void PopulateTree() { + tvTree.BeginUpdate(); ArrayList nodesToDelete = new ArrayList(tvTree.Nodes);// Fill with all (discard found later) foreach (WifiDevice dev in WifiDevice.List()) { @@ -92,16 +95,9 @@ foreach (Wifi wifi in wifis) { TreeNode nwnode = GetNodeFor(wifi, node.Nodes); - if (string.IsNullOrEmpty(wifi.SSID)) - { - nwnode.Text = $"[{wifi.BSSID}]"; // Update network name (for configuration changes) - } - else - { - nwnode.Text = wifi.SSID; // Update network name (for configuration changes) - } + if (nwnode.Text != wifi.DisplayName) + nwnode.Text = wifi.DisplayName; // Update network name (for configuration changes) nwnode.ImageIndex = nwnode.SelectedImageIndex = 1; - nwnode.BackColor = wifi.Conf.Color; disabledNetNodes.Remove(nwnode); } foreach (TreeNode nwdel in disabledNetNodes) @@ -114,6 +110,7 @@ // remove old foreach (TreeNode ndel in nodesToDelete) tvTree.Nodes.Remove(ndel); + tvTree.EndUpdate(); } #endregion @@ -144,34 +141,10 @@ foreach (TreeNode n in ns) { n.StateImageIndex = w.Conf.Visible ? -1 : 3; - n.BackColor = w.Conf.Color; } scMain.Panel2.Invalidate(); } - // Returns the bounds of the specified node, including the region - // occupied by the node label and any node tag displayed. - private Rectangle NodeBounds(TreeNode node) - { - // Set the return value to the normal node bounds. - Rectangle bounds = node.Bounds; - if (node.Tag != null) - { - // Retrieve a Graphics object from the TreeView handle - // and use it to calculate the display width of the tag. - Graphics g = tvTree.CreateGraphics(); - int tagWidth = (int)g.MeasureString - (node.Tag.ToString(), tagFont).Width + 6; - - // Adjust the node bounds using the calculated value. - bounds.Offset(tagWidth / 2, 0); - bounds = Rectangle.Inflate(bounds, tagWidth / 2, 0); - g.Dispose(); - } - - return bounds; - } - private void WifiTimer_Tick(object sender, EventArgs e) { WifiApi.Snapshoot(); @@ -243,51 +216,38 @@ private void tvTree_DrawNode(object sender, DrawTreeNodeEventArgs e) { - // Draw the background and node text for a selected node. - if ((e.State & TreeNodeStates.Selected) != 0) + if (imageListDevices.Images.Count == 0) + return; + if (e.Node.Tag is Wifi w) { - // Draw the background of the selected node. The NodeBounds - // method makes the highlight rectangle large enough to - // include the text of a node tag, if one is present. - e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node)); - - // Retrieve the node font. If the node font has not been set, - // use the TreeView font. - Font nodeFont = e.Node.NodeFont; - if (nodeFont == null) nodeFont = ((System.Windows.Forms.TreeView)sender).Font; - - // Draw the node text. - e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, - Rectangle.Inflate(e.Bounds, 2, 0)); - } - - // Use the default background and node text. - else - { - e.DrawDefault = true; - } - - // If a node tag is present, draw its string representation - // to the right of the label text. - if (e.Node.Tag != null) - { - e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont, - Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top); - } - - // If the node has focus, draw the focus rectangle large, making - // it large enough to include the text of the node tag, if present. - if ((e.State & TreeNodeStates.Focused) != 0) - { - using (Pen focusPen = new Pen(Color.Black)) + //bool selected = (e.State & TreeNodeStates.Selected) != 0; + bool focused = (e.State & TreeNodeStates.Focused) != 0; + using (Brush color = new SolidBrush(w.Conf.Color)) + using (Brush bbg = new SolidBrush(focused ? SystemColors.Highlight : tvTree.BackColor)) + using (Brush bfont = new SolidBrush(focused ? SystemColors.HighlightText : tvTree.ForeColor)) { - focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; - Rectangle focusBounds = NodeBounds(e.Node); - focusBounds.Size = new Size(focusBounds.Width - 1, - focusBounds.Height - 1); - e.Graphics.DrawRectangle(focusPen, focusBounds); + Font nodeFont = e.Node.NodeFont ?? tvTree.Font; + e.Graphics.FillRectangle(bbg, e.Bounds); + int x = e.Bounds.X + 15; + int y = e.Bounds.Y; + if (e.Node.StateImageIndex >= 0) + e.Graphics.DrawImage(imageListDevices.Images[e.Node.StateImageIndex], x, y+10, 15, 15); + x += 10; + imageListDevices.Draw(e.Graphics, x, y, e.Node.ImageIndex); + x += 25; + y++; + e.Graphics.FillRectangle(color, new Rectangle(x,y,5,e.Bounds.Height-2)); + x += 5; + e.Graphics.DrawString(e.Node.Text, nodeFont, bfont, x, y); + x += 5; + y += 13; + e.Graphics.DrawString($"{w.RangeName}, Ch: {w.Channel}, Power: {w.Signal}%", tagFont, Brushes.Gray, x, y); + } } + else + e.DrawDefault = true; + } } } diff --git a/WifiScan/Wifi.cs b/WifiScan/Wifi.cs index 43423d5..2a047e3 100644 --- a/WifiScan/Wifi.cs +++ b/WifiScan/Wifi.cs @@ -12,11 +12,15 @@ { class Wifi { + public enum WifiRangle { G2_4, G5, G60 }; private Wlan.WlanBssEntry network; private string ssid; private string bssid; private DateTime lastSeen; private WifiConf conf; + private WifiRangle range; + private uint channel; + public Wifi(Wlan.WlanBssEntry network) { @@ -25,6 +29,16 @@ ssid = network.GetSSID(); conf = WifiApi.GetConf(bssid); Notify(); + if (network.chCenterFrequency > 5000000) + { + range = WifiRangle.G5; + channel = ((network.chCenterFrequency - 5150000) / 1000) / 5 + 30; + } + else + { + range = WifiRangle.G2_4; + channel = (network.chCenterFrequency - 2400000) / 5000; + } } public void Notify() @@ -42,11 +56,35 @@ this.network = network; } + public override string ToString() + { + return $"Wifi: {DisplayName}"; + } + + private string GetRangeName() + { + switch (range) + { + case WifiRangle.G2_4: + return "2.4G"; + case WifiRangle.G5: + return "5G"; + case WifiRangle.G60: + return "60G"; + } + return "?G"; + } + public string BSSID => bssid; public string SSID => ssid; + + public string DisplayName => string.IsNullOrWhiteSpace(ssid) ? $"[{bssid}]" : ssid; public bool Active => (DateTime.Now - lastSeen).Seconds < 10; public uint Signal => network.linkQuality; - public uint Channel => network.GetChannel(); + public uint Channel => channel; + public uint Frequency => network.chCenterFrequency; + public WifiRangle Range => range; + public string RangeName => GetRangeName(); public WifiConf Conf => conf; } } diff --git a/WifiScan/WifiInfo.cs b/WifiScan/WifiInfo.cs index a8bcd86..2bfaff4 100644 --- a/WifiScan/WifiInfo.cs +++ b/WifiScan/WifiInfo.cs @@ -16,6 +16,7 @@ SSID = wifi.SSID; Signal = wifi.Signal; Channel = wifi.Channel; + Range = wifi.Range; c = wifi.Conf; } @@ -23,6 +24,7 @@ public string SSID { get; } public uint Signal { get; } public uint Channel { get; } + public Wifi.WifiRangle Range { get; } public WifiConf Conf => c; } } diff --git a/WifiScan/WifiaPI.cs b/WifiScan/WifiaPI.cs index 40910ff..821f151 100644 --- a/WifiScan/WifiaPI.cs +++ b/WifiScan/WifiaPI.cs @@ -41,12 +41,6 @@ return ssid; } - public static uint GetChannel(this Wlan.WlanBssEntry wifi) - { - uint ch = (wifi.chCenterFrequency - 2400000) / 5000; - return ch - 1; - } - #endregion public static void Snapshoot() @@ -69,12 +63,12 @@ return snap[snap.Count - 1]; } - private static List GetNetworksInChannel(int ch) + private static List GetNetworksInChannel(Wifi.WifiRangle range, int ch) { List result = new List(); List scan = GetLastScan(); foreach (WifiInfo wii in scan) - if (wii.Channel == ch) + if (wii.Range == range && wii.Channel == ch) result.Add(wii); return result; } @@ -89,6 +83,15 @@ //Draw Guidelines float scale = g.DpiX / 96; float hh2 = sz.Height / 2f; + //Vertical (channels) + float chw6 = (sz.Width * 4) / 16f; + for (int i = 1; i <= 14; i++) + { + int x = (int)((sz.Width * (i + 1)) / 16f); + g.DrawLine(pLineDark2, new PointF(x, sz.Height), new PointF(x, hh2)); + g.DrawString("" + i, font, bText, new PointF(x - 10, sz.Height - (CHART_FOOT * scale))); + } + //Horizontal g.DrawLine(pLineLight, new Point(0, (int)hh2), new Point(sz.Width, (int)hh2)); for (int i = 1; i < 4; i++) { @@ -98,13 +101,10 @@ g.DrawLine(pLineDark, new PointF(0, hl2), new PointF(sz.Width, hl2)); } //Draw Channels - float chw6 = (sz.Width * 4) / 16f; for (int i = 1; i <= 14; i++) { int x = (int)((sz.Width * (i + 1)) / 16f); - g.DrawString("" + i, font, bText, new PointF(x - 10, sz.Height - (CHART_FOOT * scale))); - g.DrawLine(pLineDark2, new PointF(x, sz.Height), new PointF(x, hh2)); - List chw = GetNetworksInChannel(i); + List chw = GetNetworksInChannel(Wifi.WifiRangle.G2_4, i); foreach (WifiInfo wii in chw) { if (!wii.Conf.Visible)