如何从另一个HTA关闭HTA窗口? [英] How to close a HTA window from another HTA?

查看:125
本文介绍了如何从另一个HTA关闭HTA窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现自己的模式对话框(为什么?).我使用WScript.Shell打开一个新窗口:

I've tried to implement my own modal dialogs (Why?). I open a new window using WScript.Shell:

dlg = shell.Run(dlgPath, 1, true);

上面的行创建一个半模态" HTA窗口.脚本的执行在这一行停止,但是主窗口仍处于响应状态.此问题已在onfocusin事件处理程序中解决,该事件处理程序使用shell.AppActivate()将焦点返回到新的HTA窗口.这甚至防止了关闭窗口"按钮关闭主窗口.但是,在Windows7中,有一个后门可以关闭主窗口,即Windows任务栏中的图标.

The line above creates a "semi-modal" HTA window. The script execution is stopped at this line, but the main window is still responsive. This issue is tackled within onfocusin eventhandler, which returns focus back to the new HTA window using shell.AppActivate(). This prevents even "Close Window" button to close the main window. However, in Windows7 there's a back gate to close the main window, the icon in Windows Task Bar.

如果关闭主窗口,则也应关闭一个体面的模态对话框.在这种情况下,由于打开的HTA窗口不是主窗口的真实子窗口,因此不会关闭.

If the main window is closed, a decent modal dialog should be closed as well. In this case it is not closed, since the opened HTA window is not a real child window of the main.

我可以在下面主窗口的onbeforeunload处理函数中关闭此对话框模拟器.

I can close this dialog emulator within main window's onbeforeunload handler function below.

beforeTopClose = function () {
    var wmiLocator = new ActiveXObject('WbemScripting.SWbemLocator'),
        wmiService = wmiLocator.ConnectServer('.', 'root\\CIMV2'),
        htas = new Enumerator(wmiService.ExecQuery("Select * from Win32_Process Where name = 'mshta.exe'"));
    while (!htas.atEnd()) {
        if (htas.item().CommandLine.indexOf('_tools\\dlgbase') > -1) {
            htas.item().Terminate(0);
        }
        htas.moveNext();
    }
    return;
}

现在一切看起来都很好,屏幕上没有剩余我的应用程序窗口.但是,当我打开Windows Task Manager进程-tab时,可以看到主窗口的进程仍在运行.

Now everything looks good, there are no windows of my app left on the screen. However, when I open Windows Task Manager Processes -tab, I can see the process of the main window still running.

为什么主窗口仍在运行?它在等待shell进程的结束吗?我应该在onbeforeunload处理程序中怎么做才能完全关闭主窗口?如果需要其他方法,请告诉我.

Why the main window is still running? Is it waiting for the end of the shell-process? What should I do in onbeforeunload handler to get the main window closed totally? If I need a different approach to this, please let me know.

推荐答案

我刚刚意识到,我也可以在beforeTopClose()中终止主窗口.固定代码如下.

I just realized, that I can terminate the main window as well in beforeTopClose(). Fixed code below.

beforeTopClose = function () {
    var thisHta = {},
        thisHtaPath = lib.shell.currentDirectory + '\\index.hta', // Path to current HTA
        htas = new Enumerator(lib.wmiService.ExecQuery("Select * from Win32_Process Where name = 'mshta.exe'"));
    while (!htas.atEnd()) {
        if (htas.item().CommandLine.indexOf('_tools\\dlgbase') > -1) {
            htas.item().Terminate();
        }
        if (htas.item().CommandLine.indexOf(thisHtaPath) > -1) {
            thisHta = htas.item();
        }
        htas.moveNext();
    }
    thisHta.Terminate();
    return;
}

这篇关于如何从另一个HTA关闭HTA窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆