How to get the main window handle of a process using JScript?

ぃ、小莉子 提交于 2019-12-10 22:18:30

问题


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?


回答1:


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.




回答2:


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?




回答3:


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.




回答4:


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)



来源:https://stackoverflow.com/questions/3708151/how-to-get-the-main-window-handle-of-a-process-using-jscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!