using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;

namespace MinitaskMan_Run
{
    public partial class miniTaskManager : Form
    {
        public miniTaskManager()
        {
            InitializeComponent();
        }
        //dll imports
        [DllImport("powrprof.dll")]
        public static extern Int32 SetSuspendState(Int32 HiberNate, Int32 Force, Int32 Disible);

        //for shutdown restart log off...
        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int uFlags, int dwReason);
        [DllImport("user32.dll")]
        public static extern void LockWorkStation();

        //end import dll

        
        private void turnOffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("shutdown", "-s -t 00");
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("shutdown", "-r -t 00");
        }

        private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExitWindowsEx(4, 0);
        }

        private void hibernateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //SetSuspendState(1, 0, 0);
            Application.SetSuspendState(PowerState.Hibernate, true, true);
        }

        private void standbyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //SetSuspendState(0, 0, 0);
            Application.SetSuspendState(PowerState.Suspend, true, true);
        }

        private void tmrTaskMgr_Tick(object sender, EventArgs e)
        {
            Process[] allp = Process.GetProcesses();
            if (listBox1.Items.Count != allp.Length)
            {
                listBox1.Items.Clear();
                foreach (Process p in allp)
                {
                    listBox1.Items.Add(p.ProcessName);
                }
                listBox1.SetSelected(0, true);
                label1.Text = "Run Process :"+listBox1.Items.Count.ToString();    
            
            }
        }

        private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!alwaysOnTopToolStripMenuItem.Checked)
            {
                alwaysOnTopToolStripMenuItem.Checked = true;
                this.TopMost = true;
            }
            else 
            {
                alwaysOnTopToolStripMenuItem.Checked = false;
                this.TopMost = false;
            }
        }

        private void exitTaskManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            Process[] allp = Process.GetProcesses();
            foreach (Process p in allp)
            {
                if (listBox1.SelectedItem.ToString().ToLower() == p.ProcessName.ToLower())
                {
                    DialogResult end = MessageBox.Show("Are you sure you want end this process ?", "Task Man", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (end == DialogResult.Yes) p.Kill();
                }
            }

        }
        catch
        {
            MessageBox.Show("The Process can not be end", "Task Man", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
          
        }

        private void swichUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LockWorkStation();
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int sel = (e.X / 120 * 22) + (e.Y / 13);
            if(sel<listBox1.Items.Count) listBox1.SetSelected(sel,true);
            
        }

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

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Process[] allp = Process.GetProcesses();
            tmrTaskMgr.Enabled = false;
            listBox1.Items.Clear();
            foreach (Process p in allp)
            {
                if (p.MainWindowTitle.ToLower() == this.Text.ToLower()) continue;
                System.Threading.Thread.Sleep(30);
                listBox1.Items.Add(p.ProcessName);
                Application.DoEvents();
            }
            listBox1.SetSelected(0, true);
            tmrTaskMgr.Enabled = true;
        }

        private void newTaskRunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            propertisToolStripMenuItem_Click(sender, e);
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            ToolTip listB = new ToolTip();
            listB.ToolTipTitle = "Information Of Selected Process";
            listB.ToolTipIcon = ToolTipIcon.Info;
            listB.AutomaticDelay = 50;
            listB.AutoPopDelay = 15000;
            listB.UseAnimation = true;

            Process[] allp = Process.GetProcesses();
                foreach (Process p in allp)
                {
                    if (p.ProcessName.ToLower() == listBox1.SelectedItem.ToString().ToLower())
                    {
                        try
                        {
                            FileInfo a = new FileInfo(p.MainModule.FileName);
                            string len = null;
                            if (a.Length / 1073741824 > 0)
                                len =string.Format("{0:n2}", (double)a.Length / 1073741824) + " GB";
                            else if (a.Length / 1048576 > 0) len =string.Format("{0:n2}", (double)a.Length /1048576 ) + " MB";
                            else if (a.Length / 1024 > 0) len =string.Format("{0:n2}",a.Length / 1024) + " KB";
                            else len = a.Length + " Byte";
                            listB.SetToolTip(listBox1, "Process Name :" + p.ProcessName
                                + "\nMem Usage :" + p.WorkingSet / 1024 + " k"
                                + "\nProcess Id :" + p.Id
                                + "\nAddress :" + p.MainModule.FileName
                                + "\nFile Name :" + p.MainModule.FileVersionInfo.OriginalFilename
                                + "\nFile Version :" + p.MainModule.FileVersionInfo.FileVersion
                                + "\nFile Size :" + len
                                + "\nCreationTime :" + a.CreationTime
                                + "\nUser Name :" + Environment.UserName
                                + "\nComputer Name :" + Environment.MachineName);
                            
                        }
                        catch {listB.SetToolTip(listBox1,"I'm not access to properties of this process.\nSelected process is a System process."); };
                    }
                }
                
                   
        }
        

        private void miniTaskManager_Shown(object sender, EventArgs e)
        {
            label1.Text = "UserDomainName: " + Environment.UserDomainName;
        }

        private void propertisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName.ToLower() == listBox1.SelectedItem.ToString().ToLower())
                {
                    try
                    {
                        FileInfo a = new FileInfo(p.MainModule.FileName);
                        string len = null;
                        if (a.Length / 1073741824 > 0)
                            len = string.Format("{0:n2}", (double)a.Length / 1073741824) + " GB";
                        else if (a.Length / 1048576 > 0) len = string.Format("{0:n2}", (double)a.Length / 1048576) + " MB";
                        else if (a.Length / 1024 > 0) len = string.Format("{0:n2}", a.Length / 1024) + " KB";
                        else len = a.Length + " Byte";
                        MessageBox.Show("Process Name :" + p.ProcessName
                            + "\nWindowTitle :" + p.MainWindowTitle
                            + "\nAddress :" + p.MainModule.FileName
                            + "\nFile Name :" + p.MainModule.FileVersionInfo.OriginalFilename
                            + "\nFile Version :" + p.MainModule.FileVersionInfo.FileVersion
                            + "\nFile Size :" + len
                            + "\nCreationTime :" + a.CreationTime
                            + "\nLastAccessTime :" + a.LastAccessTime
                            + "\nLastWriteTime :" + a.LastWriteTime
                            + "\nUser Name :" + Environment.UserName
                            + "\nComputer Name :" + Environment.MachineName
                            + "\nStartTime :" + p.StartTime
                            + "\nMem Usage :" + p.WorkingSet / 1024 + " k"
                            + "\nProcess Id :" + p.Id
                            + "\nSessionId :" + p.SessionId
                            + "\nPriority :" + p.PriorityClass
                            + "\nPagedMemorySize :" + p.PagedMemorySize
                            + "\nWindowHandle :" + p.MainWindowHandle
                            + "\nBasePriority :" + p.BasePriority
                            ,p.ProcessName,MessageBoxButtons.OK
                            ,MessageBoxIcon.Information);

                    }
                    catch { };
                }
            }
        }
       
        /// <summary>
        /// For Set Priority Mnu ! ☺
        /// </summary>
        /// <param name="PPC"></param>
        private void setPriority(ProcessPriorityClass PPC)
        {
            try
            {
                if (listBox1.SelectedIndex != -1)
                    foreach (Process p in Process.GetProcesses())
                    {
                        if (p.ProcessName.ToLower() == listBox1.SelectedItem.ToString().ToLower())
                        {
                            p.PriorityClass = PPC;
                            break;
                        }
                    }
            }
            catch { }
        }

        private void realTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.RealTime);
        }

        private void highToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.High);
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.AboveNormal);
        }

        private void normalToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.Normal);
        }

        private void belowNormalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.BelowNormal);
        }

        private void lowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPriority(ProcessPriorityClass.Idle);
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            propertisToolStripMenuItem_Click(sender, e);
        }

        
        
     }
}
