如何使用 JScript 获取进程的主窗口句柄?

发布于 2024-09-19 02:31:52 字数 264 浏览 13 评论 0原文

JScript 中是否有任何方法可以通过提供进程名称来获取进程主窗口的句柄? Process.MainWindowHandle< /a> 属性仅适用于 JScript .NET。经典 JScript 中有类似的东西吗?

Is there any method in JScript to get the handle of the main window of a process by providing the process name? The Process.MainWindowHandle property works only in JScript .NET. Is anything similar available in classic JScript?

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

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

发布评论

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

评论(4

飞烟轻若梦 2024-09-26 02:31:52

我不确定这是否有效,只需尝试循环 window.parent 直到其未定义。

像 -

var mainWindow = window;
while( mainWindow.parent ) {
    mainWindow = mainWindow.parent;
}

你还有像 window.top 这样的东西,它总是返回最上面的窗口。但不确定是否所有浏览器都支持。

I am not sure if this works, just try to loop window.parent until its undefined.

something like -

var mainWindow = window;
while( mainWindow.parent ) {
    mainWindow = mainWindow.parent;
}

you also have something like window.top which always returns you the topmost window. But not sure if this is supported by all browsers.

梦巷 2024-09-26 02:31:52

JScript 和 Windows Script Host 没有此功能,WMI 也没有。

如果 PowerShell 适合您,那么您可以使用您提到的 Process.MainWindowHandle 属性:

(Get-Process notepad).MainWindowHandle

否则,您需要查找或编写一个实用程序(COM 对象、命令行工具等)将提供此功能,并从您的脚本调用此工具。


编辑:所以您需要关闭窗口 - 这是一个 UI 自动化任务。

Windows 脚本宿主提供非常有限的 UI 自动化功能。如果您知道窗口标题,可以尝试使用 AppActivateSendKeys 方法激活该窗口并向其发送 Alt+F4 快捷方式。您可以找到一个示例此答案。 (该代码是用 VBScript 编写的,但它应该可以让您了解。)但是,这种方法并不可靠。

如果您真的不想终止该进程,最简单的解决方案是使用一些第三方 UI 自动化工具。例如,您可以尝试免费的 AutoIt 工具 - 我认为它应该能够完成您的任务需要。


编辑2:您是否尝试过记录窗口的关闭?您应该得到这样的脚本:

Sys.Process("notepad").Window("Notepad", "Untitled - Notepad").Close();

这不是您需要的吗?

JScript and Windows Script Host don't have this functionality, and neither does WMI.

If PowerShell is an option for you, then you can use the Process.MainWindowHandle property you mentioned:

(Get-Process notepad).MainWindowHandle

Otherwise, you'll need to find or write an utility (COM object, command-line tool etc) that would provide this functionality, and call this tool from your script.


Edit: So you need to close the window — that's a UI automation task.

Windows Script Host provides very limited UI automation functionality. If you know the window title, you could try using the AppActivate to and SendKeys methods to activate that window and send the Alt+F4 shortcut to it. You can find an example this answer. (The code is in VBScript, but it should give you the idea.) However, this approach isn't reliable.

If you really really don't want to kill the process, the easiest solution is to use some third-party UI automation tool. For example, you could try the free AutoIt tool — I think it should be able to accomplish what you need.


Edit 2: Have you tried recording the closing of the window? You should get a script like this:

Sys.Process("notepad").Window("Notepad", "Untitled - Notepad").Close();

Isn't this what you need?

明媚殇 2024-09-26 02:31:52

对于本机 win32 应用程序,不存在“主窗口”之类的东西。一个进程可以根本没有窗口,也可以有几个顶级“主”窗口。

For a native win32 application, there is no such thing as a "main window". A process can have no windows at all, or several top level "main" windows.

素年丶 2024-09-26 02:31:52

有一次我不得不为 Outlook 编写一个插件。我的老板希望 Outlook 加载时出现启动屏幕。但 Outlook 窗口超出了启动画面。经过大量搜索后,我发现 FindWindow http://msdn.microsoft.com /query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28FINDWINDOW%29%3bk%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29%3bk%28DevLang-CSHARP% 29&rd=true 这是对它的帮助。该函数根据窗口标题和窗口类名查找窗口。我通过 C# 调用并使用了它。如果您可以通过 JScript 使用此功能,我认为它可以为您完成这项工作。 (我使用 Spy++ 查找 lpClassName 参数)

Well once i had to write a add-in for Outlook. My boss wants a splash-screen to appear when Outlook loads. But Outlook window goes over the splash. After a lot of search i found FindWindow http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28FINDWINDOW%29%3bk%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29%3bk%28DevLang-CSHARP%29&rd=true this is help for it . This function finds window based on window caption and window class name. I p-invoked it and used it from C#. If you can use this function through JScript I think it could do the job for you. (I used Spy++ for finding lpClassName parameter)

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