当前位置: 技术问答>java相关
如何使一个frame始终显示在最前面?
来源: 互联网 发布时间:2015-03-04
本文导语: 如何使一个窗口始终显示在最前面? | int w = Toolkit.getDefaultToolkit().getScreenSize().width ; int h = Toolkit.getDefaultToolkit().getScreenSize().height; this.setSize(w,h); | Make a Window "stay on top"...
如何使一个窗口始终显示在最前面?
|
int w = Toolkit.getDefaultToolkit().getScreenSize().width ;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(w,h);
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setSize(w,h);
|
Make a Window "stay on top"
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;
}
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;
}