标签:实例 his == apt 结构 状态 技术 item hid
下载链接: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 }
上面就是主要代码。
标签:实例 his == apt 结构 状态 技术 item hid
原文地址:http://www.cnblogs.com/gyx215/p/7210548.html