JNIでJFrameのHWNDを取得
Total: 10522
, Today: 3
, Yesterday: 1
Posted by aterai at
Last-modified:
概要
JNI Windowsでウィンドウハンドルを取得する - Memo of Masterの方法2を参考にして、JNI
(Java Native Interface
)でJFrame
(SunAwtFrame
)のHWND
(ウィンドウハンドル)を取得します。
環境
Microsoft Windows XP [Version 5.1.2600]
JDK 1.6.0_04
Microsoft Visual Studio .NET 2003\vc7
サンプルコード
import java.awt.*;
import javax.swing.*;
public class windowHandler{
public native static int getHWND(Object obj);
static {
System.loadLibrary("hwndTest");
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try{
//UIManager.getInstalledLookAndFeels();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
int hwnd = getHWND(frame);
frame.setTitle("HWND: 0x"+Integer.toHexString(hwnd).toUpperCase());
}
}
#include "windowHandler.h"
#include "jawt_md.h"
#include <windows.h>
JNIEXPORT jint JNICALL Java_windowHandler_getHWND(
JNIEnv *env, jobject jobj, jobject jframe) {
JAWT awt;
JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo* dsi;
JAWT_Win32DrawingSurfaceInfo* dsi_win;
jint lock;
HWND hWnd;
awt.version = JAWT_VERSION_1_4;
if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
return 0;
}
ds = awt.GetDrawingSurface(env, jframe);
if (ds == NULL) {
return 0;
}
lock = ds->Lock(ds);
if ((lock & JAWT_LOCK_ERROR) != 0) {
awt.FreeDrawingSurface(ds);
return 0;
}
dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi == NULL) {
ds->Unlock(ds);
awt.FreeDrawingSurface(ds);
return 0;
}
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
hWnd = dsi_win->hwnd;
/* Free the drawing surface info */
ds->FreeDrawingSurfaceInfo(dsi);
/* Unlock the drawing surface */
ds->Unlock(ds);
/* Free the drawing surface */
awt.FreeDrawingSurface(ds);
return (jint)hWnd;
}
解説
以下のようにコンパイル、実行するとJFrame
のタイルトバーにHWND
が表示されます。
@echo off
SETLOCAL
"%JAVA_HOME%\bin\javac" windowHandler.java
"%JAVA_HOME%\bin\javah" -jni windowHandler
set SDK=C:\Program Files\Microsoft Visual Studio .NET 2003\vc7
rem set SDK=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC
"%SDK%\bin\cl.exe" /c -I"%SDK%\include" -I"%SDK%\PlatformSDK\include" -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -IhwndTest.h hwndTest.c
set LIB=%SDK%\lib;%SDK%\PlatformSDK\lib
"%SDK%\bin\link.exe" /DLL hwndTest.obj "%JAVA_HOME%\lib\jawt.lib"
set Path=%Path%;%JAVA_HOME%\jre\bin
"%JAVA_HOME%\bin\java" windowHandler
JNI
を使用せず、以下のようにHWND
を取得する方法もあります。
- winapi - In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? - Stack Overflowに投稿されているJared MacD.さんのコードを引用
import java.awt.*;
import java.awt.peer.ComponentPeer;
import javax.swing.*;
import sun.awt.windows.WComponentPeer;
public class WComponentPeerTest {
public static long getHWnd(Frame f) {
ComponentPeer p = f.getPeer();
return p instanceof WComponentPeer ? ((WComponentPeer) p).getHWnd() : 0;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setTitle("HWnd: " + getHWnd(f));
}
}