Share this page 

Make a Window "stay on top"Tag(s): JNI


First you need the handle of the Window. Call this JNI function with Window Title.
JNIEXPORT jint JNICALL Java_JavaHowTo_getHwnd
     (JNIEnv *env, jclass obj, jstring title){
 HWND hwnd = NULL;
 const char *str = NULL;

 str = (*env)->GetStringUTFChars(env, title, 0);
 hwnd = FindWindow(NULL,str);
 (*env)->ReleaseStringUTFChars(env, title, str);
 return (jint) hwnd;
 }
Then you pass the handle to this function
JNIEXPORT void JNICALL Java_JavaHowTo_setWindowAlwaysOnTop
    (JNIEnv *env, jclass obj, jint hwnd, jboolean flag){
 if (flag)
    SetWindowPos((HWND) hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
 else
    SetWindowPos((HWND) hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
 return;
}