2012-12-26 44 views
0

我試圖實現我自己的模態對話框(Why?)。我打開使用WScript.Shell一個新窗口:如何從另一個HTA關閉HTA窗口?

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

上面的行創建了一個「半模式」 HTA窗口。腳本執行停止在這一行,但主窗口仍然響應。在onfocusin事件處理程序中處理此問題,該處理程序使用shell.AppActivate()將焦點返回到新的HTA窗口。這甚至可以防止「關閉窗口」按鈕關閉主窗口。但是,在Windows7中,有一個後門用於關閉主窗口,Windows任務欄中的圖標。

如果主窗口關閉,還應該關閉體面的模式對話框。在這種情況下,它並未關閉,因爲打開的HTA窗口不是主要的真實子窗口。

我可以在下面的主窗口的onbeforeunload處理函數中關閉這個對話模擬器。

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任務管理器進程選項卡時,我可以看到主窗口的進程仍在運行。

爲什麼主窗口仍在運行?它是否在等待shell-process的結尾?我應該怎麼做onbeforeunload處理程序完全關閉主窗口?如果我需要不同的方法,請告訴我。

回答

1

我剛剛意識到,我可以在beforeTopClose()中終止主窗口。下面的固定代碼。

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; 
}