5,662,937 members and growing! (297 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Cross Platform » General     Intermediate

Embed ActiveX controls inside Java GUI

By Davanum Srinivas

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.
Java, VC6, C++, Java, Windows, NT4, Win2K, Visual Studio, ATL, Dev

Posted: 8 May 2000
Updated: 8 May 2000
Views: 270,753
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
56 votes for this Article.
Popularity: 7.58 Rating: 4.33 out of 5
2 votes, 11.8%
1
0 votes, 0.0%
2
2 votes, 11.8%
3
0 votes, 0.0%
4
13 votes, 76.5%
5
  • 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 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

    About the Author

    Davanum Srinivas



    Location: United States United States

    Other popular Cross Platform articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 25 of 105 (Total in Forum: 105) (Refresh)FirstPrevNext
    GeneralReverse - JAva applet in cmemberpsychospiller3:22 14 Dec '06  
    GeneralEmbed ActiveX controls inside Java GUImemberShibaev Valera16:03 26 Nov '06  
    Generalinitialize() method call crashesmemberndenkha15:09 16 May '06  
    GeneralCan't compile and build.memberndenkha9:55 16 May '06  
    GeneralProblem with not showingmembernameR223:00 8 Aug '05  
    GeneralUsing C++membercberam1:50 12 Oct '04  
    GeneralNewJawin Source CodememberArunsl21:35 1 Aug '04  
    GeneralIECanvas and JPanelmembergigigigigi3:35 21 Jun '04  
    GeneralIECanvas Project Based on This ArticlesussAnonymous10:03 24 May '04  
    GeneralJava Applet in page: ERRORmemberradim.keseg4:23 24 May '04  
    GeneralTab key doesn't workmemberterryphillips1:39 14 May '04  
    Generalcan we use other light weight class instead of Canvas?sussAnonymous13:02 10 Sep '03  
    Generalunresolved externalmemberi_is_mike0:12 23 Jul '03  
    GeneralRe: unresolved externalmemberRobinYang21:54 14 Nov '05  
    GeneralOther functionality of IEmembersachinvshah3:30 15 Jul '03  
    GeneralUnsatisfiedLinkErrormemberVipinTyagi23:00 13 Jul '03  
    GeneralError when exiting from Javamemberbrighty5:13 27 Jun '03  
    GeneralCan this be done on Mac OS X?memberslwkf18:14 12 Jun '03  
    GeneralRe: Can this be done on Mac OS X?sussMac Guy12:00 17 Jun '03  
    GeneralRe: Can this be done on Mac OS X?memberslwkf117:29 17 Jun '03  
    Generalembedding on a Pocket PCmembermarad6:09 2 May '03  
    GeneralRe: embedding on a Pocket PCmemberrubao1:18 19 Nov '03  
    GeneralA problem of Dependencies is caused when deploying the project in VB .NET while application containing a third party ActiveXsussRiz Ali12:45 16 Apr '03  
    GeneralHow to get Hwnd with jdk 1.4sussAnonymous22:59 13 Apr '03  
    GeneralRe: How to get Hwnd with jdk 1.4membergusp6:39 14 Apr '03  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 8 May 2000
    Editor: Chris Maunder
    Copyright 2000 by Davanum Srinivas
    Everything else Copyright © CodeProject, 1999-2008
    Java | Advertise on the Code Project