TNB Library
TnbWindowProcedureHooker.h
[詳解]
1#pragma once
11#include "TnbSimpleMap.h"
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
21#ifndef _TnbDOXYGEN //Document作成用シンボル
22 // HWNDとリスナーのMAP
23 _SELECTANY CSimpleMapT<HWND, LPVOID>* s_pmapWphHwndProc = NULL;
24 _SELECTANY bool s_isWphInit = true;
25#endif
26
27
28
74{
75public:
77 CWindowProcedureHooker(void) : m_hWnd(NULL), m_pOrgWndProc(NULL)
78 {
79 }
80
83 {
84 Destroy();
85 }
86
92 bool IsAttached(void) const
93 {
94 return m_pOrgWndProc != NULL;
95 }
96
103 bool Attach(HWND hWnd)
104 {
105 Detach();
106 if ( s_isWphInit && s_pmapWphHwndProc == NULL )
107 {
108 s_isWphInit = false;
109 static CReject s_cRejeWPH;
110 s_pmapWphHwndProc = new CSimpleMapT<HWND, LPVOID>;
111 }
112 if ( s_pmapWphHwndProc == NULL || s_pmapWphHwndProc->HasKey(hWnd) )
113 {
114 return false;
115 }
116 //
117 PreAttach(hWnd);
118 //
119 WNDPROC oldWndProc = m_SetWndProc(hWnd, ms_WndProc);
120 if ( oldWndProc == ms_WndProc || oldWndProc == NULL )
121 {
122 return false;
123 }
124 m_pOrgWndProc = oldWndProc;
125 m_hWnd = hWnd;
126 (*s_pmapWphHwndProc)[hWnd] = this;
127 //
128 OnAttached();
129 return true;
130 }
131
135 void Detach(void)
136 {
137 if ( m_pOrgWndProc != NULL )
138 {
139 PreDetach();
140 WNDPROC oldWndProc = m_SetWndProc(m_hWnd, m_pOrgWndProc);
141 IgnoreUnusedValue(oldWndProc);
142 if ( s_pmapWphHwndProc != NULL )
143 {
144 s_pmapWphHwndProc->RemoveKey(m_hWnd);
145 }
146 OnDetached();
147 m_hWnd = NULL;
148 m_pOrgWndProc = NULL;
149 }
150 }
151
156 void Destroy(void)
157 {
158 if ( m_pOrgWndProc != NULL )
159 {
160 WNDPROC oldWndProc = m_SetWndProc(m_hWnd, m_pOrgWndProc);
161 IgnoreUnusedValue(oldWndProc);
162 if ( s_pmapWphHwndProc != NULL )
163 {
164// s_pmapWphHwndProc->RemoveKey(m_hWnd);
165 }
166 m_hWnd = NULL;
167 m_pOrgWndProc = NULL;
168 }
169 }
170
176 HWND GetSafeHwnd(void) const
177 {
178 return m_hWnd;
179 }
180
189 LRESULT SendMessage(UINT message, WPARAM wParam, LPARAM lParam)
190 {
191 return ::SendMessage(m_hWnd, message, wParam, lParam);
192 }
193
202 BOOL PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
203 {
204 return ::PostMessage(m_hWnd, message, wParam, lParam);
205 }
206
211 DWORD GetStyle(void) const
212 {
213 return ::GetWindowLong(m_hWnd, GWL_STYLE);
214 }
215
220 DWORD GetExStyle(void) const
221 {
222 return ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
223 }
224
230 void ModifyStyle(DWORD dwRemove, DWORD dwAdd)
231 {
232 DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
233 DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
234 if ( dwStyle != dwNewStyle )
235 {
236 ::SetWindowLong(m_hWnd, GWL_STYLE, dwNewStyle);
237 }
238 }
239
245 void ModifyStyleEx(DWORD dwRemove, DWORD dwAdd)
246 {
247 DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
248 DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
249 if ( dwStyle != dwNewStyle )
250 {
251 ::SetWindowLong(m_hWnd, GWL_EXSTYLE, dwNewStyle);
252 }
253 }
254
255protected:
256
261 virtual void PreAttach(HWND hWnd) {}
262
266 virtual void OnAttached(void) {}
267
271 virtual void PreDetach(void) {}
272
276 virtual void OnDetached(void) {}
277
286 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
287 {
288 LRESULT r = ::CallWindowProc(m_pOrgWndProc, m_hWnd, message, wParam, lParam);
289 if ( message == WM_DESTROY )
290 {
291 Detach();
292 }
293 return r;
294 }
295
296 HWND m_hWnd;
297
298private:
299 // WNDPROC設定
300 WNDPROC m_SetWndProc(HWND hWnd, WNDPROC P)
301 {
302 LONG_PTR i = reinterpret_cast<LONG_PTR>(P);
303 return reinterpret_cast<WNDPROC>(::SetWindowLongPtr(hWnd, GWLP_WNDPROC, i));
304 }
305
310 static LRESULT CALLBACK ms_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
311 {
312 if ( s_pmapWphHwndProc != NULL )
313 {
314 CWindowProcedureHooker* P = static_cast<CWindowProcedureHooker*>((*s_pmapWphHwndProc)[hWnd]);
315 if ( P != NULL )
316 {
317 return P->WindowProc(message, wParam, lParam);
318 }
319 }
320 return 0;
321 }
322 //Unhook処理用サブクラス
323 class CReject
324 {
325 public:
327 ~CReject(void)
328 {
329 if ( s_pmapWphHwndProc != NULL )
330 {
331// ASSERT( s_pmapWphHwndProc->GetSize() == 0 );
332 s_pmapWphHwndProc->RemoveAll();
333 delete s_pmapWphHwndProc;
334 s_pmapWphHwndProc = NULL;
335 }
336 }
337 };
338 WNDPROC m_pOrgWndProc;
339};
340
341
342
343}; //TNB
簡易配列型情報管理関係のヘッダ
マップ型情報管理テンプレート
Definition: TnbSimpleMap.h:44
ウィンドウプロシージャフッククラス
DWORD GetStyle(void) const
[取得] スタイル取得
void ModifyStyle(DWORD dwRemove, DWORD dwAdd)
[変更] スタイル変更
void ModifyStyleEx(DWORD dwRemove, DWORD dwAdd)
[変更] 拡張スタイル変更
HWND m_hWnd
管理しているウィンドウ
CWindowProcedureHooker(void)
コンストラクタ
virtual void OnDetached(void)
[通知] デタッチ通知
LRESULT SendMessage(UINT message, WPARAM wParam, LPARAM lParam)
[処理] SendMessage.
void Destroy(void)
[設定] 破棄
virtual void PreAttach(HWND hWnd)
[通知] アタッチ直前通知
void Detach(void)
[設定] デタッチ
virtual void OnAttached(void)
[通知] アタッチ通知
BOOL PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
[処理] PostMessage.
DWORD GetExStyle(void) const
[取得] 拡張スタイル取得
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
[通知] for processing Windows messages.
bool IsAttached(void) const
[確認] アタッチ中?
~CWindowProcedureHooker(void)
デストラクタ
bool Attach(HWND hWnd)
[設定] アタッチ
virtual void PreDetach(void)
[通知] デタッチ直前通知
HWND GetSafeHwnd(void) const
[取得] ウィンドウハンドル取得
void IgnoreUnusedValue(const T &value)
[宣言] 参照しない値宣言.
Definition: TnbDef.h:434
TNB Library
Definition: TnbDoxyTitle.txt:2