📄 javaandactivex.txt
字号:
成功了 高兴中
先谢谢上面各位了
小嵩我用的是jni调用的c++作的嵌入了一个浏览器:)
把代码发上来大家分享
------------------------------------------------------
//MyWindow.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.peer.*;
import sun.awt.*;
public class MyWindow extends Canvas
{
static
{
// Load the library that contains the JNI code.
System.loadLibrary("MyWindow");
}
// native entry point for initializing the IE control.
public native static void initialize(int hwnd, String strURL);
// native entry point for resizing
public native static void resizeControl(int hwnd, int nWidth, int nHeight);
public native static int getNativeWindowHandle(Canvas canvas);
public void addNotify()
{
super.addNotify();
m_hWnd = getNativeWindowHandle(this);
initialize(m_hWnd, m_strURL);
}
String m_strURL = "http://www.163.com";
int m_hWnd = 0;
public static void main( String[] argv )
{
Frame f = new Frame();
f.setLayout(new BorderLayout());
f.setTitle("Internet Explorer inside Java Canvas");
MyWindow w = new MyWindow();
if(argv.length>0)
w.m_strURL = argv[0];
String strText = "URL:" + w.m_strURL;
f.add(w,BorderLayout.CENTER);
f.add(new Label(strText),BorderLayout.NORTH);
f.setBounds(300,300,500,300);
f.setVisible(true);
}
public void setSize( int width, int height )
{
super.setSize(width,height);
if(m_hWnd!=0)
resizeControl(m_hWnd, width, height);
}
public void setSize( Dimension d )
{
super.setSize(d);
if(m_hWnd!=0)
resizeControl(m_hWnd, d.width, d.height);
}
public void setBounds( int x, int y, int width, int height )
{
super.setBounds(x,y,width,height);
if(m_hWnd!=0)
resizeControl(m_hWnd, width, height);
}
public void setBounds( Rectangle r )
{
super.setBounds(r);
if(m_hWnd!=0)
resizeControl(m_hWnd, r.width, r.height);
}
}
--------------------------------------------------------------
将上面的文件编译得到MyWindow.class
再用javah MyWindow命令得到MyWindow.h头文件(是给c++用的)
剩下的事交给vc了
小嵩的vc是vc6.0中文版
建立一个dll的工程
文件-〉新建-〉Win32 Dynamic-Link Libiary
命名为MyWindow
建立一个.cpp文件 MyWindow.cpp
----------------------------------------------------------
//MyWindow.cpp
#include <jni.h>
#include <jawt.h>
#include <afxwin.h>
#include <windows.h>
#include "MyWindow.h"
#include "jawt_md.h"
//#include <assert.h>
#include <process.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);
JNIEXPORT jint JNICALL Java_MyWindow_getNativeWindowHandle
(JNIEnv *env, jobject jobj, jobject window)
{
JAWT awt;
awt.version = JAWT_VERSION_1_3;
jboolean result = JAWT_GetAWT(env, &awt);
if (result == JNI_FALSE)
return 0;
JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, window);
if (ds == 0)
return 0;
jint lock = ds->Lock(ds);
if ((lock & JAWT_LOCK_ERROR) != 0)
return 0;
JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi == 0)
return 0;
JAWT_Win32DrawingSurfaceInfo* dsiwin = (JAWT_Win32DrawingSurfaceInfo*) dsi->platformInfo;
jint ret = reinterpret_cast<jint>(dsiwin->hwnd);
ds->FreeDrawingSurfaceInfo(dsi);
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
return ret;
}
// 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",
"http://www.microsoft.com",
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);
}
}
----------------------------------------------------------
在编译之前有几件事情得先做一下
1.把刚才得到的MyWindow.h文件复制到工程中
2.把C:\Program Files\Java\j2sdk1.5.0\include和
C:\Program Files\Java\j2sdk1.5.0\include\win32下的文件复制到
C:\Program Files\Microsoft Visual Studio\VC98\Include下面
3.把C:\Program Files\Java\j2sdk1.5.0\lib下的jawt.lib复制到
C:\Program Files\Microsoft Visual Studio\VC98\Lib下
并在工程-〉设置-〉Link中添加jawt.lib
一切准备就绪按F7进行编译,于是在Debug中得到MyWindow.dll文件
将它复制到你编写的java 应用程序的文件夹中
好了运行MyWindow.class看看结果吧
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -