【什么都懂一点,生活更多彩一些】Java 关闭显示器的几种方法【原创】
本文是楼主原创内容,如需转发,请注明原文出处,谢谢。1楼,JNA版本的示例程序,在WIN7下通过测试
优点:代码优雅、功能强大,让Java基本上能够和原生程序一样,非常简便的调用WindowsAPI
缺点:VC不能操作Java,在一些特定的情况下会有一些局限
ShutdownMonitor.java
程序代码:
/** * 使用Java Native Access(JNA)关闭显示器的示例程序 * * 您可能需要从【<span style="color: #008000; text-decoration: underline;">https://[/color]】 * 下载最新的【jna.jar】和【jna-platform.jar】来进行编译和运行 */ import static com.sun.jna.platform.win32.WinUser.*; public class ShutdownMonitor { public static void main(String[] args) { User32.INSTANCE.SendMessageW( HWND_BROADCAST, WM_SYSCOMMAND, User32.SC_MONITORPOWER, User32.MONITOR_OFF); } }
User32.java
程序代码:
import com.sun.jna.*; import com.sun.jna.platform.win32.WinDef.*; import com.sun.jna.win32.*; public interface User32 extends StdCallLibrary { User32 INSTANCE = (User32) Native.loadLibrary("User32", User32.class); final WPARAM SC_MONITORPOWER = new WPARAM(0xF170); final LPARAM MONITOR_ON = new LPARAM(-1); // the display is powering on final LPARAM MONITOR_STANBY = new LPARAM(1); // the display is being shut off final LPARAM MONITOR_OFF = new LPARAM(2); // the display is going to low power LRESULT SendMessageA(final HWND hWnd, final int msg, final WPARAM wParam, final LPARAM lParam); LRESULT SendMessageW(final HWND hWnd, final int msg, final WPARAM wParam, final LPARAM lParam); }