开发者

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

Is there any method in JScript to get the handle of 开发者_C百科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?


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.


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?


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜