一、窗体编辑和P图
1.插入图片
插入PictureBox控件
Image属性选择图片
SizeMode->StretchImage
2.窗体属性
ShowInTaskbar->False
TopMost->True
Cursor->SizeAll(选做)
FormBoarderStyle->None
3.添加显示流量的数字
插入Label控件
Text->”0.00B”
Font->加粗、小四
二、添加界面功能
1.鼠标拖拽
选中窗体Form1,点击属性窗口小闪电图标,找到MouseDown事件,双击
在生成的函数中,添加以下代码:
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
在private void Form1_MouseDown(object sender, MouseEventArgs e)上方,添加以下代码
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
public static float send = 0;
public static float recieve = 0;
public static float netRecv;
public static float netSend;
为了使拖拽更灵敏,可以给两张图片、0.00B字体,添加同样的MouseDown事件,并添加代码
2.窗口初始显示位置
双击窗体Form1,在生成的private void Form1_Load(object sender, EventArgs e)函数中添加
Control.CheckForIllegalCrossThreadCalls = false;
timer1.Start();
int x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - 280;
int y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - 125;
this.SetDesktopLocation(x, y);
3.窗体圆角
选中窗体Form1,点击属性窗口小闪电图标,找到Resize事件,双击
在生成的函数中,添加以下代码
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect, 40);
this.Region = new Region(FormPath);
在private void Form1_Resize(object sender, EventArgs e)函数上方,添加如下代码
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath(); path.AddArc(arcRect, 180, 90);//左上角
arcRect.X = rect.Right - diameter;//右上角
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter;// 右下角
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left;// 左下角
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
三、添加流量监控功能
1.添加定时器控件
添加Timer,修改Timer的属性,达到每1S读取一次流量的效果
Interval->1000
2.添加定时时间
单击选中Timer,点击属性窗口小闪电图标,找到Tick事件,双击
在生成的函数里面,添加如下代码
netRecv = NetMonitorCore.GetNetRecv();
netSend = NetMonitorCore.GetNetSend();
send = netSend / 1024;
recieve = netRecv / 1024;
string netRecvText = "";
string netSendText = "";
if (netRecv < 1024)
{
netRecvText = netRecv.ToString("0.00") + "B";
}
else if (netRecv >= 1024 && netRecv < 1024 * 1024)
{
netRecvText = (netRecv / 1024).ToString("0.00") + "KB";
}
else if (netRecv >= 1024 * 1024)
{
netRecvText = (netRecv / (1024 * 1024)).ToString("0.00") + "MB";
}
if (netSend < 1024)
{
netSendText = netSend.ToString("0.00") + "B";
}
else if (netSend >= 1024 && netSend < 1024 * 1024)
{
netSendText = (netSend / 1024).ToString("0.00") + "KB";
}
else if (netSend >= 1024 * 1024)
{
netSendText = (netSend / (1024 * 1024)).ToString("0.00") + "MB";
}
label1.Text = netSendText;
label2.Text = netRecvText;
3.添加NetMonitorCore.cs文件
首先将NetMonitorCore.cs文件复制粘贴到工程目录中,接着在解决方案资源管理器项目上,右键选择添加->现有项
最后选择NetMonitorCore.cs文件完成添加
四、添加通道选择及退出功能
1.通道选择功能
(1)添加ContextMenuStrip,添加通道0-6和退出选项
(2)修改窗体Form1、两张图片、0.00B的属性,将ContextMenuStrip中选择contextMenuStrip1
(3)双击通道0,在生成的函数上方,添加如下函数代码
private void NetRestart(int num)
{
NetMonitorCore.performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
try
{
NetMonitorCore.instance = NetMonitorCore.performanceCounterCategory.GetInstanceNames()[num];
}
catch
{
MessageBox.Show("此通道不通哦~");
}
NetMonitorCore.interfaceLength = NetMonitorCore.instance.Length;
NetMonitorCore.performanceCounterRecv = new PerformanceCounter("Network Interface", "Bytes Received/sec", NetMonitorCore.instance);
NetMonitorCore.performanceCounterSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", NetMonitorCore.instance);
}
(4)在通道0生成的函数private void 通道0ToolStripMenuItem_Click(object sender, EventArgs e)中
添加如下代码
NetRestart(0);
同样,在双击“通道1”生成的函数中,添加NetRestart(1); 依次类推
2.退出功能
双击“退出”,在生成的函数中,添加如下代码
SystemSounds.Beep.Play();
DialogResult r = MessageBox.Show("主银,辣么萌的流量悬浮窗您要狠心退出么/(ㄒoㄒ)/~~", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
Application.Exit();
}
暂无相关推荐.