Created
September 6, 2012 08:25
-
-
Save xeonchen/3653008 to your computer and use it in GitHub Desktop.
turn off monitors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
void TurnOffMonitors(BOOL bLock = FALSE) | |
{ | |
::PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); | |
if (bLock) ::LockWorkStation(); | |
} | |
template <BOOL bLock> | |
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) | |
{ | |
if (nCode == HC_ACTION) | |
{ | |
KBDLLHOOKSTRUCT *s = (KBDLLHOOKSTRUCT*)lParam; | |
switch (s->vkCode) | |
{ | |
case 'L': | |
if (wParam == WM_KEYDOWN && GetKeyState(VK_RCONTROL) & 0x8000) | |
TurnOffMonitors(bLock); | |
break; | |
} | |
} | |
return ::CallNextHookEx(NULL, nCode, wParam, lParam); | |
} | |
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int) | |
{ | |
BOOL bDaemon = FALSE; | |
BOOL bNoLock = FALSE; | |
for (int i = 1; i < __argc; ++i) | |
{ | |
if (strncmp(__argv[i], "-d", 3) == 0) | |
bDaemon = TRUE; | |
else if (strncmp(__argv[i], "-n", 3) == 0) | |
bNoLock = TRUE; | |
} | |
if (bDaemon) | |
{ | |
if (bNoLock) | |
::SetWindowsHookEx(WH_KEYBOARD_LL, HookProc<FALSE>, NULL, 0); | |
else | |
::SetWindowsHookEx(WH_KEYBOARD_LL, HookProc<TRUE>, NULL, 0); | |
MSG msg; | |
while (::GetMessage(&msg, NULL, 0, 0)) | |
{ | |
::TranslateMessage(&msg); | |
::DispatchMessage(&msg); | |
} | |
} | |
else | |
{ | |
TurnOffMonitors(!bNoLock); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment