1. 程式人生 > >自制NetMonitor小插件

自制NetMonitor小插件

main 解壓密碼 truct etl 使用 ini log param 網卡

下載鏈接:http://pan.baidu.com/s/1jImlFFK

提取密碼:sigy

解壓密碼:shawn

寫在前面:

這個東西是我在網上看了些教程以及源代碼,如果您覺得侵權或者有趣都請您與我聯系,這裏也是班門弄斧,第一次開發小程序,難免會有很多BUG,還請海涵以及多多指正!

代碼相關:

主要利用C#進行編寫,思路是監測網卡,CPU以及監測kernel32.dll當中的內存模塊。

本軟件基本功能有:

1. 檢測網速

2. 查看內存以及CPU使用率

基本界面如下圖所示:

技術分享

它們的含義:

技術分享

蠻好用的,日常使用,不怎麽占用空間,第一次做這種東西,還請多多指教!

技術分享

下面是MainForm

  1 using System;
  2 using System.Diagnostics;
  3 using System.Drawing;
  4 using System.Runtime.InteropServices;
  5 using System.Windows.Forms;
  6 
  7 namespace NetMonitor
  8 {
  9     public partial class MainForm : Form
 10     {
 11 
 12 
 13         public MainForm()
 14         {
 15             InitializeComponent();
16 } 17 18 [DllImport("user32.dll")] 19 public static extern bool ReleaseCapture(); 20 [DllImport("user32.dll")] 21 public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 22 public const int WM_SYSCOMMAND = 0x0112; 23
public const int SC_MOVE = 0xF010; 24 public const int HTCAPTION = 0x0002; 25 //獲取當前工作區寬度和高度(工作區不包含狀態欄) 26 int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width; 27 int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height; 28 29 [StructLayout(LayoutKind.Sequential)] 30 internal struct MEMORYSTATUSEX //這個結構用於獲得系統信息 31 { 32 internal uint dwLength; 33 internal uint dwMemoryLoad; 34 internal ulong ullTotalPhys; 35 internal ulong ullAvailPhys; 36 internal ulong ullTotalPageFile; 37 internal ulong ullAvailPageFile; 38 internal ulong ullTotalVirtual; 39 internal ulong ullAvailVirtual; 40 } 41 [return: MarshalAs(UnmanagedType.Bool)] 42 [DllImport("kernel32.dll ", CharSet = CharSet.Auto, SetLastError = true)]//調用系統DLL(內存模塊使用) 43 static extern bool GlobalMemoryStatus(ref MEMORYSTATUSEX lpBuffer); //獲得系統DLL裏的函數 44 45 PerformanceCounter cpuCounter; 46 //PerformanceCounter ramCounter; //Memory實例 47 48 private void button1_Click(object sender, EventArgs e)//這個是觸發事件的按紐 49 { 50 51 } 52 53 private void MainForm_MouseDown(object sender, MouseEventArgs e) 54 { 55 ReleaseCapture(); 56 SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); 57 //下面自動吸附功能 58 int attract = 10; //啟動吸附閾值 59 if ((this.Location.X + this.Width >= ScreenWidth - attract) && (this.Location.X + this.Width <= ScreenWidth + attract)) 60 { 61 int x = ScreenWidth - this.Width; 62 int y = this.Location.Y; 63 this.Location = new Point(x, y); 64 } 65 if ((this.Location.Y + this.Height >= ScreenHeight - attract) && (this.Location.Y + this.Height <= ScreenHeight + attract)) 66 { 67 int x = this.Location.X; 68 int y = ScreenHeight - this.Height; 69 this.Location = new Point(x, y); 70 } 71 if ((this.Location.X + 36 >= 0 - attract) && (this.Location.X + 36 <= 0 + attract)) 72 { 73 int x = -36; 74 int y = this.Location.Y; 75 this.Location = new Point(x, y); 76 } 77 } 78 79 private void MainForm_Load(object sender, EventArgs e) 80 { 81 timer1.Start(); 82 83 84 //計算窗體顯示的坐標值,可以根據需要微調幾個像素 85 int x = ScreenWidth - this.Width; 86 int y = ScreenHeight - this.Height; 87 this.Location = new Point(x, y); 88 this.ShowInTaskbar = false; 89 90 cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); 91 //ramCounter = new PerformanceCounter("Memory", "Available MBytes"); //看剩余Memory 92 } 93 94 private void timer1_Tick(object sender, EventArgs e) 95 { 96 float netRecv = NetMonitorCore.GetNetRecv(); 97 float netSend = NetMonitorCore.GetNetSend(); 98 MEMORYSTATUSEX vBuffer = new MEMORYSTATUSEX();//實例化結構 99 GlobalMemoryStatus(ref vBuffer);//給此結構賦值 100 string Memory = Convert.ToString(vBuffer.dwMemoryLoad); 101 string netRecvText = ""; 102 string netSendText = ""; 103 string netRecvUnit = ""; 104 string netSendUnit = ""; 105 106 if (netRecv < 1024 * 1000) 107 { 108 netRecvText = (netRecv / 1024).ToString("0.00"); 109 netRecvUnit = "KB/s"; 110 } 111 else if (netRecv >= 1024 * 1000) 112 { 113 netRecvText = (netRecv / (1024 * 1024)).ToString("0.00"); 114 netRecvUnit = "MB/s"; 115 } 116 117 if (netSend < 1024 * 1000) 118 { 119 netSendText = (netSend / 1024).ToString("0.00"); 120 netSendUnit = "KB/s"; 121 } 122 else if (netSend >= 1024 * 1000) 123 { 124 netSendText = (netSend / (1024 * 1024)).ToString("0.00"); 125 netSendUnit = "MB/s"; 126 } 127 128 label1.Text = netSendText; 129 label2.Text = netRecvText; 130 label3.Text = netSendUnit; 131 label4.Text = netRecvUnit; 132 label5.Text = Memory; 133 label7.Text = Convert.ToString(Math.Round(cpuCounter.NextValue(),0)); 134 } 135 136 137 private void MainForm_MouseUp(object sender, MouseEventArgs e) 138 { 139 this.Hide(); 140 this.WindowState = FormWindowState.Minimized; 141 } 142 143 144 private void notifyIcon_MouseDoubleClick(object sender, EventArgs e) 145 { 146 147 if (this.WindowState == FormWindowState.Minimized) 148 { 149 this.Show(); 150 this.WindowState = FormWindowState.Normal; 151 //this.notifyIcon1.Visible = true; 152 //this.ShowInTaskbar = true; 153 this.notifyIcon1.Text = "雙擊隱藏網速檢測"; 154 return; 155 } 156 if (this.WindowState == FormWindowState.Normal) 157 { 158 this.Hide(); 159 this.WindowState = FormWindowState.Minimized; 160 //this.notifyIcon1.Visible = true; 161 //this.ShowInTaskbar = false; 162 this.notifyIcon1.Text = "雙擊顯示網速檢測"; 163 } 164 } 165 166 private void F_Main_FormClosing(object sender, FormClosingEventArgs e) 167 { 168 169 this.Dispose(); 170 this.Close(); 171 } 172 173 174 175 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 176 { 177 178 this.Dispose(); 179 this.Close(); 180 181 } 182 183 private void 關於ToolStripMenuItem_Click(object sender, EventArgs e) 184 { 185 About aboutDialog = new About(); 186 aboutDialog.ShowDialog(); 187 } 188 189 private void label3_Click(object sender, EventArgs e) 190 { 191 192 } 193 194 private void 幫助ToolStripMenuItem_Click(object sender, EventArgs e) 195 { 196 helpDialog helpDialog = new helpDialog(); 197 helpDialog.ShowDialog(); 198 } 199 200 private void 右下角ToolStripMenuItem_Click(object sender, EventArgs e) 201 { 202 //計算窗體顯示的坐標值,可以根據需要微調幾個像素 203 int x = ScreenWidth - this.Width; 204 int y = ScreenHeight - this.Height; 205 this.Location = new Point(x, y); 206 this.Show(); 207 this.WindowState = FormWindowState.Normal; 208 } 209 210 private void 右上角ToolStripMenuItem_Click(object sender, EventArgs e) 211 { 212 int x = ScreenWidth - this.Width; 213 int y = 0; 214 this.Location = new Point(x, y); 215 this.Show(); 216 this.WindowState = FormWindowState.Normal; 217 } 218 219 private void 左下角ToolStripMenuItem_Click(object sender, EventArgs e) 220 { 221 int x = -36; 222 int y = ScreenHeight - this.Height; 223 this.Location = new Point(x, y); 224 this.Show(); 225 this.WindowState = FormWindowState.Normal; 226 } 227 228 private void 左上角ToolStripMenuItem_Click(object sender, EventArgs e) 229 { 230 int x = -36; 231 int y = 0; 232 this.Location = new Point(x, y); 233 this.Show(); 234 this.WindowState = FormWindowState.Normal; 235 } 236 237 } 238 }

下面是NetMonitorCore

 1 using System.Diagnostics;
 2 
 3 namespace NetMonitor
 4 {
 5     static class NetMonitorCore
 6     {
 7         private static PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
 8         private static string instance = performanceCounterCategory.GetInstanceNames()[1];
 9         private static int interfaceLength = instance.Length;
10         private static PerformanceCounter performanceCounterRecv = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
11         private static PerformanceCounter performanceCounterSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
12 
13         public static float GetNetRecv()
14         {
15             return performanceCounterRecv.NextValue();
16         }
17 
18         public static float GetNetSend()
19         {
20             return performanceCounterSend.NextValue();
21         }
22     }
23 }

上面就是主要代碼。

自制NetMonitor小插件