2009-09-29 4 views
1

Je veux regarder dans la liste des processus pour déterminer si OpenOffice Calc est en cours d'exécution ou si OpenOffice Writer est en cours d'exécution. Avec QuickStart désactivé, j'obtiens un scalc.exe et un swriter.exe donc c'est simple.Comment puis-je savoir quelle application de openoffice est en cours d'exécution?

Cependant, lorsque le démarrage rapide est-je obtenir juste soffice.bin et soffice.exe

Est-il possible de demander à ces processus dont l'application est en cours d'exécution?

Répondre

2

Je pense qu'il n'y a aucun moyen de vérifier le titre de la fenêtre. Vous auriez d'énumérer toutes les fenêtres et vérifier si le titre se termine par « - OpenOffice.org Writer » ou « - OpenOffice.org Calc », etc.

Le court exemple suivant vérifierait si Writer est en cours d'exécution:

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Text; 

namespace Sample 
{ 
    public class Window 
    { 
     public string Title { get; set; } 
     public int Handle { get; set; } 
     public string ProcessName { get; set; } 
    } 

    public class WindowHelper 
    { 
     /// <summary> 
     /// Win32 API Imports 
     /// </summary> 
     [DllImport("user32.dll")] 
     private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size); 
     [DllImport("user32.dll")] 
     private static extern int EnumWindows(EnumWindowsProc ewp, int lParam); 
     [DllImport("user32.dll")] 
     private static extern bool IsWindowVisible(IntPtr hWnd); 
     [DllImport("user32.dll", SetLastError = true)] 
     static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

     public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); 

     private List<Window> _openOfficeWindows; 

     public List<Window> GetOpenOfficeWindows() 
     { 
      _openOfficeWindows = new List<Window>(); 
      EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow); 
      EnumWindows(ewp, 0); 

      return _openOfficeWindows; 
     } 

     private bool EvalWindow(IntPtr hWnd, int lParam) 
     { 
      if (!IsWindowVisible(hWnd)) 
       return (true); 

      uint lpdwProcessId; 
      StringBuilder title = new StringBuilder(256); 

      GetWindowThreadProcessId(hWnd, out lpdwProcessId); 
      GetWindowText(hWnd, title, 256); 

      Process p = Process.GetProcessById((int)lpdwProcessId); 
      if (p != null && p.ProcessName.ToLower().Contains("soffice")) 
      { 
       _openOfficeWindows.Add(new Window() { Title = title.ToString(), Handle = hWnd.ToInt32(), ProcessName = p.ProcessName }); 
      } 

      return (true); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      WindowHelper helper = new WindowHelper(); 

      List<Window> openOfficeWindows = helper.GetOpenOfficeWindows(); 

      foreach (var item in openOfficeWindows) 
      { 
       if (item.Title.EndsWith("- OpenOffice.org Writer")) 
       { 
        Console.WriteLine("Writer is running"); 
       } 
      } 
     } 
    } 
} 
Questions connexes