• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JNIでJFrameのHWNDを取得
#navi(../)
RIGHT:Posted by [[aterai]] at 2008-09-04(([[コメント欄>Comments/Swing]]から移動))
*JNIでJFrameのHWNDを取得 [#ibeb10df]
#adsense2

- [http://terai.xrea.jp/data/swing/hwndTest.zip hwndTest.zip(サンプル)]

//#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TUKZTxK0pwI/AAAAAAAAAz4/qohiEl4CJss/s800/JAVA_HOME.png)
#ref(https://lh3.googleusercontent.com/-NNjiMJe_o94/TeSO493_q3I/AAAAAAAAA8k/KU8B7ms4SQE/s800/JNI_HWND.png)

**概要 [#d54756f6]
[http://www.telejapan.com/af/master/memo/memo_Java.html JNI Windowsでウインドウハンドラを取得する - Memo of Master]の方法2を参考にして、JNI(Java Native Interface) で JFrame(SunAwtFrame) の HWND(ウィンドウハンドル) を取得します。

**環境 [#q9fd6c07]
- Microsoft Windows XP [Version 5.1.2600]
- JDK 1.6.0_04
- Microsoft Visual Studio .NET 2003\vc7

**サンプルコード [#yfef3d00]
#code{{
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() {
      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());
  }
}
}}

#code{{
#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;
}
}}

**解説 [#gcd89e73]
以下のようにコンパイル、実行するとフレームのタイルトバーにHWNDが表示されます。

#code{{
@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
"%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
}}

**参考リンク [#z82fc32a]
- [http://www.telejapan.com/af/master/memo/memo_Java.html JNI Windowsでウインドウハンドラを取得する - Memo of Master]

**コメント [#h2768609]
#comment