5,696,576 members and growing! (19,523 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Hardware     Beginner License: The Code Project Open License (CPOL)

Turn off laptop monitor and lock the system

By Weiwei.Fang

simple code to replace the win+L hotkey
C++ (VC6, VC7, VC7.1, VC8.0, C++), C, Windows (Windows, Win2K, WinXP, Vista), Win32, Visual Studio (VS6, Visual Studio), SysAdmin, Dev, Design

Posted: 25 Jul 2008
Updated: 25 Jul 2008
Views: 2,938
Bookmarked: 7 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 1.63 Rating: 2.09 out of 5
3 votes, 50.0%
1
1 vote, 16.7%
2
1 vote, 16.7%
3
1 vote, 16.7%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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



Company: Beihang University
Location: China China

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralRemove intermediate files from source archivememberMrARSoft20:14 27 Jul '08  
GeneralYou shouldn't broadcast the monitor power message. [modified]memberLeo Davidson22:42 26 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jul 2008
Editor:
Copyright 2008 by Weiwei.Fang
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project