Click here to Skip to main content
15,896,497 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 448.8K   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

     
    GeneralRe: Can this be done on Mac OS X? Pin
    slwkf117-Jun-03 16:29
    slwkf117-Jun-03 16:29 
    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 
    Hi Peter

    I'm not quite sure that I understand your question.
    When you override the paint method of your canvas your can extract the hwnd from the
    platform dependendent drawingsurface, in windows (jawt_Win32DrawingSurfaceInfo* dsi_win) from that you extract the hwnd (dsi_win->hwnd).

    When you have the hwnd you can use the ATL method described in this example to draw the IE activex component on to your hwnd.

    The result of this code is that the canvas will ask the IE to redraw itself every time the paint method is invoked. This might however not be the desired result since you don’t want the activex component to be reinitialized every time the canvas is redrawn. This is in particular true since the IE Activex component handles it's own drawing and updating.

    An easy workaround for this problem is to initiate a public bool variable for example
    bool isFirstTime=true;


    when you have done your initialization you set that variable to false and the next time paint is invoked you wont reinitialize the component.

    There are however one more issue to consider. If you resize the canvas you must notify the activex component so it will resize as well. This could be done by calling the resize function in this projects example every time the paint method is invoked.

    Hope this helps
    /Peter

    GeneralRe: How to get Hwnd with jdk 1.4 Pin
    brighty5-Jun-03 4:48
    brighty5-Jun-03 4:48 
    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 

    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.