JNI-C

敏晨發表於2018-10-15

微軟官網文件

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 = &rect;
 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 = &rect;
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 = &rect;
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 }

 

注意:以上功能塊中為主要程式碼,並不是完整版程式碼