必威体育Betway必威体育官网
当前位置:首页 > IT技术

NetMonitor V1.0发布!

时间:2019-06-03 17:40:00来源:IT技术作者:seo实验室小编阅读:79次「手机版」
 

netmonitor

下载链接:http://pan.baidu.com/s/1jImlFFK 

提取密码:sigy

解压密码:shawn

写在前面:

这个东西是我在网上看了些教程以及源代码,如果您觉得侵权或者有趣都请您与我联系,这里也是班门弄斧,第一次开发程序,难免会有很多BUG,还请海涵以及多多指正!

软件基本功能有:

1. 检测网速

2. 查看内存以及cpu使用率

基本界面如下图所示:

它们的含义:

蛮好用的,日常使用,不怎么占用空间,第一次做这种东西,还请多多指教!

下面是MainForm:

using System;
using System.Diagnostics;
using System.Drawing;
using System.runtime.InteropServices;
using System.windows.Forms;

namespace netmonitor
{
    public partial class MainForm : Form
    {


        public MainForm()
        {
            Initializecomponent();
        }

        [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;
        //获取当前工作区宽度和高度(工作区不包含状态栏)
        int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
        int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;

        [structlayout(LayoutKind.Sequential)]
        internal struct MEMORYSTATUSEX //这个结构用于获得系统信息
        {
            internal uint dwLength;
            internal uint dwMemoryLoad;
            internal ulong ullTotalPhys;
            internal ulong ullAvailPhys;
            internal ulong ullTotalPageFile;
            internal ulong ullAvailPageFile;
            internal ulong ullTotalvirtual;
            internal ulong ullAvailVirtual;
        }
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("kernel32.dll ", CharSet = CharSet.Auto, SetLastERROR = true)]//调用系统DLL(内存模块使用)
        static extern bool GlobalMemoryStatus(ref MEMORYSTATUSEX lpBuffer); //获得系统DLL里的函数

        PerformanceCounter cpuCounter;
        //PerformanceCounter ramCounter; //Memory实例

        private void button1_Click(object sender, EventArgs e)//这个是触发事件的按纽
        {
            
         }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            //下面自动吸附功能
            int attract = 10; //启动吸附阈值
            if ((this.Location.X + this.Width >= ScreenWidth - attract) && (this.Location.X + this.Width <= ScreenWidth + attract))
            {
                int x = ScreenWidth - this.Width;
                int y = this.Location.Y;
                this.Location = new Point(x, y);
            }
            if ((this.Location.Y + this.Height >= ScreenHeight - attract) && (this.Location.Y + this.Height <= ScreenHeight + attract))
            {
                int x = this.Location.X;
                int y = ScreenHeight - this.Height;
                this.Location = new Point(x, y);
            }
            if ((this.Location.X + 36 >= 0 - attract) && (this.Location.X + 36 <= 0 + attract))
            {
                int x = -36;
                int y = this.Location.Y;
                this.Location = new Point(x, y);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            timer1.Start();
            
            
            //计算窗体显示的坐标值,可以根据需要微调几个像素
            int x = ScreenWidth - this.Width;
            int y = ScreenHeight - this.Height;
            this.Location = new Point(x, y);
            this.ShowInTaskbar = false;

            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
            //ramCounter = new PerformanceCounter("Memory", "Available MBytes");  //看剩余Memory
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            float netRecv = NetMonitorCore.GetNetRecv();
            float netSend = NetMonitorCore.GetNetSend();
            MEMORYSTATUSEX vBuffer = new MEMORYSTATUSEX();//实例化结构
            GlobalMemoryStatus(ref vBuffer);//给此结构赋值
            string Memory = Convert.ToString(vBuffer.dwMemoryLoad);
            string netRecvText = "";
            string netSendText = "";
            string netRecvUnit = "";
            string netSendUnit = "";

            if (netRecv < 1024 * 1000)
            {
                netRecvText = (netRecv / 1024).ToString("0.00");
                netRecvUnit = "KB/s";
            }
            else if (netRecv >= 1024 * 1000)
            {
                netRecvText = (netRecv / (1024 * 1024)).ToString("0.00");
                netRecvUnit = "MB/s";
            }

            if (netSend < 1024 * 1000)
            {
                netSendText = (netSend / 1024).ToString("0.00");
                netSendUnit = "KB/s";
            }
            else if (netSend >= 1024 * 1000)
            {
                netSendText = (netSend / (1024 * 1024)).ToString("0.00");
                netSendUnit = "MB/s";
            }

            label1.Text = netSendText;
            label2.Text = netRecvText;
            label3.Text = netSendUnit;
            label4.Text = netRecvUnit;
            label5.Text = Memory;
            label7.Text = Convert.ToString(Math.Round(cpuCounter.nextvalue(),0));
        }

        
        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            this.Hide();
            this.WindowState = FormWindowState.Minimized;
        }


        private void notifyIcon_MouseDoubleClick(object sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                //this.notifyIcon1.Visible = true;
                //this.ShowInTaskbar = true;
                this.notifyIcon1.Text = "双击隐藏网速检测";
                return;
            }
            if (this.WindowState == FormWindowState.Normal)
            {
                this.Hide();
                this.WindowState = FormWindowState.Minimized;
                //this.notifyIcon1.Visible = true;
                //this.ShowInTaskbar = false;
                this.notifyIcon1.Text = "双击显示网速检测";
            }
        }

        private void F_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
         
            this.Dispose();
            this.Close();
         }
        


        private void 退出ToolstripMenuItem_Click(object sender, EventArgs e)
        {
           
            this.Dispose();
            this.Close();

        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About aboutDialog = new About();
            aboutDialog.ShowDialog();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
                helpDialog helpDialog = new helpDialog();
                helpDialog.ShowDialog();
        }

        private void 右下角ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //计算窗体显示的坐标值,可以根据需要微调几个像素
            int x = ScreenWidth - this.Width;
            int y = ScreenHeight - this.Height;
            this.Location = new Point(x, y);
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void 右上角ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = ScreenWidth - this.Width;
            int y = 0;
            this.Location = new Point(x, y);
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void 左下角ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = -36;
            int y = ScreenHeight - this.Height;
            this.Location = new Point(x, y);
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void 左上角ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = -36;
            int y = 0;
            this.Location = new Point(x, y);
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

    }
}
下面是NetMonitorCore:
using System.Diagnostics;

namespace NetMonitor
{
    static class NetMonitorCore
    {
        private static PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
        private static string instance = performanceCounterCategory.GetInstanceNames()[1];
        private static int interfaceLength = instance.Length;
        private static PerformanceCounter performanceCounterRecv = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
        private static PerformanceCounter performanceCounterSend = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);

        public static float GetNetRecv()
        {
            return performanceCounterRecv.NextValue();
        }

        public static float GetNetSend()
        {
            return performanceCounterSend.NextValue();
        }
    }
}
上面就是主要代码。

相关阅读

JAVA项目中发布WebService服务——调用方式

相关myeclipse创建webservice和测试client项目可以参考如下 http://www.cnblogs.com/yisheng163/p/4524808.html?utm_source=tuic

提前看iPhone11发布会:不用熬夜了

黑客技术点击右

【人人早报】573期:2016年苹果春季新品发布会

导读北京时间3月22日凌晨,苹果召开了2016年春季发布会。苹果发布了iPhone SE,采用A9处理器,4种颜色可选,售价399美元起。iPhone SE采

[开源]Fre 发布 0.5 版本,更新 diff-patch 和 proxy 方

halo 大家好,我是 132,然后就是…… 经过最近几天的探讨,fre 终于发了一个……能跑起来的版本了这次的更新主要是针对 diff 和 proxy

坚果新机月底发布!配置、售价可能超出你的想象

日前,坚果手机官方公布了新机的发布时间,确定为10月31日晚七点半,在北京工业大学奥林匹克体育馆举行。时隔一年的时间,坚果新机承载了

分享到:

栏目导航

推荐阅读

热门阅读