Embed ActiveX controls inside Java GUI






4.78/5 (16 votes)
May 9, 2000

461709

867
With this your Java projects can take advantage of ActiveX controls and Office documents such as spreadsheets, charts, calendars, word processors, specialized graphics, and many more.
Introduction
This article provides a skeleton for embedding ActiveX controls within your Java Applications. The sample project shows how to embed the Internet Explorer 5.0 just like any other Java Widget.
There are two major problems that needed to be solved to be able to do this.
Problem #1 was solved by an undocumented API present in all versions of JDK's from Sun, for example JDK 1.1.8, JDK 1.2.2 and JDK 1.3. Here's the URL which explains how to get the HWND of a java.awt.Canvas object and an extract from the project which shows the implementation.
http://www.jguru.com/jguru/faq/view.jsp?EID=44507
// Returns the HWND for panel. This is a hack which works with // JDK1.1.8, JDK1.2.2 and JDK1.3. This is undocumented. Also // you should call this only after addNotify has been called. public int getHWND() { int hwnd = 0; DrawingSurfaceInfo drawingSurfaceInfo = ((DrawingSurface)(getPeer())).getDrawingSurfaceInfo(); if (null != drawingSurfaceInfo) { drawingSurfaceInfo.lock(); Win32DrawingSurface win32DrawingSurface = (Win32DrawingSurface)drawingSurfaceInfo.getSurface(); hwnd = win32DrawingSurface.getHWnd(); drawingSurfaceInfo.unlock(); } return hwnd; }
Problem #2 was a bit more complicated but MSDN's Online KB was of great help in solving this. Here's the URL which explains how to add an ActiveX control as a child of any HWND using ATL and an extract from the project which shows how the concept is implemented.
http://support.microsoft.com/support/kb/articles/Q192/5/60.ASP
// Creates IE control VOID CreateIEControl(ThreadParam *pThreadParam) { AtlAxWinInit(); printf("Create AtlAxWin Begin...[0x%x][%s]\n",pThreadParam->hwnd,pThreadParam->szURL); // In the 2nd Param you can use ProgID or UUID of any activex control. HWND hwndChild = ::CreateWindow("AtlAxWin", "Shell.Explorer.1", WS_CHILD|WS_VISIBLE, 0,0,0,0, pThreadParam->hwnd,NULL, ::GetModuleHandle(NULL), NULL); IUnknown *pUnk = NULL; AtlAxGetControl(hwndChild,&pUnk); printf("Create AtlAxWin Done...[0x%x]\n",pUnk); // get an interface to set the URL. CComPtrTo Build the Project edit and use Build.BAT present in the downloadable Zip file above.spBrowser; pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser); if (spBrowser) { CComVariant ve; CComVariant vurl(pThreadParam->szURL); #pragma warning(disable: 4310) // cast truncates constant value spBrowser->put_Visible(VARIANT_TRUE); #pragma warning(default: 4310) // cast truncates constant value spBrowser->Navigate2(&vurl, &ve, &ve, &ve, &ve); } }
To run the Java Application, use "java MyWindow http://www.codeproject.com" from the command line.
That's it! Have Fun.