开发者

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

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 开发者_如何学JAVAbe great to do it programatically and avoid having to configure it on each machine.


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜