note 50173 deleted from class.com by nlopess
| From: | nlopess@php.net | Date: | Tue, 22 Feb 2005 11:14:01 +0000 |
| Subject: | note 50173 deleted from class.com by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-85363@lists.php.net to get a copy of this message | ||
Note Submitter: csaba at alum dot mit dot edu
----
Intermediate Windows IO
If you are using a command line (CLI) version of PHP on a Win32 platform (e.g. XP Pro, SP2), you
might want to have output directed to IE (perhaps you'll want to work with the output there)
when Popup does not suffice (see my earlier post, below).
It's easy enough to get an instance of IE using $ie = new
COM("InternetExplorer.Application");. The problem is, you get a minimized instance so
it's difficult to get it to the foreground without having to resort to Win32 APIs, which PHP is
not currently supporting.
The code below (watch line wrapping) shows how to get an instance of Internet Explorer to the
foreground, stuff it with HTML, have an onLoad function which won't suspend PHP, and then be
able to (simply) dismiss IE on demand.
<?php
// Shows how to activate AND DISPLAY IE from (command line) PHP without Win APIs
$oWSH = new COM("Wscript.Shell");
$oShellApp = new COM("Shell.Application");
// Inventory open Windows Explorer windows
checkWindows($aHandle=array());
// open a NEW, ACTIVATED (non minimized) Windows Explorer pointing to My Documents
$oExec = $oWSH->Exec("explorer");
// reinventory Explorer windows, removing previously inventoried ones
$newWndCnt = checkWindows($aHandle, false);
if ($newWndCnt!=1) die($oWSH->Popup ("Hey! We don't only have 1 new window",
0,"IE Display Error",48));
// this is what we plan to display
// setTimeout used else PHP stalls waiting for IE to finish loading
$onload = "\"window.setTimeout('alert("Ready");";
$onload .= "document.title="Close Button"',0)\"";
$onClick = "\"alert('Goodbye');self.close()\""; // How to close the
(IE) window
$button = "<button type=button onclick=$onClick
accessKey=C><u>C</u>lose</button>";
$bodyHTML = "<body
onload=$onload>"Hello"<br>$button</body>";
$html = "<html><head><title>PHP
testing</title></head>$bodyHTML</html>";
// next line rids all but title bar (following line nicer), body, and status bar
$ie->Toolbar = false;
//$ie->MenuBar = false; // only rids directory tree to left and menu bar
$ie->Navigate("about:blank"); // else $doc->Open() fails
$doc = $ie->document;
$doc->ParentWindow->opener = "me"; // avoids confirm message when closing
$doc->Open();
$doc->Write($html); // do the nasty
$doc->Close();
function checkWindows ($aPassed, $addP=true) {
// iterate through open Explorers counting only those
// which have My Documents as their location
$oWins = $GLOBALS["oShellApp"]->Windows();
$count = 0;
foreach ($oWins as $oWin) {
$oDoc = $oWin->Document;
$typename = typeName($oDoc); // next line eliminates IE instances
if (substr(typeName($oDoc),0,20)!="IShellFolderViewDual") continue; // There may be other
suffixes
// next line possibly affected by registry setting for how to display names
if ($oWin->LocationName<>"My Documents") continue; // next line: weeding out
phase ($addP false)
if (array_key_exists($oWin->hwnd,$aPassed)) {unset($aPassed[$oWin->hwnd]);continue;}
if ($addP) $aOld[$oWin->hwnd] = $oWin->LocationURL; // accumulation phase ($addP true)
else {$count++; $GLOBALS["ie"] = $oWin; } // count those not weeded out
}
return $count;
}
?>
The code above requires the typeName function, which I posted earlier to http://php.net/com_print_typeinfo
Csaba Gabor from Vienna