Click here to Skip to main content
Licence CPOL
First Posted 25 Jul 2008
Views 17,333
Downloads 266
Bookmarked 17 times

Turn off laptop monitor and lock the system

By Weiwei.Fang | 25 Jul 2008 | Unedited contribution
simple code to replace the win+L hotkey
2 votes, 40.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4

5
2.33/5 - 5 votes
μ 2.33, σa 2.28 [?]

Introduction

I use the Thinkpad laptop for my research work. Usually, I keep it working in the night for the simulation work. Therefore, when I wake up at the next morning, I can analyze the results immediately. I need to lock the WinXP system and turn off the monitor when I leave the laptop. Then I write this program to help me. It is more convenient to use than the traditional way (Win+L hotkey to lock the system and turn off monitor through power schemes).

Code explanation

The code is very simple.

First, turn off the monitor by using SendMessage function. You can find more introduction about it from this article.

// Turn off monitor
    
Sleep(500); // Eliminate user's interaction for 500 ms
    
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
        

Second, use LockWorkStation function to lock the system.

LockWorkStation(); 

Third, the LockWorkStation function requires Windows Vista, Windows XP, or Windows 2000 Professional. To compile the program successfully, we need to add the following macro in the StdAfx.h.

#define _WIN32_WINNT 0x0500
This program is very simple yet very useful. It keeps your task running on the laptop while turns off the monitor for saving energy and protects the laptop from unauthorized use.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Weiwei.Fang


Beihang University
China China

Member
http://sites.google.com/site/fangvv

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRemove intermediate files from source archive PinmemberMrARSoft20:14 27 Jul '08  
GeneralYou shouldn't broadcast the monitor power message. [modified] PinmemberLeo Davidson22:42 26 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 26 Jul 2008
Article Copyright 2008 by Weiwei.Fang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid