JNA - 如何调用系统功能? (旋转,屏幕关闭)

时间:2013-12-25 18:29:54

标签: java jna

我是JNA接口(?库?)的新手 - 但我看到一些系统功能只能通过JNA在Java中访问。我想做一些显示器,即旋转和关闭监视器。对于这两个函数,我必须调用一些系统函数 - 对于关闭监视器,我找到link以及旋转link。但说实话,我并不知道如何从其中一个开始。

有人可以提出如何启动resp的想法。如何实施?

1 个答案:

答案 0 :(得分:2)

好的,我不得不开启和关闭显示器,它有效!太酷了!

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;

public class TurnOffMonitor {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int SC_MONITORPOWER = 0xF170;
      int SC_MONITOR_OFF = 2;
      int SC_MONITOR_ON = -1;

      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
            LPARAM paramLPARAM);

      LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
            LPARAM paramLPARAM);
   }

   private static final long SLEEP_TIME = 4 * 1000; // 4 seconds

   public static void main(String[] args) {
      final User32 user32 = User32.INSTANCE;
      System.out.println("Foo");

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_OFF));

      try {
         Thread.sleep(SLEEP_TIME);
      } catch (InterruptedException e) {}

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_ON));

   }
}