TNB Library
TnbCePowerWatcher.h
[詳解]
1#pragma once
11#ifndef _WIN32_WCE
12 #error TnbCePowerWatcher.h is only supported on Windows CE platforms.
13#endif // _WIN32_WCE
14
15
16
18#include <pm.h>
19
20
21
22//TNB Library
23namespace TNB{
24
25
26
37{
38public:
39
48 struct IListener
49 {
51 virtual ~IListener(void) {}
52
59 virtual void OnPowerTransitionEvent(DWORD flag, LPCTSTR lpszState) {}
60
65 virtual void OnPowerResumeEvent(void) {}
66
71 virtual void OnPowerStatusChange(void) {}
72
78 virtual void OnPowerInfoChange(const POWER_BROADCAST_POWER_INFO& info) {}
79 };
80
81
82 //---------------------------
83
84
86 CPowerWatcher(void) : m_handle(NULL)
87 {
88 m_inner.pListener = NULL;
89 }
90
93 {
94 Stop();
95 }
96
101 HANDLE GetSafeHandle(void) const
102 {
103 return m_handle;
104 }
105
111 {
112 m_inner.pListener = P;
113 }
114
120 bool IsStarted(void) const
121 {
122 return m_handle != NULL;
123 }
124
131 bool Start(DWORD flag = POWER_NOTIFY_ALL)
132 {
133 Stop();
134 m_msgQueue.SetListener(&m_inner);
135 if ( m_msgQueue.Start(3, true, _T("PowerWatch")) )
136 {
137 m_handle = ::RequestPowerNotifications(m_msgQueue, flag);
138 }
139 if ( m_handle != NULL )
140 {
141 return true;
142 }
143 DWORD r = ::GetLastError();
144 Stop();
145 ::SetLastError(r);
146 return false;
147 }
148
153 void Stop(void)
154 {
155 if ( m_handle != NULL )
156 {
157 ::StopPowerNotifications(m_handle);
158 m_handle = NULL;
159 }
160 m_msgQueue.Stop();
161 }
162
163private:
165 struct TMyPowerBroadcast
166 {
167 DWORD Message;
168 DWORD Flags;
169 DWORD Length;
170 union
171 {
172 POWER_BROADCAST_POWER_INFO powerInfo;
173 TCHAR state[260];
174 };
175 };
177 class CInner : public CMsgQueueWatcherT<TMyPowerBroadcast>::IListener
178 {
179 public:
186 virtual void OnMsgQueueWatcherEvent(const TMyPowerBroadcast& data, size_t length, bool isAlert)
187 {
188 if ( pListener != NULL )
189 {
190 switch ( data.Message )
191 {
192 case PBT_POWERINFOCHANGE:
193 ASSERT( data.Length == sizeof(POWER_BROADCAST_POWER_INFO) );
194 pListener->OnPowerInfoChange(data.powerInfo);
195 break;
196 case PBT_POWERSTATUSCHANGE:
197 pListener->OnPowerStatusChange();
198 break;
199 case PBT_RESUME:
200 pListener->OnPowerResumeEvent();
201 break;
202 case PBT_TRANSITION:
203 pListener->OnPowerTransitionEvent(data.Flags, data.state);
204 break;
205 }
206 }
207 }
208 CPowerWatcher::IListener* pListener;
209 };
210 CInner m_inner;
211 HANDLE m_handle;
213};
214
215
216
217}; // TNB
CE 用メッセージキュー関係のヘッダ
メッセージキュー監視 (CE専用).
void SetListener(IListener *P)
[設定] リスナー設定.
bool Start(size_t maxMessagesCount=0, bool isRead=true, LPCTSTR lpszThreadName=NULL)
[設定] 監視開始.
bool Stop(void)
[設定] 監視停止.
電源監視 (CE専用).
void SetListener(IListener *P)
[設定] リスナー設定.
bool Start(DWORD flag=POWER_NOTIFY_ALL)
[設定] 監視開始.
bool IsStarted(void) const
[確認] 監視確認.
HANDLE GetSafeHandle(void) const
[取得] ハンドル取得.
void Stop(void)
[設定] 監視停止.
CPowerWatcher(void)
コンストラクタ
~CPowerWatcher(void)
デストラクタ
TNB Library
Definition: TnbDoxyTitle.txt:2
virtual void OnMsgQueueWatcherEvent(const TYP &data, size_t length, bool isAlert)=0
[通知] 受信通知.
電源監視のリスナーインターフェース (CE専用).
virtual void OnPowerInfoChange(const POWER_BROADCAST_POWER_INFO &info)
[通知] 電源状態変化通知.
virtual void OnPowerResumeEvent(void)
[通知] レジューム通知.
virtual void OnPowerTransitionEvent(DWORD flag, LPCTSTR lpszState)
[通知] 電源状態変化通知.
virtual ~IListener(void)
デストラクタ
virtual void OnPowerStatusChange(void)
[通知] DC/AC切り替え通知.