JNI-C
微软官网文档
API: https://docs.microsoft.com/en-us/windows/desktop/api/index
窗口操作API:https://docs.microsoft.com/en-us/windows/desktop/api/winuser/
窗口操作API详解:
①SetWindowPos函数详解: https://blog.csdn.net/qq_23992597/article/details/54409223?utm_source=blogxgwz0
数据类型对应关系及互相转换方式
HWND handle1 jlong handle2 long handle3
HWND handle1 = (HWND)handle2;
jlong handle2 = (jlong)handle1;
Windows 数据类型
所有的 Windows 数据类型都是由 C 数据类型经过类型重定义得到的
1.句柄
HWND hWnd
2.
LPSTR
LPSTR window_title = new char[2048];
LPWSTR
LPWSTR window_title = new wchar_t[2048];
功能块
1.关于windows中正在使用的窗口的标题(32位运行环境)
1 #include "tool_WindowInterface.h" 2 #include "jni.h" 3 #include <Windows.h> 4 5 int getLength(LPWSTR pat){ 6 int i=0; 7 while(true) 8 { 9 if(pat[i] == NULL){ 10 break; 11 } 12 i = i + 1; 13 } 14 return i; 15 } 16 17 jstring stoJstring(JNIEnv* env, const LPWSTR pat){ 18 jclass strClass = env->FindClass("Ljava/lang/String;"); 19 jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); 20 jbyteArray bytes = env->NewByteArray(getLength(pat)); 21 env->SetByteArrayRegion(bytes, 0, getLength(pat), (jbyte*)pat); 22 jstring encoding = env->NewStringUTF("utf-8"); 23 return (jstring)env->NewObject(strClass, ctorID, bytes, encoding); 24 } 25 26 JNIEXPORT jstring JNICALL Java_tool_WindowInterface_getCurWinTitle(JNIEnv *env, jobject){ 27 HWND foreground = GetForegroundWindow(); 28 LPWSTR window_title = new wchar_t[1024]; 29 if (foreground){ 30 GetWindowText(foreground, window_title, 1024); 31 } 32 return stoJstring(env, window_title); 33 }
2.获取windows中当前窗口的位置,及宽高
1 #include <Windows.h> 2 3 HWND foreground = GetForegroundWindow(); 4 RECT rect; 5 LPRECT lpRect = ▭ 6 GetWindowRect(foreground, lpRect); 7 //位置 8 printf("%d ", lpRect->left); 9 printf("%d ", lpRect->top); 10 //宽高 11 printf("%d ", lpRect->right - lpRect->left); 12 printf("%d ", lpRect->bottom - lpRect->top);
3.显示指定窗口
1 JNIEXPORT void JNICALL Java_tool_WindowInterface_showWindow(JNIEnv *, jobject, jlong handle){ 2 3 RECT rect; 4 LPRECT lpRect = ▭ 5 HWND foreground = (HWND)handle; 6 GetWindowRect(foreground, lpRect); 7 SetWindowPos(foreground, HWND_TOPMOST, lpRect->left, lpRect->top, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top, SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW); 8 SetForegroundWindow(foreground); 9 }
4.隐藏指定窗口
1 JNIEXPORT void JNICALL Java_tool_WindowInterface_hideWindow(JNIEnv *, jobject, jlong handle){ 2 3 RECT rect; 4 LPRECT lpRect = ▭ 5 HWND foreground = (HWND)handle; 6 GetWindowRect(foreground, lpRect); 7 SetWindowPos(foreground, HWND_BOTTOM, lpRect->left, lpRect->top, lpRect->right - lpRect->left, lpRect->bottom - lpRect->top, SWP_NOSIZE|SWP_NOMOVE|SWP_HIDEWINDOW); 8 }
注意:以上功能块中为主要代码,并不是完整版代码