日期:2014-05-18  浏览次数:20816 次

获得所有进程的标题的问题?
/获取当前系统正在运行的程序
  string MyInfo = "当前系统正在运行的程序包括:\n";
  Process[] MyProcesses = Process.GetProcesses();
  foreach (Process MyProcess in MyProcesses)
  {
  if (MyProcess.MainWindowTitle.Length > 0)
  MyInfo += MyProcess.MainWindowTitle+"\n";
  }
  MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
隐藏窗体的标题怎么得不到。
例如 讯雷 QQ等得不到。为什么。请大师们指点。
还有就是能不能提供下代码。我是初学者。谢谢。

------解决方案--------------------
参考如下代码:
C# code
using System.Runtime.InteropServices;

private const int MAX_PATH = 260;
private struct PROCESSENTRY32
{
    // data members
    public uint dwSize;
    public uint cntUsage;
    public uint th32ProcessID;
    public uint th32DefaultHeapID;
    public uint th32ModuleID;
    public uint cntThreads;
    public uint th32ParentProcessID;
    public int pcPriClassBase;
    public uint dwFlags;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_PATH)]
    public byte[] szExeFile;
}


private const int TH32CS_SNAPPROCESS = 0x00000002;
[DllImport("kernel32")]
private static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processID);
[DllImport("kernel32")]
private static extern bool Process32First(IntPtr handle, ref PROCESSENTRY32 procData);
[DllImport("kernel32")]
private static extern bool Process32Next(IntPtr handle, ref PROCESSENTRY32 procData);
[DllImport("kernel32")]
private static extern bool CloseHandle(IntPtr handle);

private void button1_Click(object sender, EventArgs e)
{
    IntPtr vHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    PROCESSENTRY32 vProcessEnter = new PROCESSENTRY32();
    vProcessEnter.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
    if (Process32First(vHandle, ref vProcessEnter))
    {
        do
        {
            int j = 0;
            for (int i = 0; i < vProcessEnter.szExeFile.Length; i++)
                if (vProcessEnter.szExeFile[i] == 0)
                {
                    j = i;
                    break;
                }
            Console.WriteLine("进程ID:{0},进程文件名:{1}", 
                vProcessEnter.th32ProcessID,
                Encoding.Default.GetString(vProcessEnter.szExeFile, 0, j));
        }
        while (Process32Next(vHandle, ref vProcessEnter));
    }

}