For Win32, Windows handle hwnd is a critical resource, and many operations require hwnd as a parameter. In Java, the concept of hwnd is absent, and all operations are completed by calling the component method. If we can obtain the corresponding Win32 hwnd based on component, it means that some operations on Java component can be completed in Windows mode. This can be achieved through the JNI interface of Java.
JNI's jawt. h file is required to implement this function. The sample code is as follows:
Jniexport jint jnicall java_getjavacomponent_util_get1_whandle (jnienv * ENV, jclass, jobject jocomponent)
{
Jawt AWT;
Jawt_drawingsurface * Ds;
Jawt_drawingsurfaceinfo * DSI;
Jawt_win32drawingsurfaceinfo * dsi_win;
Jboolean result;
Jint lock;
Jint hwnd =-1;
// Get the AWT
AWT. Version = jawt_version_1_3;
Result = jawt_getawt (ENV, & AWT );
Assert (result! = Jni_false );
// Get the drawing surface
DS = AWT. getdrawingsurface (ENV, jocomponent );
Assert (Ds! = NULL );
// Lock the drawing surface
Lock = ds-> lock (DS );
Assert (Lock & jawt_lock_error) = 0 );
// Get the drawing surface info
DSI = ds-> getdrawingsurfaceinfo (DS );
// Get the platform-specific drawing info
Dsi_win = (jawt_win32drawingsurfaceinfo *) DSI-> platforminfo;
Hwnd = (jint) (dsi_win-> hwnd );
// Free the drawing surface info
DS-> freedrawingsurfaceinfo (DSI );
// Unlock the drawing surface
DS-> unlock (DS );
// Free the drawing surface
AWT. freedrawingsurface (DS );
Return hwnd;
}
Another method jobject (jnicall * getcomponent) (jnienv * ENV, void * platforminfo) provided by jawt. h can help us get the Java component object through hwnd.
It is worth noting that these operations are only useful for Java heavyweight component.