码迷,mamicode.com
首页 > 其他好文 > 详细

一个简单的阻止笔记本合上盖子休眠的程序

时间:2015-01-07 00:35:13      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

用来练手,顺便自己拿来用。。。

 

  1  /*----------------------------------------
  2    Keep your Notebook awake when Lid is closed.
  3                 (c) LJN, 2014
  4   ----------------------------------------*/
  5 
  6 //Improve:    Lock your computer when Lid is closed.
  7 
  8 #include <windows.h>
  9 #include <powrprof.h>
 10 #include <process.h>
 11 #include "resource.h"
 12 #pragma comment(lib, "powrprof.lib")
 13 
 14 #define IDM_TRAY    1
 15 #define IDN_TRAY    2
 16 #define IDM_SLEEP    3
 17 #define IDM_QUIT    4
 18 #define WM_TRAY        WM_USER + 1
 19 
 20 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
 21 VOID Thread (PVOID);
 22 
 23 BOOL fCurrent;
 24 
 25 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 26                     PSTR szCmdLine, int iCmdShow)
 27 {
 28     static TCHAR szAppName[] = TEXT ("Sleep-Manager");
 29     HWND hwnd;
 30     MSG msg;
 31     WNDCLASS wndclass;
 32     HANDLE hMutex;
 33 
 34     hMutex = CreateMutex (NULL, FALSE, szAppName);
 35     if (GetLastError () == ERROR_ALREADY_EXISTS)
 36     {
 37         MessageBox (NULL, TEXT ("Sorry, Sleep Manager has already run."),
 38                     TEXT ("Error"), MB_ICONERROR);
 39         return 0;
 40     }
 41 
 42     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
 43     wndclass.lpfnWndProc   = WndProc;
 44     wndclass.cbClsExtra    = 0;
 45     wndclass.cbWndExtra    = 0;
 46     wndclass.hInstance     = hInstance;
 47     wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_APP_ICON));
 48     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
 49     wndclass.hbrBackground = (HBRUSH) (GetStockObject (WHITE_BRUSH));
 50     wndclass.lpszMenuName  = NULL;
 51     wndclass.lpszClassName = szAppName;
 52     RegisterClass (&wndclass);
 53 
 54     hwnd = CreateWindow (szAppName, szAppName,
 55                         WS_OVERLAPPEDWINDOW,
 56                         CW_USEDEFAULT, CW_USEDEFAULT,
 57                         CW_USEDEFAULT, CW_USEDEFAULT,
 58                         NULL, NULL, hInstance, NULL);
 59 
 60     while (GetMessage (&msg, NULL, 0, 0))
 61     {
 62         TranslateMessage (&msg);
 63         DispatchMessage (&msg);
 64     }
 65 
 66     ReleaseMutex (hMutex);
 67 
 68     return (int) msg.wParam;
 69 }
 70 
 71 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 72 {
 73     static HINSTANCE hInstance;
 74     static HMENU hMenu;
 75     static NOTIFYICONDATA Notify;
 76     static POINT pt;
 77 
 78     static HPOWERNOTIFY hPowerSchemeNotify, hPowerSourceNotify;
 79     PPOWERBROADCAST_SETTING ppbs;
 80 
 81     static DWORD fAC_restore, fDC_restore, fPlugged;
 82     static GUID *pguid; // a pointer to a pointer
 83     SYSTEM_POWER_STATUS sysPowerStatus;
 84 
 85     TCHAR szAppName[] = TEXT ("Sleep Manager\t© 2014 LJN");
 86     static TCHAR szSleepTip[] = TEXT ("Sleep Mode");
 87     static TCHAR szAwakeTip[] = TEXT ("Awake Mode");
 88     static TCHAR szSleepInfo[] = TEXT ("Sleep when your lid is closed.\nAll settings will be restored after you quit.");
 89     static TCHAR szAwakeInfo[] = TEXT ("Keep awake when your lid is closed.\nAll settings will be restored after you quit.");
 90 
 91     switch (message)
 92     {
 93     case WM_CREATE:
 94 
 95         _beginthread (Thread, 0, NULL) ;
 96         hInstance = ((LPCREATESTRUCT) lParam) -> hInstance;
 97 
 98         GetSystemPowerStatus (&sysPowerStatus);
 99         fPlugged = sysPowerStatus.ACLineStatus ? TRUE : FALSE;
100 
101         hPowerSchemeNotify = RegisterPowerSettingNotification (hwnd,
102                         &GUID_ACTIVE_POWERSCHEME, DEVICE_NOTIFY_WINDOW_HANDLE);
103         hPowerSourceNotify = RegisterPowerSettingNotification (hwnd,
104                         &GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE);
105 
106         PowerGetActiveScheme (NULL, &pguid);
107         PowerReadACValueIndex (NULL, pguid,
108                             &GUID_SYSTEM_BUTTON_SUBGROUP,
109                             &GUID_LIDCLOSE_ACTION,
110                             &fAC_restore);
111         PowerReadDCValueIndex (NULL, pguid,
112                             &GUID_SYSTEM_BUTTON_SUBGROUP,
113                             &GUID_LIDCLOSE_ACTION,
114                             &fDC_restore);
115 
116         if (fPlugged) fCurrent = fAC_restore;
117         else fCurrent = fDC_restore;
118 
119         hMenu = CreatePopupMenu ();
120         if (fPlugged) AppendMenu (hMenu, MF_STRING, IDM_SLEEP, TEXT ("AC Mode Sleep"));
121         else AppendMenu (hMenu, MF_STRING, IDM_SLEEP, TEXT ("DC Mode Sleep"));
122         AppendMenu (hMenu, MF_SEPARATOR, 0, 0);
123         AppendMenu (hMenu, MF_STRING, IDM_QUIT, TEXT ("Quit"));
124 
125         Notify.cbSize = (DWORD) sizeof (NOTIFYICONDATA);
126         Notify.hWnd = hwnd;
127         Notify.uID = IDN_TRAY;
128         Notify.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;
129         Notify.uCallbackMessage = WM_TRAY;
130         Notify.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
131         wcscpy_s (Notify.szInfoTitle, sizeof szAppName, szAppName);
132 
133         if (fCurrent)
134         {
135             Notify.hIcon = Notify.hBalloonIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRAY_ICON));
136             wcscpy_s (Notify.szTip, sizeof szSleepTip, szSleepTip);
137             wcscpy_s (Notify.szInfo, sizeof szSleepInfo, szSleepInfo);
138         }
139         else
140         {
141             Notify.hIcon = Notify.hBalloonIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_DISABLED_ICON));
142             wcscpy_s (Notify.szTip, sizeof szAwakeTip, szAwakeTip);
143             wcscpy_s (Notify.szInfo, sizeof szAwakeInfo, szAwakeInfo);
144         }
145         Shell_NotifyIcon (NIM_ADD, &Notify);
146         Notify.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
147 
148         return 0;
149 
150     case WM_TRAY:
151 
152         switch (lParam)
153         {
154         case WM_LBUTTONUP:
155 
156             fCurrent = fCurrent ? FALSE : TRUE;
157 
158             if (fPlugged)
159                 PowerWriteACValueIndex (NULL, pguid,
160                             &GUID_SYSTEM_BUTTON_SUBGROUP,
161                             &GUID_LIDCLOSE_ACTION,
162                             fCurrent);
163             else
164                 PowerWriteDCValueIndex (NULL, pguid,
165                             &GUID_SYSTEM_BUTTON_SUBGROUP,
166                             &GUID_LIDCLOSE_ACTION,
167                             fCurrent);
168             PowerSetActiveScheme (NULL, pguid);
169 
170             if (fCurrent)
171             {
172                 Notify.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRAY_ICON));
173                 wcscpy_s (Notify.szTip, sizeof szSleepTip, szSleepTip);
174             }
175             else
176             {
177                 Notify.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_DISABLED_ICON));
178                 wcscpy_s (Notify.szTip, sizeof szAwakeTip, szAwakeTip);
179             }
180             Shell_NotifyIcon (NIM_MODIFY, &Notify);
181 
182             break;
183 
184         case WM_RBUTTONUP:
185 
186             if (fPlugged)
187             {
188                 if (fCurrent) ModifyMenu (hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_CHECKED, IDM_SLEEP, TEXT ("AC Mode Sleep"));
189                 else ModifyMenu (hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_UNCHECKED, IDM_SLEEP, TEXT ("AC Mode Sleep"));
190             }
191             else
192             {
193                 if (fCurrent) ModifyMenu (hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_CHECKED, IDM_SLEEP, TEXT ("DC Mode Sleep"));
194                 else ModifyMenu (hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_UNCHECKED, IDM_SLEEP, TEXT ("DC Mode Sleep"));
195             }
196 
197             SetForegroundWindow (hwnd);
198             GetCursorPos (&pt);
199             TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
200 
201             break;
202         } 
203         return 0;
204 
205     case WM_COMMAND:
206     case WM_SYSCOMMAND:
207 
208         switch (LOWORD (wParam))
209         {
210         case IDM_SLEEP:
211             SendMessage (hwnd, WM_TRAY, 0, WM_LBUTTONUP);
212             return 0;
213 
214         case IDM_QUIT:
215             SendMessage (hwnd, WM_DESTROY, 0, 0);
216             return 0;
217         }
218         break;
219 
220     case WM_POWERBROADCAST:
221 
222         switch (wParam)
223         {
224         case PBT_POWERSETTINGCHANGE:
225 
226             ppbs = (POWERBROADCAST_SETTING*) lParam;
227             if (!memcmp (&ppbs -> PowerSetting, &GUID_ACTIVE_POWERSCHEME, sizeof (GUID)))
228             {
229                 PowerWriteACValueIndex (NULL, pguid,
230                                     &GUID_SYSTEM_BUTTON_SUBGROUP,
231                                     &GUID_LIDCLOSE_ACTION,
232                                     fAC_restore);
233                 PowerWriteDCValueIndex (NULL, pguid,
234                                     &GUID_SYSTEM_BUTTON_SUBGROUP,
235                                     &GUID_LIDCLOSE_ACTION,
236                                     fDC_restore);
237                 LocalFree (pguid);
238 
239                 PowerGetActiveScheme (NULL, &pguid);
240                 PowerReadACValueIndex (NULL, pguid,
241                                     &GUID_SYSTEM_BUTTON_SUBGROUP,
242                                     &GUID_LIDCLOSE_ACTION,
243                                     &fAC_restore);
244                 PowerReadDCValueIndex (NULL, pguid,
245                                     &GUID_SYSTEM_BUTTON_SUBGROUP,
246                                     &GUID_LIDCLOSE_ACTION,
247                                     &fDC_restore);
248 
249                 if (fPlugged) fCurrent = fAC_restore;
250                 else fCurrent = fDC_restore;
251             }
252             else if (!memcmp (&ppbs -> PowerSetting, &GUID_ACDC_POWER_SOURCE, sizeof (GUID)))
253             {
254                 GetSystemPowerStatus (&sysPowerStatus);
255                 fPlugged = sysPowerStatus.ACLineStatus ? TRUE : FALSE;
256 
257                 if (fPlugged)
258                 {
259                     PowerWriteDCValueIndex (NULL, pguid,
260                                         &GUID_SYSTEM_BUTTON_SUBGROUP,
261                                         &GUID_LIDCLOSE_ACTION,
262                                         fDC_restore);
263                     fCurrent = fAC_restore;
264                 }
265                 else
266                 {
267                     PowerWriteACValueIndex (NULL, pguid,
268                                         &GUID_SYSTEM_BUTTON_SUBGROUP,
269                                         &GUID_LIDCLOSE_ACTION,
270                                         fAC_restore);
271                     fCurrent = fDC_restore;
272                 }
273             }
274             else
275             {
276                 MessageBox (NULL, TEXT ("Sorry, Your Power Scheme has changed in an unexpected way."),
277                             NULL, MB_ICONERROR);
278                 break;
279             }
280 
281             //Fall Through
282             if (fCurrent)
283             {
284                 Notify.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_TRAY_ICON));
285                 wcscpy_s (Notify.szTip, sizeof szSleepTip, szSleepTip);
286             }
287             else
288             {
289                 Notify.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_DISABLED_ICON));
290                 wcscpy_s (Notify.szTip, sizeof szAwakeTip, szAwakeTip);
291             }
292             Shell_NotifyIcon (NIM_MODIFY, &Notify);
293         }
294         return 0;
295 
296     case WM_QUERYENDSESSION:
297     case WM_DESTROY:
298 
299         PowerWriteACValueIndex (NULL, pguid,
300                         &GUID_SYSTEM_BUTTON_SUBGROUP,
301                         &GUID_LIDCLOSE_ACTION,
302                         fAC_restore);
303         PowerWriteDCValueIndex (NULL, pguid,
304                         &GUID_SYSTEM_BUTTON_SUBGROUP,
305                         &GUID_LIDCLOSE_ACTION,
306                         fDC_restore);
307         PowerSetActiveScheme (NULL, pguid);
308         LocalFree (pguid);
309 
310         DestroyMenu (hMenu) ;
311         Shell_NotifyIcon (NIM_DELETE, &Notify) ;
312         UnregisterPowerSettingNotification (hPowerSchemeNotify);
313         UnregisterPowerSettingNotification (hPowerSourceNotify);
314 
315         PostQuitMessage (0);
316         return 0;
317     }
318     return DefWindowProc (hwnd, message, wParam, lParam);
319 }
320 
321 VOID Thread (PVOID pvoid)
322 {
323     while (TRUE)
324     {
325         Sleep (1000);
326         if (fCurrent) SetThreadExecutionState (ES_CONTINUOUS);
327         else SetThreadExecutionState (ES_AWAYMODE_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
328     }
329 }

 

一个简单的阻止笔记本合上盖子休眠的程序

标签:

原文地址:http://www.cnblogs.com/BOT-Man/p/4207375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!