How do you turn on and off a monitor from within a Java application?

老子叫甜甜 提交于 2019-11-26 21:01:49

问题


How do you turn on and off a monitor from within a Java application?

In case you're wondering why, this is a kiosk style application where it would be great to turn off the monitors at night. Yes you can do it within the screensaver settings of the machine, but it would be great to do it programatically and avoid having to configure it on each machine.


回答1:


Assuming you deploy your Java Applications on Windows, you can use this WIN32API functions:

// turn off monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2); 

// turn on monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);    

Then you write a little C-JNI wrapper to functions that call the mentioned SendMessage and use the little wrapper to turn off the monitor from Java.




回答2:


While you can query the monitor configurations with Java, there is no way to turn the monitors on and off programatically without using JNI.




回答3:


I have seen this exe named nircmd on the net. It is a freeware and can be used on by anyone.It has a built in function for doing this.

nircmd.exe monitor off 

A detailed help is also found in the website. Just use it in your program and call it using

Runtime.getRuntime().exec(this.getClass().getResource("nircmd.exe").getPath());

or put it to the windows directory and run it from there.




回答4:


You can open a full screen swing window, make it completely black, so the monitor, as far as observable behavior is concerned, is turned off.



来源:https://stackoverflow.com/questions/3662079/how-do-you-turn-on-and-off-a-monitor-from-within-a-java-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!