using Microsoft.VisualBasic.Devices;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Reflection.Emit;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsAppFramework
{
    public partial class Form1 : Form
    {

        Process[] myProcess = Array.Empty<Process>();
        int mhight = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            LoadTop10();
            // MessageBox.Show("Test");
        }
        private void CreateImage()
        {
            int i = panel2.Height / 100;
            Bitmap bitmap = new Bitmap(panel2.Width, panel2.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.White);
            SolidBrush solidBrush = new SolidBrush(Color.Lime);
            graphics.FillRectangle(solidBrush, 0, panel2.Height - mhight * i, panel2.Width, mhight * i);
            panel2.BackgroundImage = bitmap;
        }
        private void MyUse()
        {

            mhight = CpuPercent();
            slideShow();
            CreateImage();

        }

        private int CpuPercent()
        {
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from Win32_Processor");

            foreach (ManagementObject mobject in managementObjectSearcher.Get())
            {
                return Convert.ToInt32(mobject["LoadPercentage"]?.ToString());
            }
            return 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                MyUse();
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (null == listBox1.SelectedItem)
            {
                MessageBox.Show("请选择进程");
                return;
            }
            string name = listBox1.SelectedItem.ToString() ?? "";
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("请选择进程");
                return;
            }
            Process[] processs = Process.GetProcesses();
            foreach (var p in processs)
            {
                try
                {
                    if (p.ProcessName.ToLower().Trim().Equals(name))
                    {
                        p.Kill();
                        MessageBox.Show("已关闭进程，请刷新！");
                        return;
                    }
                }
                catch
                {
                    return;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadTop10();
        }

        private void slideShow()
        {
            this.BeginInvoke(new Action(() =>
            {
                this.label12.Text = Process.GetProcesses().Length.ToString();
                this.label13.Text = mhight + "%";
            }));
        }

        private void LoadTop10()
        {
            this.BeginInvoke(new Action(() =>
            {
                // 获取当前进程对象
                Process currentProcess = Process.GetCurrentProcess();
                // 获取系统的总内存大小
                ComputerInfo computerInfo = new ComputerInfo();
                var totalMemory = Convert.ToDouble(computerInfo.TotalPhysicalMemory) / 1024 / 1024 / 1024;
                this.label6.Text = totalMemory.ToString("0.00");
                this.progressBar1.Maximum = Convert.ToInt32(totalMemory);
                this.progressBar1.Value = Convert.ToInt32(totalMemory);
                // 获取系统的内存剩余大小
                var availablePhysicalMemory = Convert.ToDouble(computerInfo.AvailablePhysicalMemory) / 1024 / 1024 / 1024;
                this.label5.Text = availablePhysicalMemory.ToString("0.00");
                this.progressBar2.Maximum = Convert.ToInt32(totalMemory);
                this.progressBar2.Value = Convert.ToInt32(availablePhysicalMemory);


                // 获取系统的总虚拟内存大小
                var totalVirtualMemory = Convert.ToDouble(computerInfo.TotalVirtualMemory) / 1024 / 1024 / 1024;
                this.label8.Text = totalVirtualMemory.ToString("0.00");
                this.progressBar3.Maximum = Convert.ToInt32(totalVirtualMemory);
                this.progressBar3.Value = Convert.ToInt32(totalVirtualMemory);
                // 获取系统的虚拟内存剩余大小
                var availableVirtualMemory = Convert.ToDouble(computerInfo.AvailableVirtualMemory) / 1024 / 1024 / 1024;
                this.label7.Text = availableVirtualMemory.ToString("0.00");
                this.progressBar4.Maximum = Convert.ToInt32(totalVirtualMemory * 100);
                this.progressBar4.Value = Convert.ToInt32(availableVirtualMemory * 100);
                var processes = Process.GetProcesses();
                var list = processes.OrderByDescending(p =>
                {
                    return p.PagedMemorySize64;
                }).Take(15).Select(p => p.ProcessName);
                listBox1.Items.Clear();
                listBox1.Items.AddRange(list.ToArray());

                this.label12.Text = processes.Length.ToString();
                this.label13.Text = CpuPercent() + "%";

                // 获取系统的 CPU 核数
                int cpuCores = Environment.ProcessorCount;

                // 获取可用的 CPU 核数
                int availableCores = currentProcess.ProcessorAffinity.ToInt32();
            }));
        }
    }
}
