Embed ActiveX controls inside Java GUI

来源:互联网 发布:中国男女收入差距数据 编辑:程序博客网 时间:2024/05/21 10:03

From: http://www.codeproject.com/java/javacom.asp

  • 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

    Collapse
    // Creates IE controlVOID 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.

  • Davanum Srinivas


    Click here to view Davanum Srinivas's online profile.


    Other popular Java Programming articles:

    • Executing commands on a remote machine - Part 1
      A simple tool that allows you to control other machines from your program
    • SOCKS Proxy + SSL Tunnel
      A full featured SOCKS 4 & 5 proxy server written in Java.
    • Talk2Me: Java instant messenger
      A near to perfect clone of the very popular Yahoo messenger. Purely written in Java and SWING, this messenger also uses the capabilities of JNI and JAWT to provide some features.
    • Add Mouse Wheel support to Swing Widgets
      This article shows how to add support for Mouse Wheel for Java Swing Widgets