HOME - - - - - Delphi Tutorials TOC - - - - - - - - - - - - Other material for programmers

Delphi: How to blank monitor

This addresses how you can turn the monitor "off".... if you're using Win 9x, not NT or descendants. The "off" I refer to is not truely "off", as in unplugged... but it is more than an "on" monitor displaying black! It is the same state that the Windows power save option allows you to invoke.

I start with thanks to groups.google.com and Charles and Luis for their posts in borland.programming.delphi.winapi of May 17 2001. Groups.google.com is always a good place to go if you need help with "How do I..." or "Why does my computer..."

Start a project. I called mine DD41

Be a little careful as you develop this application... it is quite easy to get the screen OFF with no way (short of a power cycle!) to turn it back on again!

Create variable boMonitorOff

In FormCreate set it false

Optional, but maybe wise: Put an ink-on-paper announcement on your monitor saying "Move mouse to turn monitor back on" You'll just have to hope your user sees that before turning the computer or monitor OFF thinking he/she is turning it ON!

To start with, we're not going to turn the monitor on and off. We'll write code which will do that with very little modification AFTER other bugs have been eradicated!

To that end, add a lable to the form. Call it laMonitorState, and use the Object Inspector to initialise its caption to say "Monitor On"

Create a button, call it buMonitorOff. Make it's OnClick handler....
laMonitorState.caption:='Monitor Off';
You can run the program now. Clicking the Monitor Off button should change the MonitorState label.... but once it says "Monitor Off", there's nothing in the program to reverse the action. Aren't you glad you didn't really turn the monitor off!

Add a OnMouseMove event handler to the main form. Make it....
if boMonitorOff then begin
laMonitorState.caption:='Monitor On';
Now the program should be doing almost everything it should.

For the final bits.... (When you test this, don't be alarmed if the monitor doesn't spring to life instantly when you move the mouse. It may take a moment (less than 20 seconds, though!) to warm up! Also: in the early stages, you have to move the mouse far enought to bring the (at that time invisible!) pointer outside of the button that you clicked to blank the screen.

Add to FormMouseMove, just after "if boMonitorOff then begin", ...
Add to buMonitorOffClick...
That should do it!

(P.S.: 8/08. Hummm.. "Should do it...." I should have known I was tempting fate saying something like that! A reader sent me an email to say that when he tried this, it "worked", but he had to use 2 to turn the monitor oof, vs the 0 I cite above. If one doesn't work... for you... try the other!)

One last refinement:

In the Object Inspector, drill down to buMonitorOff's OnMouseMove event. Click the downward pointing arrow at the right hand end of the field to the right of "OnMouseMove". You should see "FormMouseMove". Click on it. Now the FormMouseMove event handler should trigger as soon as you move the mouse, even before it leaves the button's face.

And now for the bad news....

I suspect the main use for this material would be in some form of blank-screen-if-there's-no-user-action-after-a-time-inteval scheme. If the program blanking the screen does not happen to have focus at the time the screen blanks, will moving the mouse be "seen" by that program so that the screen-restore code can be invoked? I suspect there's some way to pass a "I saw the mouse move" message to all running apps.... but maybe this needs a little careful consideration/ research. I'm sure answers will be available within a screensaver tutorial somewhere.
To search THIS site....
Click this to search this site without using forms, or just use......
powered by FreeFind
Site search Web search
Be sure to spell the words you are searching for correctly!
The search engine doesn't understand English. Searching for "How do I use repeat" will just return pages with "how", "do", "I", "use", or "repeat".
(Go to my other sites (see bottom of page) and use their search buttons if you want to search them.)...

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
How to email or write this page's editor, Tom Boyd

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....