Conversion of Java component and Win32 window handle

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.