【问题标题】:How can I tell which application of openoffice is running?如何判断 openoffice 的哪个应用程序正在运行?
【发布时间】:2009-09-29 09:16:52
【问题描述】:

我想查看进程列表以确定 OpenOffice Calc 是否正在运行或 OpenOffice Writer 是否正在运行。关闭 QuickStart 后,我​​会得到一个 scalc.exe 和一个 swriter.exe,所以很简单。

但是,当快速启动启动时,我只得到 soffice.bin 和 soffice.exe

有没有办法询问这些进程正在运行哪个应用程序?

【问题讨论】:

    标签: c# windows ipc openoffice.org


    【解决方案1】:

    我认为没有办法检查窗口标题。您必须枚举所有窗口并检查标题是否以“-OpenOffice.org Writer”或“-OpenOffice.org Calc”等结尾。

    以下简短示例将检查 Writer 是否正在运行:

    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");
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多