如何知道 openoffice 的哪个应用程序正在运行?

发布于 2024-08-06 00:44:35 字数 209 浏览 2 评论 0原文

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

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

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

I want to look in the list of processes to determine if OpenOffice Calc is running or if OpenOffice Writer is running. With QuickStart off, I get a scalc.exe and an swriter.exe so its simple.

However when the quick start is on I just get soffice.bin and soffice.exe

Is there a way of asking those processes which application is running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-08-13 00:44:35

我认为没有办法检查窗口标题。您必须枚举所有窗口并检查标题是否以“- 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");
                }
            }
        }
    }
}

I think there is no way around checking the window title. You would have to enumerate all windows and check whether the title ends with "- OpenOffice.org Writer" or "- OpenOffice.org Calc" etc.

The following short sample would check whether Writer is running:

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");
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文