还真没听说过有命令行可以关闭显示器的,也没有听说过有编程能够关闭显示器的,我倒是觉得你 调用一个 bat 让一个黑色屏保运行起来更现实。现实中编程关闭显示器个人觉得这个功能因为有安全隐藏操作不应该支持,比如:假设现在某个功能会有一个确认过程并且会留下一些日志,要是木马(它本身不是驱动程序的话)却能够在某个瞬间让显示器闪一下让用户没有意识到这个问题,岂不是纵容了它?基本上类似的功能只应该是驱动程序有什么功能达到变通的可能性,而不是直接关闭显示器。就像我们让 Windows 关机时也没听说过 Windows 能同时关闭显示器的案例。
JNA版的示例程序 如果第一次接触JNA,请认真阅读注释,下载相应的jar包来编译和运行 在WIN7下通过测试ShutdownMonitor.java /** * 使用Java Native Access(JNA)关闭显示器的示例程序 * * 您可能需要从【https://github.com/twall/jna】 * 下载最新的【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); }
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().maxMemory());
try {
Process pro=Runtime.getRuntime().exec("E:\\testjava\\test.bat");
Thread.sleep(4000);
pro.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里exec参数可以给.exe绝对路径也可以给系统默认路径下的可执行文件 你在.bat文件中写入shutdown就ok了,建议你先试着掉用应用程序
如果第一次接触JNA,请认真阅读注释,下载相应的jar包来编译和运行
在WIN7下通过测试ShutdownMonitor.java
/**
* 使用Java Native Access(JNA)关闭显示器的示例程序
*
* 您可能需要从【https://github.com/twall/jna】
* 下载最新的【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);
}
用VC生成的ShutdownMonitor.dll放到C:\Windows目錄下,在Eclipse下通过测试ShutdownMonitor.java/**
* 使用Java Native Interface(JNI)关闭显示器的示例程序
* 优点:不需要【jna.jar】和【jna-platform.jar】
* 缺点:需要自己额外做个DLL
*
* Java编译方法:【javac ShutdownMonitor.java】
* 头文件制作方法:【javah -jni ShutdownMonitor】
* 如果编译时有错误,可以考虑把中文注释都删除掉再试一下
* 如果运行时有错误,需要确认一下JDK和SDK是否匹配【必须同时为32位或者64位的】
*/public class ShutdownMonitor { static {
try {
System.loadLibrary("ShutdownMonitor");
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
} private static native void shutdownMonitor(); public static void main(String[] args) {
try {
shutdownMonitor();
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
}
}
ShutdownMonitor.cpp
#include "stdafx.h"
#include "ShutdownMonitor.h"JNIEXPORT void JNICALL Java_ShutdownMonitor_shutdownMonitor(JNIEnv *, jclass) {
SendMessage (HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}ShutdownMonitor.h是用javah命令生成的
ShutdownMonitor .javaimport java.io.IOException;public class ShutdownMonitor {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Windows\\ShutdownMonitor.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}ShutdownMonitor.cpp#include <windows.h>void main(int argc, wchar_t* argv[])
{
SendMessage (HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}