📄 faq72.htm
字号:
<HTML>
<HEAD>
<TITLE>Prevent the screensaver from running</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Prevent the screensaver from running
</H3>
<P>
You can use two techniques to prevent the screensaver from starting. You can respond to
<TT>WM_SYSCOMMAND</TT> messages, or you can call the <TT>SystemParametersInfo</TT> API
function. This FAQ describes both techniques.
</P>
<BR>
<B><TT>SystemParametersInfo</TT>:</B>
<P>
<TT>SystemParametersInfo</TT> allows you to get and set a host of system settings.
Among other things, it allows you to turn off the screensaver. You can also get and set
the screensaver delay time. The code segment below shows a program with two button click
handlers. One button deactivates the screensaver, the other turns it on. The constructor
of the form determines if the screensaver was originally active when the program started
, and what the screensaver delay was set to. As an added bonus, I threw in a function
that sets the screensaver timeout to 30 seconds.
</P>
<pre>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
<font color="navy">// Find out if the screensaver was active and what the</font>
<font color="navy">// screen saver timeout value is.</font>
BOOL bActive<b>;</b>
<b>int</b> nTimeout<b>;</b>
SystemParametersInfo<b>(</b>SPI_GETSCREENSAVEACTIVE<b>,</b> <font color="blue">0</font><b>,</b> <b>&</b>bActive<b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
SystemParametersInfo<b>(</b>SPI_GETSCREENSAVETIMEOUT<b>,</b> <font color="blue">0</font><b>,</b> <b>&</b>nTimeout<b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
<b>if</b><b>(</b>bActive<b>)</b>
Label1<b>-></b>Caption <b>=</b> <font color="blue">"Screensaver was active"</font><b>;</b>
<b>else</b>
Label1<b>-></b>Caption <b>=</b> <font color="blue">"Screensaver was turned off"</font><b>;</b>
Label2<b>-></b>Caption <b>=</b> <font color="blue">"Screensaver delay time = "</font> <b>+</b> IntToStr<b>(</b>nTimeout<b>)</b><b>;</b>
<b>}</b>
<font color="navy">//---------------------------------------------------</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// Enable the screen saver.</font>
SystemParametersInfo<b>(</b>SPI_SETSCREENSAVEACTIVE<b>,</b> TRUE<b>,</b> NULL<b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//---------------------------------------------------</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button2Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// Disable the screen saver.</font>
SystemParametersInfo<b>(</b>SPI_SETSCREENSAVEACTIVE<b>,</b> FALSE<b>,</b>NULL<b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//---------------------------------------------------</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button3Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// set the screensaver timeout to 30 seconds.</font>
SystemParametersInfo<b>(</b>SPI_SETSCREENSAVETIMEOUT<b>,</b> <font color="blue">30</font><b>,</b> NULL<b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
<b>}</b>
</pre>
<BR>
<B><TT>WM_SYSCOMMAND</TT></B>
<P>
Before Windows starts the screensaver, it broadcasts a <TT>WM_SYSCOMMAND</TT> message
with <tt>WPARAM</TT> set to <TT>SC_SCREENSAVE</TT>. If any program catches the message and
returns true, the screensaver will not start. Here is a code example that shows how to
prevent the screensaver from running by detecting the <TT>WM_SYSCOMMAND</TT> message.
</P>
<pre>
<font color="navy">//----------------------------------------------</font>
<font color="navy">// class header file</font>
<b>private</b><b>:</b> <font color="navy">// User declarations</font>
<b>void</b> <b>__fastcall</b> WndProc<b>(</b>Messages<b>:</b><b>:</b>TMessage <b>&</b>Message<b>)</b><b>;</b>
<font color="navy">//----------------------------------------------</font>
<font color="navy">// CPP source file</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>WndProc<b>(</b>Messages<b>:</b><b>:</b>TMessage <b>&</b>Message<b>)</b>
<b>{</b>
<b>if</b><b>(</b> <b>(</b>Message<b>.</b>Msg <b>==</b> WM_SYSCOMMAND<b>)</b> <b>&&</b>
<b>(</b>Message<b>.</b>WParam <b>==</b> SC_SCREENSAVE<b>)</b> <b>)</b>
<b>{</b>
ShowMessage<b>(</b><font color="blue">"screensaver disabled"</font><b>)</b><b>;</b>
Message<b>.</b>Result <b>=</b> <b>true</b><b>;</b>
<b>}</b>
<b>else</b>
TForm<b>:</b><b>:</b>WndProc<b>(</b>Message<b>)</b><b>;</b>
<b>}</b>
</pre>
<BR>
<P>
<B>Note:</B> If you deactivate the screensaver with <TT>SytemParametersInfo</TT>, make
sure that you reenable it before your program terminates. The screensaver remains disabled
until somebody activates it again.
</P>
<P>
<B>Note:</B> <TT>SytemParametersInfo</TT> enables the screensaver, but it doesn't
actually execute it.
</P>
<P>
<B>Note:</B> Enabling the screensaver with <TT>SytemParametersInfo</TT> does not guarantee
that the screensaver will run. If the user has turned of the screensaver via control panel,
the OS will not activate the screensaver.
</P>
<P>
<B>Note:</B> Intercepting the <TT>WM_SYSCOMMAND</TT> message is probably more robust than
using <TT>SytemParametersInfo</TT> because another program can call
<TT>SystemParametersInfo</TT> and override any previous settings that you make.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -