⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mywindow.cpp

📁 很好很强大 提供很多类库
💻 CPP
字号:
#include <windows.h>
#include <assert.h>
#include <process.h>
#include "MyWindow.h"

// Includes for ATL
#pragma comment(lib,"atl.lib")
#include <atldef.h>
#define _ATL_DLL_IMPL
#include <atliface.h>
#include <atlbase.h>
#include <exdisp.h>

// Structure for Thread Parameters.
typedef struct {
	char szURL[1024];
	HWND hwnd;
} ThreadParam;

// Helper functions.
VOID CreateIEControl(ThreadParam *);
static void WINAPIV StartATL(LPVOID);

// native method for initializing the control.
JNIEXPORT void JNICALL Java_MyWindow_initialize
  (JNIEnv *pEnv, jobject, jint hwndIn, jstring string)
{
    // Fill up the params.
    const char *str	= pEnv->GetStringUTFChars(string, 0);
    ThreadParam *pThreadParam = new ThreadParam;
    pThreadParam->hwnd = (HWND) hwndIn;
    strcpy(pThreadParam->szURL,str);
    pEnv->ReleaseStringUTFChars(string, str);

    // Launch the Thread.
    _beginthread(StartATL, 0, pThreadParam);
}

// Thread for creating the control 
void WINAPIV StartATL(LPVOID lpVoid)
{
    ThreadParam *pThreadParam = (ThreadParam *)lpVoid;
    CreateIEControl(pThreadParam);
    delete pThreadParam;
    MSG msg;
    // Windows message loop.
    while(GetMessage(&msg, NULL, NULL, NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

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

// native method for handling resizes.
JNIEXPORT void JNICALL Java_MyWindow_resizeControl
  (JNIEnv *, jobject, jint hwndIn, jint width, jint height)
{
    HWND hwnd = (HWND) hwndIn;
    RECT rc;
    if(hwnd!=NULL)
    {
        ::GetWindowRect(hwnd,&rc);
        HWND hwndChild = GetWindow(hwnd, GW_CHILD);
        printf("got resize (0x%x,%d,%d)\n",hwndChild,width,height);
        ::SetWindowPos(hwndChild,NULL,0,0,rc.right-rc.left,rc.bottom-rc.top,SWP_NOZORDER|SWP_NOACTIVATE|SWP_SHOWWINDOW|SWP_NOMOVE);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -