Visual Studio 2013 - C# 改革了使用 C# 编写的任务管理器代码

本文关键字:代码 任务管理器 2013 Studio Visual | 更新日期: 2023-09-27 17:55:08

这是C#(Visual Studio 2013)不是这些代码,无论是由我还是为我...你好,我有这个代码

/* Made by TheDarkJoker94. 
*  Check http://thedarkjoker094.blogspot.com/ for more C# Tutorials 
*  and also SUBSCRIBE to my Youtube Channel http://www.youtube.com/user/TheDarkJoker094 
*  Thanks! */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; 
namespace SimpleTaskManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Process[] procs;
        public void GetProcesses()
        {
            procs = Process.GetProcesses();
            if (Convert.ToInt32(label2.Text) != procs.Length) // Check if new processes have been started or terminated
            {
                listBox1.Items.Clear();
                for (int i = 0; i < procs.Length; i++)
                {
                    listBox1.Items.Add(procs[i].ProcessName);  // Add the process name to the listbox
                }
                label2.Text = procs.Length.ToString();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            GetProcesses();
        }
        // Check every 1 second for changes in the processes list
        private void timer1_Tick(object sender, EventArgs e)
        {
            GetProcesses();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            procs[listBox1.SelectedIndex].Kill(); // Kill the process coresponding to the selected index of listbox1 
        }
        private void kIllProcessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            procs[listBox1.SelectedIndex].Kill();
        }
        public void lsit()
        {
        }
    }
}

它工作得很好,但并不完全是我想要的......我需要一段代码来告诉我目前哪个是前景窗口......所以使用这个其他代码我存档了它...

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
namespace activeWindow
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        [ DllImport("user32.dll") ]
        static extern int GetForegroundWindow();
        [ DllImport("user32.dll") ]
        static extern int GetWindowText(int hWnd, StringBuilder text, int count);
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label captionWindowLabel;
        private System.Windows.Forms.Label IDWindowLabel;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Timer timer1;
        private System.ComponentModel.IContainer components;
        //
        // GetForegroundWindow API 
        //
        private void GetActiveWindow()
        {
            const int nChars = 256;
            int handle = 0;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();
            if ( GetWindowText(handle, Buff, nChars) > 0 )
            {
                this.captionWindowLabel.Text = Buff.ToString();
                this.IDWindowLabel.Text = handle.ToString();
            }
        }

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.captionWindowLabel = new System.Windows.Forms.Label();
            this.IDWindowLabel = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(136, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(112, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "Active Window Detail";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(16, 40);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(96, 16);
            this.label2.TabIndex = 1;
            this.label2.Text = "Window Caption : ";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(16, 88);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(96, 16);
            this.label3.TabIndex = 2;
            this.label3.Text = "Window Handle :";
            // 
            // captionWindowLabel
            // 
            this.captionWindowLabel.Location = new System.Drawing.Point(112, 40);
            this.captionWindowLabel.Name = "captionWindowLabel";
            this.captionWindowLabel.Size = new System.Drawing.Size(224, 40);
            this.captionWindowLabel.TabIndex = 3;
            // 
            // IDWindowLabel
            // 
            this.IDWindowLabel.Location = new System.Drawing.Point(112, 88);
            this.IDWindowLabel.Name = "IDWindowLabel";
            this.IDWindowLabel.Size = new System.Drawing.Size(100, 16);
            this.IDWindowLabel.TabIndex = 4;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(176, 128);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(48, 24);
            this.button1.TabIndex = 5;
            this.button1.Text = "EXIT";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(400, 173);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.IDWindowLabel,
                                                                          this.captionWindowLabel,
                                                                          this.label3,
                                                                          this.label2,
                                                                          this.label1});
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Window Information";
            this.TopMost = true;
            this.ResumeLayout(false);
        }
        #endregion
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        private void timer1_Tick(object sender, System.EventArgs e)
        {
            GetActiveWindow();
        }
        private void button1_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}

说明:第一个代码给了我当时正在运行的所有应用程序的进程名称......第二个给了我"窗口名称"和一个代码......前景中的应用程序....我需要什么...是前景窗口的进程名称...如您所见...如果我能"混合"他们两个...它会给我我需要的东西...唔......我只需要变量中实际前景窗口的名称,这样我就可以在代码中使用......不需要窗口,框或我桌面上的实际文本...谢谢

Visual Studio 2013 - C# 改革了使用 C# 编写的任务管理器代码

要获取前台窗口的进程名称,您必须获取前台窗口GetForegroundWindow,然后您可以使用GetWindowThreadProcessId获取其进程 ID:

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
Process GetActiveProcess()
{
    IntPtr hwnd = GetForegroundWindow();
    uint pid;
    GetWindowThreadProcessId(hwnd, out pid);
    Process p = Process.GetProcessById((int)pid);
    return p;
}

然后,您可以获取进程名称:

GetActiveProcess().ProcessName