65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Java GUI 中嵌入 ActiveX 控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.78/5 (16投票s)

2000年5月9日

viewsIcon

464735

downloadIcon

867

通过此方法,您的 Java 项目可以利用 ActiveX 控制和 Office 文档,例如电子表格、图表、日历、文字处理器、专业图形等等。

  • 下载源代码和二进制文件 - 6.9 Kb
  • Sample Image - javacom.gif

    引言

    本文提供了一个在 Java 应用程序中嵌入 ActiveX 控制的框架。示例项目展示了如何像任何其他 Java 部件一样嵌入 Internet Explorer 5.0。

    要实现这一点,需要解决两个主要问题。

  • 问题 #1 - 获取 Java 重型组件的 HWND。
  • 问题 #2 - 如何将 ActiveX 控制附加为上述 HWND 的子窗口。
  • 问题 #1 通过 Sun 提供的所有 JDK 版本中都存在的一个未文档化的 API 来解决,例如 JDK 1.1.8、JDK 1.2.2 和 JDK 1.3。以下 URL 解释了如何获取 java.awt.Canvas 对象的 HWND,以及项目中的一个摘录,展示了该实现。

    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;
    }
    

    问题 #2 稍微复杂一些,但 MSDN 的在线知识库在解决这个问题方面提供了很大的帮助。以下 URL 解释了如何使用 ATL 将 ActiveX 控制作为任何 HWND 的子窗口添加,以及项目中的一个摘录,展示了如何实现该概念。

    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);
        }
    }
    
    要构建项目,请编辑并使用下载的 Zip 文件中提供的 Build.BAT。

    要运行 Java 应用程序,请从命令行使用 "java MyWindow https://codeproject.org.cn"。

    完成了!祝您玩得开心。

    © . All rights reserved.