📄 mainform.cs
字号:
Font m_itemDetailBoldFont = new Font(FontFamily.GenericSansSerif, 8F, FontStyle.Bold);
Brush m_itemDetailBrush = new SolidBrush(Color.Black);
int m_titleHeight = 0;
Pen m_blackPen = new Pen(Color.Black);
void itemList_DrawItem(object sender, DrawItemEventArgs e)
{
StringBuilder sb = new StringBuilder(1024);
// custom draw the item
e.Graphics.Clip = new Region(e.Bounds);
// draw a box around the item
e.Graphics.DrawRectangle(m_blackPen, e.Bounds);
// "select" the item if it should be
if (itemList.SelectedIndex == e.Index)
{
e.DrawBackground(Color.Goldenrod);
}
else
{
e.DrawBackground(Color.White);
}
if (m_currentView == CurrentView.Adapters)
{
Adapter currentAdapter = m_adapters[e.Index];
string properties1a = string.Format(
"Type: {0} IP: ",
currentAdapter.Type.ToString());
string properties1b = currentAdapter.CurrentIpAddress;
if (currentAdapter.IsWireless)
{
// TODO: Need to look at the Adapter src as this seems to change for the same adapter
// sb.Append(currentAdapter.IsWirelessZeroConfigCompatible ? "[WZC] " : "[Non-WZC] ");
if (currentAdapter.AssociatedAccessPoint != null)
{
sb.Append(string.Format("Connected to {0} ({1}db)", currentAdapter.AssociatedAccessPoint, currentAdapter.SignalStrengthInDecibels));
}
}
else
{
sb.Append("[Not wireless]");
}
string properties2 = sb.ToString();
//Draw the data
if (e.Index <= itemList.Items.Count - 1)
{
if (m_titleHeight == 0)
{
m_titleHeight = ((int)(e.Graphics.MeasureString(currentAdapter.Name, m_itemTitleFont).Height));
}
e.Graphics.DrawString(currentAdapter.Name, m_itemTitleFont, m_itemTitleBrush, 2, e.Bounds.Top);
e.Graphics.DrawString(properties1a, m_itemDetailFont, m_itemDetailBrush, 2, e.Bounds.Top + m_titleHeight);
e.Graphics.DrawString(properties1b, m_itemDetailBoldFont, m_itemDetailBrush, 2 + e.Graphics.MeasureString(properties1a, m_itemDetailFont).Width, e.Bounds.Top + m_titleHeight);
e.Graphics.DrawString(properties2, m_itemDetailFont, m_itemDetailBrush, 2, e.Bounds.Top + (2 * m_titleHeight));
}
}
else if (m_currentView == CurrentView.Networks)
{
AccessPoint ap = m_lastAPList[e.Index];
if (m_titleHeight == 0)
{
m_titleHeight = ((int)(e.Graphics.MeasureString(ap.Name, m_itemTitleFont).Height));
}
string properties1 = MACToString(ap.MacAddress);
// Begin bitmap section
int bars = 0;
if (ap.SignalStrengthInDecibels < -80)
bars = 5;
else if (ap.SignalStrengthInDecibels < -70)
bars = 4;
else if (ap.SignalStrengthInDecibels < -60)
bars = 3;
else if (ap.SignalStrengthInDecibels < -50)
bars = 2;
else
bars = 1;
string bmpName = string.Format("NetUI.Resource.{0}Bars{1}.bmp", bars, ap.Privacy != 0 ? "L" : "");
Bitmap strength = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(bmpName));
// TODO: cache these instead of calculating every time
int bmpWidth = strength.Width;
ImageAttributes transparentAttributes = new ImageAttributes();
Color transparentColor = strength.GetPixel(0, 0);
transparentAttributes.SetColorKey(transparentColor, transparentColor);
Rectangle destinationRectangle = new Rectangle(e.Bounds.Left, e.Bounds.Top, strength.Width, strength.Height);
e.Graphics.DrawImage(strength, destinationRectangle, 0, 0, strength.Width, strength.Height, GraphicsUnit.Pixel, transparentAttributes);
// End bitmap section
string properties2 = string.Format(
"{0} mode - Privacy {1}",
ap.InfrastructureMode.ToString(),
ap.Privacy != 0 ? "Enabled" : "Disabled");
if (e.Index <= itemList.Items.Count - 1)
{
e.Graphics.DrawString(string.Format("{0} ({1}dB)", ap.Name, ap.SignalStrengthInDecibels), m_itemTitleFont, m_itemTitleBrush, bmpWidth, e.Bounds.Top);
e.Graphics.DrawString(properties1, m_itemDetailFont, m_itemDetailBrush, bmpWidth, e.Bounds.Top + m_titleHeight);
e.Graphics.DrawString(properties2, m_itemDetailFont, m_itemDetailBrush, bmpWidth, e.Bounds.Top + (2 * m_titleHeight));
}
}
e.Graphics.ResetClip();
}
private string MACToString(byte[] mac)
{
StringBuilder sb = new StringBuilder(100);
for (int b = 0; b < mac.Length; b++)
{
if (b < mac.Length - 1)
{
sb.Append(string.Format("{0:X2}:", mac[b]));
}
else
{
sb.Append(string.Format("{0:X2}", mac[b]));
}
}
return sb.ToString();
}
private void RefreshNetworkList()
{
if (propertiesFrame.Visible)
{
propertiesFrame.Visible = false;
itemList.Height = this.Height - 3;
}
itemList.ContextMenu = null;
itemList.BeginUpdate();
itemList.Items.Clear();
m_lastAPList = m_selectedAdapter.NearbyAccessPoints;
foreach (AccessPoint ap in m_lastAPList)
{
itemList.Items.Add(new ListItem());
}
itemList.EndUpdate();
}
private void RefreshAdapterList()
{
itemList.ContextMenu = adapterMenu;
if (!propertiesFrame.Visible)
{
propertiesFrame.Visible = true;
itemList.Height = this.Height - propertiesFrame.Height - 3;
}
Adapter lastSelected = null;
int index = 0;
int select = -1;
if ((itemList.SelectedIndex >= 0) && (itemList.SelectedIndex < m_adapters.Count))
{
lastSelected = m_adapters[itemList.SelectedIndex];
}
else
{
macLabel.Text = gatewayLabel.Text = dhcpLabel.Text = subnetLabel.Text = "";
wirelessNetworkMenu.Enabled = false;
}
itemList.BeginUpdate();
m_adapters = Networking.GetAdapters();
itemList.Items.Clear();
foreach(Adapter adapter in m_adapters)
{
itemList.Items.Add(new ListItem());
if ((lastSelected != null) && (MACToString(lastSelected.MacAddress) == MACToString(adapter.MacAddress)))
{
select = index;
}
index++;
}
itemList.SelectedIndex = select;
itemList.EndUpdate();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -