当前位置:  首页>> 技术小册>> C#学习笔记

一、窗体编辑和P图
1.插入图片
插入PictureBox控件
Image属性选择图片
SizeMode->StretchImage
2.窗体属性
ShowInTaskbar->False
TopMost->True
Cursor->SizeAll(选做)
FormBoarderStyle->None
3.添加显示流量的数字
插入Label控件
Text->”0.00B”
Font->加粗、小四
二、添加界面功能
1.鼠标拖拽
选中窗体Form1,点击属性窗口小闪电图标,找到MouseDown事件,双击
在生成的函数中,添加以下代码:

  1. ReleaseCapture();
  2. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

在private void Form1_MouseDown(object sender, MouseEventArgs e)上方,添加以下代码

  1. [DllImport("user32.dll")]
  2. public static extern bool ReleaseCapture();
  3. [DllImport("user32.dll")]
  4. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  5. public const int WM_SYSCOMMAND = 0x0112;
  6. public const int SC_MOVE = 0xF010;
  7. public const int HTCAPTION = 0x0002;
  8. public static float send = 0;
  9. public static float recieve = 0;
  10. public static float netRecv;
  11. public static float netSend;

为了使拖拽更灵敏,可以给两张图片、0.00B字体,添加同样的MouseDown事件,并添加代码
2.窗口初始显示位置
双击窗体Form1,在生成的private void Form1_Load(object sender, EventArgs e)函数中添加

  1. Control.CheckForIllegalCrossThreadCalls = false;
  2. timer1.Start();
  3. int x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - 280;
  4. int y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - 125;
  5. this.SetDesktopLocation(x, y);

3.窗体圆角
选中窗体Form1,点击属性窗口小闪电图标,找到Resize事件,双击
在生成的函数中,添加以下代码

  1. System.Drawing.Drawing2D.GraphicsPath FormPath;
  2. FormPath = new System.Drawing.Drawing2D.GraphicsPath();
  3. Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
  4. FormPath = GetRoundedRectPath(rect, 40);
  5. this.Region = new Region(FormPath);
  6. private void Form1_Resize(object sender, EventArgs e)函数上方,添加如下代码
  7. private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
  8. {
  9. int diameter = radius;
  10. Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
  11. GraphicsPath path = new GraphicsPath(); path.AddArc(arcRect, 180, 90);//左上角
  12. arcRect.X = rect.Right - diameter;//右上角
  13. path.AddArc(arcRect, 270, 90);
  14. arcRect.Y = rect.Bottom - diameter;// 右下角
  15. path.AddArc(arcRect, 0, 90);
  16. arcRect.X = rect.Left;// 左下角
  17. path.AddArc(arcRect, 90, 90);
  18. path.CloseFigure();
  19. return path;
  20. }

三、添加流量监控功能
1.添加定时器控件
添加Timer,修改Timer的属性,达到每1S读取一次流量的效果
Interval->1000
2.添加定时时间
单击选中Timer,点击属性窗口小闪电图标,找到Tick事件,双击
在生成的函数里面,添加如下代码

  1. netRecv = NetMonitorCore.GetNetRecv();
  2. netSend = NetMonitorCore.GetNetSend();
  3. send = netSend / 1024;
  4. recieve = netRecv / 1024;
  5. string netRecvText = "";
  6. string netSendText = "";
  7. if (netRecv < 1024)
  8. {
  9. netRecvText = netRecv.ToString("0.00") + "B";
  10. }
  11. else if (netRecv >= 1024 && netRecv < 1024 * 1024)
  12. {
  13. netRecvText = (netRecv / 1024).ToString("0.00") + "KB";
  14. }
  15. else if (netRecv >= 1024 * 1024)
  16. {
  17. netRecvText = (netRecv / (1024 * 1024)).ToString("0.00") + "MB";
  18. }
  19. if (netSend < 1024)
  20. {
  21. netSendText = netSend.ToString("0.00") + "B";
  22. }
  23. else if (netSend >= 1024 && netSend < 1024 * 1024)
  24. {
  25. netSendText = (netSend / 1024).ToString("0.00") + "KB";
  26. }
  27. else if (netSend >= 1024 * 1024)
  28. {
  29. netSendText = (netSend / (1024 * 1024)).ToString("0.00") + "MB";
  30. }
  31. label1.Text = netSendText;
  32. label2.Text = netRecvText;

3.添加NetMonitorCore.cs文件
首先将NetMonitorCore.cs文件复制粘贴到工程目录中,接着在解决方案资源管理器项目上,右键选择添加->现有项
最后选择NetMonitorCore.cs文件完成添加

四、添加通道选择及退出功能
1.通道选择功能
(1)添加ContextMenuStrip,添加通道0-6和退出选项
(2)修改窗体Form1、两张图片、0.00B的属性,将ContextMenuStrip中选择contextMenuStrip1
(3)双击通道0,在生成的函数上方,添加如下函数代码

  1. private void NetRestart(int num)
  2. {
  3. NetMonitorCore.performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
  4. try
  5. {
  6. NetMonitorCore.instance = NetMonitorCore.performanceCounterCategory.GetInstanceNames()[num];
  7. }
  8. catch
  9. {
  10. MessageBox.Show("此通道不通哦~");
  11. }
  12. NetMonitorCore.interfaceLength = NetMonitorCore.instance.Length;
  13. NetMonitorCore.performanceCounterRecv = new PerformanceCounter("Network Interface", "Bytes Received/sec", NetMonitorCore.instance);
  14. NetMonitorCore.performanceCounterSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", NetMonitorCore.instance);
  15. }

(4)在通道0生成的函数private void 通道0ToolStripMenuItem_Click(object sender, EventArgs e)中
添加如下代码
NetRestart(0);
同样,在双击“通道1”生成的函数中,添加NetRestart(1); 依次类推
2.退出功能
双击“退出”,在生成的函数中,添加如下代码

  1. SystemSounds.Beep.Play();
  2. DialogResult r = MessageBox.Show("主银,辣么萌的流量悬浮窗您要狠心退出么/(ㄒoㄒ)/~~", "提示", MessageBoxButtons.OKCancel);
  3. if (r == DialogResult.OK)
  4. {
  5. Application.Exit();
  6. }

该分类下的相关小册推荐:

暂无相关推荐.