This Java code sample uses the
SHDocVw.ShellWindows object to iterate the running instances of Internet Explorer. The code obtains an
IWebBrowser2 interface object for each instance of Internet Explorer and compares the
LocationName property to determine if the necessary instance of Internet Explorer is present. The following code searches for a
LocationName of Page1.htm.
To build this application, follow these steps:
- Create a Visual J++ 6.0 Console Application project.
- Replace the default Class1.java file in the Visual J++ Console Application project with the following code:
import shdocvw.*;
import com.ms.com.Variant;
public class Class1
{
public static void main (String[] args)
{
//From Q176792
ShellWindows sw = new ShellWindows();
int nCount = sw.getCount();
for(int i=0; i<nCount ; i++) {
Object spDisp = sw.Item(new Variant(i));
// Verify that the returned object supports the IWebBrowser2 interface.
// This, in effect, calls a COM QueryInterface.
if( spDisp instanceof IWebBrowser2 )
{
IWebBrowser2 iwb = (IWebBrowser2)spDisp;
//Q195175 How To Sending Text to the Visual J++ Output window.
com.ms.debug.Debugger.out.println(iwb.getFullName());
com.ms.debug.Debugger.out.println(iwb.getLocationName());
com.ms.debug.Debugger.out.println("");
String s = iwb.getLocationName();
if(s.equals("Page1.htm")) {
iwb.setAddressBar(false);
iwb.setMenuBar(false);
iwb.setToolBar(0);
iwb.setStatusBar(false);
}
}
}
}
}
- On the Project menu, click Add COM Wrapper to add a Component Object Model (COM) wrapper for the Internet Explorer shdocvw object.
- In the COM Wrappers dialog box, click Microsoft Internet Controls from the drop-down list box, or, alternatively, click Browse and then select Shdocvw.dll from the Microsoft Windows SYSTEM32 directory.
This step generates a shdocvw package in your Visual J++ project that contains the Java wrapper classes for the Internet Controls COM object. - Compile and build your Java application.
- The Java application searches for a page named Page1.htm. To create this Hypertext Markup Language (HTML) page in Visual J++, click Project on the Add Web Page menu.
- In the Add Item dialog box, click Web Page, and then click Page. Make sure that the name is set to Page1.htm, and then click Open.
This creates a new Page1.htm file in your project. - In a File Explorer window that is external to Visual J++, double-click Page1.htm to start Internet Explorer.
- After Internet Explorer is running with Page1.htm, run the Java console application. Notice that while the Java code sets properties for the instance of Internet Explorer that is running Page1.htm, the Address bar, Menu bar, Status bar, and Toolbars disappear.