欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JNI-C

程序员文章站 2022-03-10 15:10:01
微软官网文档 API: https://docs.microsoft.com/en-us/windows/desktop/api/index 窗口操作API:https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ 窗口操作API详解 ......

微软官网文档

api: 

窗口操作api:

  窗口操作api详解:

    ①setwindowpos函数详解: 

 

数据类型对应关系及互相转换方式

  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 }

 

注意:以上功能块中为主要代码,并不是完整版代码