简体   繁体   English

如何从Java应用程序中打开和关闭监视器?

[英]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? 如何从Java应用程序中打开和关闭监视器?

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. 是的,您可以在计算机的屏幕保护程序设置中完成此操作,但是最好以编程方式进行操作,而不必在每台计算机上进行配置。

Assuming you deploy your Java Applications on Windows, you can use this WIN32API functions: 假设您在Windows上部署Java应用程序,则可以使用以下WIN32API函数:

// 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. 然后,为调用上述SendMessage函数编写一个小的C-JNI包装器 ,并使用该小的包装器从Java中关闭监视器。

I have seen this exe named nircmd on the net. 我已经在网上看到了这个名为nircmd的 exe 文件 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. 或将其放入Windows目录并从那里运行。

尽管可以使用Java查询监视器配置,但是如果不使用JNI,就无法以编程方式打开和关闭监视器。

您可以打开一个全屏的旋转窗口,使其全黑,因此,就可观察到的行为而言,显示器已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM