MSDN Magazine - July 2007 - (Page 49) Management documentation. For comparison, the PBT_ APMPOWERSTATUSCHANGE, PBT_APMSUSPEND, and PBT_APMRESUMESUSPEND wParams are analogous to the StatusChange, Suspend, and Resume PowerModes in the PowerModeChange event, respectively. It is important to note the difference between wParam values for PBT_APMPOWERSTATUSCHANGE and the PBT_ POWERSETTINGCHANGE. The latter can be understood as a Windows Vista-only superset of PBT_APMPOWERSTATUSCHANGE. It indicates all the power-related changes that PBT_APMPOWERSTATUSCHANGE indicates as well as other information specific only to Windows Vista, such as power plans and monitor state. Both of these wParams exist and are passed along with the WM_POWERBROADCAST window message on Windows Vista, but processing both would result in redundancy in this sample when running on Windows Vista. The PBT_POWERSETTINGCHANGE wParam, however, will not be sent to a Windows Vista application until the application registers for power setting notifications (PBT_APMPOWERSTATUSCHANGE is received without registration). This is a very important step for receiving power notifications specific to Windows Vista. grouped all the unregistration into one function. Registration occurs when the PowerAwareWindow loads (the Window.Loaded event is handled by PowerAwareWindow_Loaded function) and unregistration occurs when it closes (Window.Closed handled by PowerAwareWindow_Closed). Registration introduces one of the main issues that I address in this sample—to create a consistent approach to I want to create an approach power-awareness in one to power-awareness that is Windows Presentation consistent and accounts for Foundation application that accounts for the differthe differences regarding ences regarding power nopower notifications on tifications on Windows XP both Windows XP and and Windows Vista. The Windows Vista. RegisterPowerSettingNotification and UnregisterPowerSettingNotification functions exported from User32.dll are not available on operating systems prior to Windows Vista; as a result, any code that tries to use these functions should only do so after first ensuring the functions are available. Figure 3 Four Types of PowerSettingGuid // notify application when percentage of battery remaining changes static Guid GUID_BATTERY_PERCENTAGE_REMAINING = new Guid(“A7AD8041-B45A-4CAE-87A3-EECBB468A9E1”); // notify application when the state of the monitor changes static Guid GUID_MONITOR_POWER_ON = new Guid(“02731015-4510-4526-99E6-E5A17EBD1AEA”); // notify application when the system power source changes static Guid GUID_ACDC_POWER_SOURCE = new Guid(“5D3E9A59-E9D5-4B00-A6BD-FF34FF516548”); // notify application when the user-defined power plan changes static Guid GUID_POWERSCHEME_PERSONALITY = new Guid(“245D8541-3943-4422-B025-13A784F679B7”); Registering for Notifications on Windows Vista To register for power setting notifications on Windows Vista, the first step is to make the RegisterPowerSettingNotification function available for use in the application. You can do that with the following P/Invoke function definition: [DllImport(@”User32.dll”, SetLastError=true, CallingConvention=CallingConvention.StdCall)] static extern IntPtr RegisterPowerSettingNotification( IntPtr hRecipient, ref Guid PowerSettingGuid, Int32 Flags); This function returns a handle that will be used to unregister for the events when the application is shutting down. The Flags passed into this function indicate to the operating system how the application would like to receive notifications. For this sample, I declare this as: const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000; Figure 4 Windows Vista Power Registration IntPtr IntPtr IntPtr IntPtr _hBattCapacity; _hMonitorOn; _hPowerScheme; _hPowerSrc; This indicates to the registration function that notifications are to be sent to the application using the WM_POWERBROADCAST window message with a wParam of PBT_POWERSETTINGCHANGE. The other option for Flags is relevant for OS services and is explained in the RegisterPowerSettingNotification documentation mentioned earlier. The PowerSettingGuid value passed to the registration function indicates to the operating system the notifications in which the application is interested. Figure 3 shows the four PowerSettingGuid values you’re probably most interested in. Other notification GUIDs are available and can be used to get notified for events such as when the system is entering or exiting an away mode or when the system will be moving into an idle state in the near future and the current time is a good time to perform background or idle tasks. In the sample, I have grouped all the Windows Vista power registration into one function (see Figure 4). Similarly, I have also void RegisterForVistaPowerNotifications(IntPtr hWnd) { _hPowerSrc = RegisterPowerSettingNotification( hWnd, ref GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE); _hBattCapacity = RegisterPowerSettingNotification( hWnd, ref GUID_BATTERY_PERCENTAGE_REMAINING, DEVICE_NOTIFY_WINDOW_HANDLE); _hMonitorOn = RegisterPowerSettingNotification( hWnd, ref GUID_MONITOR_POWER_ON, DEVICE_NOTIFY_WINDOW_HANDLE); _hPowerScheme = RegisterPowerSettingNotification( hWnd, ref GUID_POWERSCHEME_PERSONALITY, DEVICE_NOTIFY_WINDOW_HANDLE); } void UnregisterForVistaPowerNotifications() { UnregisterPowerSettingNotification(_hBattCapacity); UnregisterPowerSettingNotification(_hMonitorOn); UnregisterPowerSettingNotification(_hPowerScheme); UnregisterPowerSettingNotification(_hPowerSrc); } Power-Aware Apps july2007 49 Table of Contents for the Digital Edition of MSDN Magazine - July 2007 Contents Toolbox CLR Inside Out Data Points Cutting Edge Mobility: Make Your WPF Apps Power-Aware Share Code: Write Code Once for Both Mobile and Desktop Apps NTFS: Enhance Your Apps with File System Transactions Security: Applying Cryptology Using the CNG API in Windows Vista Speak Up: Support Dictation with Text Services Framework Service Station Bugslayer Security Briefs Foundations {End Bracket} MSDN Magazine - July 2007 MSDN Magazine - July 2007 - Contents (Page Cover1) MSDN Magazine - July 2007 - Contents (Page Cover2) MSDN Magazine - July 2007 - Contents (Page 1) MSDN Magazine - July 2007 - Contents (Page 2) MSDN Magazine - July 2007 - Contents (Page 3) MSDN Magazine - July 2007 - Contents (Page 4) MSDN Magazine - July 2007 - Contents (Page 5) MSDN Magazine - July 2007 - Contents (Page 6) MSDN Magazine - July 2007 - Contents (Page 7) MSDN Magazine - July 2007 - Contents (Page 8) MSDN Magazine - July 2007 - Contents (Page 9) MSDN Magazine - July 2007 - Contents (Page 10) MSDN Magazine - July 2007 - Toolbox (Page 11) MSDN Magazine - July 2007 - Toolbox (Page 12) MSDN Magazine - July 2007 - Toolbox (Page 13) MSDN Magazine - July 2007 - Toolbox (Page 14) MSDN Magazine - July 2007 - Toolbox (Page 15) MSDN Magazine - July 2007 - Toolbox (Page 16) MSDN Magazine - July 2007 - CLR Inside Out (Page 17) MSDN Magazine - July 2007 - CLR Inside Out (Page 18) MSDN Magazine - July 2007 - CLR Inside Out (Page 19) MSDN Magazine - July 2007 - CLR Inside Out (Page 20) MSDN Magazine - July 2007 - CLR Inside Out (Page 21) MSDN Magazine - July 2007 - CLR Inside Out (Page 22) MSDN Magazine - July 2007 - CLR Inside Out (Page 23) MSDN Magazine - July 2007 - CLR Inside Out (Page 24) MSDN Magazine - July 2007 - Data Points (Page 25) MSDN Magazine - July 2007 - Data Points (Page 26) MSDN Magazine - July 2007 - Data Points (Page 27) MSDN Magazine - July 2007 - Data Points (Page 28) MSDN Magazine - July 2007 - Data Points (Page 29) MSDN Magazine - July 2007 - Data Points (Page 30) MSDN Magazine - July 2007 - Data Points (Page 31) MSDN Magazine - July 2007 - Data Points (Page 32) MSDN Magazine - July 2007 - Data Points (Page 33) MSDN Magazine - July 2007 - Data Points (Page 34) MSDN Magazine - July 2007 - Cutting Edge (Page 35) MSDN Magazine - July 2007 - Cutting Edge (Page 36) MSDN Magazine - July 2007 - Cutting Edge (Page 37) MSDN Magazine - July 2007 - Cutting Edge (Page 38) MSDN Magazine - July 2007 - Cutting Edge (Page 39) MSDN Magazine - July 2007 - Cutting Edge (Page 40) MSDN Magazine - July 2007 - Cutting Edge (Page 41) MSDN Magazine - July 2007 - Cutting Edge (Page 42) MSDN Magazine - July 2007 - Cutting Edge (Page 43) MSDN Magazine - July 2007 - Cutting Edge (Page 44) MSDN Magazine - July 2007 - Cutting Edge (Page 45) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 46) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 47) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 48) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 49) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 50) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 51) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 52) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 53) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 54) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 55) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 56) MSDN Magazine - July 2007 - Mobility: Make Your WPF Apps Power-Aware (Page 57) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 58) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 59) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 60) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 61) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 62) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 63) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 64) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 65) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 66) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 67) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 68) MSDN Magazine - July 2007 - Share Code: Write Code Once for Both Mobile and Desktop Apps (Page 69) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 70) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 71) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 72) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 73) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 74) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 75) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 76) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 77) MSDN Magazine - July 2007 - NTFS: Enhance Your Apps with File System Transactions (Page 78) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 79) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 80) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 81) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 82) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 83) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 84) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 85) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 86) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 87) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 88) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 89) MSDN Magazine - July 2007 - Security: Applying Cryptology Using the CNG API in Windows Vista (Page 90) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 91) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 92) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 93) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 94) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 95) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 96) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 97) MSDN Magazine - July 2007 - Speak Up: Support Dictation with Text Services Framework (Page 98) MSDN Magazine - July 2007 - Service Station (Page 99) MSDN Magazine - July 2007 - Service Station (Page 100) MSDN Magazine - July 2007 - Service Station (Page 101) MSDN Magazine - July 2007 - Service Station (Page 102) MSDN Magazine - July 2007 - Service Station (Page 103) MSDN Magazine - July 2007 - Service Station (Page 104) MSDN Magazine - July 2007 - Service Station (Page 105) MSDN Magazine - July 2007 - Service Station (Page 106) MSDN Magazine - July 2007 - Service Station (Page 107) MSDN Magazine - July 2007 - Service Station (Page 108) MSDN Magazine - July 2007 - Service Station (Page 109) MSDN Magazine - July 2007 - Service Station (Page 110) MSDN Magazine - July 2007 - Bugslayer (Page 111) MSDN Magazine - July 2007 - Bugslayer (Page 112) MSDN Magazine - July 2007 - Bugslayer (Page 113) MSDN Magazine - July 2007 - Bugslayer (Page 114) MSDN Magazine - July 2007 - Bugslayer (Page 115) MSDN Magazine - July 2007 - Bugslayer (Page 116) MSDN Magazine - July 2007 - Bugslayer (Page 117) MSDN Magazine - July 2007 - Bugslayer (Page 118) MSDN Magazine - July 2007 - Security Briefs (Page 119) MSDN Magazine - July 2007 - Security Briefs (Page 120) MSDN Magazine - July 2007 - Security Briefs (Page 121) MSDN Magazine - July 2007 - Security Briefs (Page 122) MSDN Magazine - July 2007 - Security Briefs (Page 123) MSDN Magazine - July 2007 - Security Briefs (Page 124) MSDN Magazine - July 2007 - Security Briefs (Page 125) MSDN Magazine - July 2007 - Security Briefs (Page 126) MSDN Magazine - July 2007 - Foundations (Page 127) MSDN Magazine - July 2007 - Foundations (Page 128) MSDN Magazine - July 2007 - Foundations (Page 129) MSDN Magazine - July 2007 - Foundations (Page 130) MSDN Magazine - July 2007 - Foundations (Page 131) MSDN Magazine - July 2007 - Foundations (Page 132) MSDN Magazine - July 2007 - Foundations (Page 133) MSDN Magazine - July 2007 - Foundations (Page 134) MSDN Magazine - July 2007 - Foundations (Page 135) MSDN Magazine - July 2007 - {End Bracket} (Page 136) MSDN Magazine - July 2007 - {End Bracket} (Page Cover3) MSDN Magazine - July 2007 - {End Bracket} (Page Cover4) http://www.nxtbookMEDIA.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.