Click here to Skip to main content
15,900,511 members
Articles / Desktop Programming / ATL
Article

Embed ActiveX controls inside Java GUI

Rate me:
Please Sign up or sign in to vote.
4.78/5 (18 votes)
8 May 2000 449.4K   859   39   106
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.
  • Download source and binary files - 6.9 Kb
  • Sample Image - javacom.gif

    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 - Get the HWND of a Java Heavy Weight component.
  • Problem #2 - How to attach an activex control as a child of the HWND above.
  • 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.
        CComPtr<IWebBrowser2> 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 Build the Project edit and use Build.BAT present in the downloadable Zip file above.

    To run the Java Application, use "java MyWindow http://www.codeproject.com" from the command line.

    That's it! Have Fun.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Generalembedding on a Pocket PC Pin
    marad2-May-03 5:09
    marad2-May-03 5:09 
    GeneralRe: embedding on a Pocket PC Pin
    rubao19-Nov-03 0:18
    rubao19-Nov-03 0:18 
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveX Pin
    rizoriz16-Apr-03 11:45
    rizoriz16-Apr-03 11:45 
    QuestionHow to get Hwnd with jdk 1.4 Pin
    Anonymous13-Apr-03 21:59
    Anonymous13-Apr-03 21:59 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    gusp14-Apr-03 5:39
    gusp14-Apr-03 5:39 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty2-Jun-03 1:36
    brighty2-Jun-03 1:36 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    gusp3-Jun-03 0:13
    gusp3-Jun-03 0:13 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty5-Jun-03 4:48
    brighty5-Jun-03 4:48 
    Hi gusp,

    thanx so much for your help. I think now i understand you better. But for me it is very strange to get the window handle of the canvas by overriding its paint() method and making it native. This is really nice if one really wants to "paint", but let's take a look again to the example that uses the sun.awt.SunToolkit.getNativeWindowHandleFromComponent(this) method in order to get the window handle of the canvas.

    This example passes the window handle of the canvas - retrieved as mentioned - to the initialize() method which is native. This method passes the window handle as a thread-parameter to _beginthread() which starts a thread whose function creates a native window that is the IE browser with the passed window handle from the canvas as it's parent window. Like this, the IE browser appears in the canvas and position as well as resizing is kept intact by the use of the resizeControl() function which gets called from the overridden setSize() member of the canvas. There does not appear any paint() method.

    Now if i understood you right, with jdk 1.4.1 there is no such getNativeWindowHandleFromComponent(this) method so the window handle of the canvas must get retrieved by overriding the paint() method of the canvas, making it native, then getting it by extracting it from dsi_win->hwnd. Is that correct? So i would get the window handle the first time the paint method gets called, but would get it again and again each time paint() gets called. So of course i need something like you've mentioned, a bool isFirstTime var that i need in order to create the thread that creates the native window with the IE browser in it just once. Did i understand this correct? Because the canvas' setSize() methods would not have a window handle i must call the resizeControl() native method from within the paint() method, right? This assumes that each resize of the canvas does really call the paint() method, right? If this is not the case, i could use the canvas' setSize() method still and call repaint() in order to inderctly call paint(), right? Good idea? Maybe there will be a flickering if i do so?! I am not sure. I should try it out.

    My code currently does not work proper, i don't know why. With the first call to CreateIEControl() which does a pUnk->QueryInterface(IID_IWebBrowser2, (void**)&spBrowser) i have saved spBrowser in a static variable. I have so reused spBrowser in other functions that just call spBrowser->Navigate2() which worked always fine. But now spBrowser is null somehow. This is very strange.

    Any help highly appreciated.

    Peter

    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty5-Jun-03 23:16
    brighty5-Jun-03 23:16 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    TorgeirV23-Jul-03 1:23
    sussTorgeirV23-Jul-03 1:23 
    QuestionRe: How to get Hwnd with jdk 1.4 Pin
    conquestkid7-Aug-06 11:17
    conquestkid7-Aug-06 11:17 
    AnswerRe: How to get Hwnd with jdk 1.4 Pin
    conquestkid18-Aug-06 4:09
    conquestkid18-Aug-06 4:09 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    James Duy Trinh (VietDoor)19-Jan-05 16:09
    James Duy Trinh (VietDoor)19-Jan-05 16:09 
    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    Txarli6-Jun-05 5:39
    Txarli6-Jun-05 5:39 
    GeneralExperience with embedding IE Pin
    brighty10-Apr-03 3:19
    brighty10-Apr-03 3:19 
    GeneralRe: Experience with embedding IE Pin
    Anonymous12-Apr-03 19:13
    Anonymous12-Apr-03 19:13 
    GeneralRe: Experience with embedding IE Pin
    brighty15-Apr-03 1:44
    brighty15-Apr-03 1:44 
    GeneralRe: Experience with embedding IE Pin
    Member 4599051-Jul-03 23:07
    Member 4599051-Jul-03 23:07 
    Generalnjawin Pin
    sjava31-Mar-03 16:31
    sjava31-Mar-03 16:31 
    GeneralRe: njawin Pin
    brighty5-Jun-03 22:53
    brighty5-Jun-03 22:53 
    GeneralRe: njawin Pin
    sjava6-Jun-03 17:54
    sjava6-Jun-03 17:54 
    GeneralCasting error Pin
    Member 16812927-Jan-03 19:38
    Member 16812927-Jan-03 19:38 
    GeneralEmbed Excel Pin
    Anonymous14-Nov-02 19:57
    Anonymous14-Nov-02 19:57 
    GeneralRe: Embed Excel Pin
    Anonymous10-Jul-03 1:32
    Anonymous10-Jul-03 1:32 
    Generalerror on win32drawingsurface Pin
    vinothlee14-Nov-02 7:08
    vinothlee14-Nov-02 7:08 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.