私はJNAの初心者です。最小化されたものを含むすべてのウィンドウのハンドルを取得しようとしています。HWND
すべての窓が必要です。私は「ウィンドウ:表示されているすべてのウィンドウのリストを取得する方法」という質問をしました。ウィンドウのリストを取得するのに役立ちましたが、hWnd
型はintです。タイプcom.sun.jna.platform.win32.User32
を要求する関数では使用できません。それで、intポインタではなくタイプのすべてのウィンドウハンドルを取得する方法はありますか?最後に、なぜ違いがあると?両方をどのように受け入れますか?私は少し混乱しています。ありがとう。hWnd
com.sun.jna.platform.win32.WinDef.HWND
com.sun.jna.platform.win32.WinDef.HWND
int
HWND
私は次のコードを持っています(Hovercreftの回答から編集):
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
public class TryWithHWND {
public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
public boolean callback(HWND hWnd, Pointer arg1) {
char[] windowText = new char[512];
user32.GetWindowText(hWnd, windowText, 512);
String wText = Native.toString(windowText);
RECT rectangle = new RECT();
user32.GetWindowRect(hWnd, rectangle);
// get rid of this if block if you want all windows regardless
// of whether
// or not they have text
// second condition is for visible and non minimised windows
if (wText.isEmpty() || !(User32.INSTANCE.IsWindowVisible(hWnd)
&& rectangle.left > -32000)) {
return true;
}
System.out.println("Found window with text " + hWnd
+ ", total " + ++count + " Text: " + wText);
return true;
}
}, null);
}
}
デフォルトUser32
クラスのみ(カスタムインターフェイスではなく)を使用しようとしました。正常に動作しています。既存のインターフェイスの代わりにユーザー定義のインターフェイスを使用しているのはなぜですか?もう1つ、ユーザー定義のメソッドシグネチャと既存のメソッドシグネチャには常に違いがあります。たとえば、変数windowText
はですがchar[]
、ホバークラフトの変数は型byte[]
です。誰かが私を説明できますか?ありがとう。
JNAの最新バージョンには、これを修正する必要のあるいくつかの変更が加えられています(JNAの作成者の1人であるLuke Quinaneがここで述べています)。最新バージョンを使用してJNA APIを確認すると、WinUser.WNDENUMPROCインターフェイスのメソッドが実際にはlongまたはintではなく、WinDef.HWNDをパラメーターとして使用していることがわかります。
例えば:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;
public class TryWithHWND {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
}
public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
// get rid of this if block if you want all windows regardless of whether
// or not they have text
if (wText.isEmpty()) {
return true;
}
System.out.println("Found window with text " + hWnd + ", total " + ++count
+ " Text: " + wText);
return true;
}
}, null);
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加