TNB Library
TnbDummyWindow.h
[詳解]
1#pragma once
11#include "TnbSync.h"
12#include "TnbSimpleMap.h"
13#include "TnbStr.h"
14
15
16
17//TNB Library
18namespace TNB
19{
20
21
22
23#ifndef _TnbDOXYGEN //Document作成用シンボル
24 // HWNDとリスナーのMAP
25 _SELECTANY CSimpleMapT<LPVOID, LPVOID>* s_pmapDmyWndHwndProc = NULL;
26 _SELECTANY CSyncSection* s_psyncDmyWnd = NULL;
27
28 #ifndef WS_OVERLAPPEDWINDOW
29 #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX)
30 #endif
31#endif
32
33
34
56{
57public:
58
69 struct IListener
70 {
72 virtual ~IListener(void) {}
85 virtual bool OnWindowMessage(LRESULT& _result, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
86 };
87
88
89 //----
90
91
93 CDummyWindow(void) : m_hWnd(NULL)
94 {
95 m_brush = static_cast<HBRUSH>(::GetStockObject(NULL_BRUSH));
96 }
97
100 {
101 Destroy();
102 }
103
109 operator HWND(void) const
110 {
111 return m_hWnd;
112 }
113
119 HWND GetSafeHwnd(void) const
120 {
121 return m_hWnd;
122 }
123
129 bool IsWindow(void) const
130 {
131 return !! ::IsWindow(m_hWnd);
132 }
133
137 void Destroy(void)
138 {
139 if ( m_hWnd != NULL )
140 {
141 ::DestroyWindow(m_hWnd);
142 m_hWnd = NULL;
143 }
144 m_UnregisterClass();
145 m_className.Empty();
146 }
147
154 void SetBrush(HBRUSH b)
155 {
156 m_brush = b;
157 }
158
175 bool Create(CDummyWindow::IListener* I, LPCTSTR lpszTitle = NULL, LPCTSTR lpszClassName = NULL, DWORD dwStyle = WS_OVERLAPPEDWINDOW, HMENU hMenu = NULL, HWND hParent = NULL)
176 {
177 if ( I != NULL )
178 {
179 m_CreateSub();
180 if ( lpszTitle == NULL )
181 {
182 lpszTitle = _T("");
183 }
184 m_className = lpszClassName;
185 if ( m_className.IsEmpty() )
186 {
187 m_className.Format(_T("TnbLib:%d:%d"), ::GetCurrentProcessId(), this);
188 }
189 m_RegisterClass();
190 HINSTANCE hInst = GetInstanceHandleByTnb(EI_Process);
191 m_hWnd = ::CreateWindow(m_className, lpszTitle, dwStyle, 100, 0, 100, 0, hParent, hMenu, hInst, NULL);
192 if ( m_hWnd != NULL )
193 {
194 (*s_pmapDmyWndHwndProc)[m_hWnd] = I;
195 return true;
196 }
197 }
198 return false;
199 }
200
212 bool CreateByTemplate(CDummyWindow::IListener* I, UINT templateId, HWND hParent = NULL)
213 {
214 if ( I != NULL )
215 {
216 m_CreateSub();
217 HINSTANCE hInst = GetInstanceHandleByTnb(EI_Process);
218 HANDLE h = ::GetCurrentThread();
219 (*s_pmapDmyWndHwndProc)[h] = I; //仮キー
220 m_hWnd = ::CreateDialog(hInst, MAKEINTRESOURCE(templateId), hParent, ms_DlgProc);
221 if ( m_hWnd != NULL )
222 {
223 (*s_pmapDmyWndHwndProc)[m_hWnd] = I;
224 s_pmapDmyWndHwndProc->RemoveKey(h); //仮キー削除
225 return true;
226 }
227 }
228 return false;
229 }
230
235 DWORD GetStyle(void) const
236 {
237 return ::GetWindowLong(m_hWnd, GWL_STYLE);
238 }
239
244 DWORD GetStyleEx(void) const
245 {
246 return ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
247 }
248
257 bool ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0)
258 {
259 return m_ModifyStyle(GWL_STYLE, dwRemove, dwAdd, nFlags);
260 }
261
270 bool ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0)
271 {
272 return m_ModifyStyle(GWL_EXSTYLE, dwRemove, dwAdd, nFlags);
273 }
274
275private:
276
277#if 0
278 //Unhook処理用サブクラス
279 class CReject
280 {
281 public:
282 ~CReject(void)
283 {
284 if ( s_pmapDmyWndHwndProc != NULL )
285 {
286 s_pmapDmyWndHwndProc->RemoveAll();
287 delete s_pmapDmyWndHwndProc;
288 s_pmapDmyWndHwndProc = NULL;
289 }
290 if ( s_psyncDmyWnd != NULL )
291 {
292 delete s_psyncDmyWnd;
293 s_psyncDmyWnd = NULL;
294 }
295 }
296 };
297#endif
299 void m_CreateSub(void)
300 {
301 if ( s_pmapDmyWndHwndProc == NULL )
302 {
303// static CReject s_cReje;
304 s_pmapDmyWndHwndProc = new CSimpleMapT<LPVOID, LPVOID>;
305 s_psyncDmyWnd = new CSyncSection;
306 }
307 Destroy();
308 }
310 bool m_ModifyStyle(int nStyleOffset, DWORD dwRemove, DWORD dwAdd, UINT nFlags)
311 {
312 DWORD dwStyle = ::GetWindowLong(m_hWnd, nStyleOffset);
313 DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
314 if ( dwStyle == dwNewStyle )
315 {
316 return false;
317 }
318 ::SetWindowLong(m_hWnd, nStyleOffset, dwNewStyle);
319 if ( nFlags != 0 )
320 {
321 ::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | nFlags);
322 }
323 return true;
324 }
326 static bool CALLBACK ms_CmnProc(LRESULT& _result, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
327 {
328 if ( s_pmapDmyWndHwndProc == NULL )
329 {
330 return false;
331 }
332 s_psyncDmyWnd->Lock();
333 IListener* I = NULL;
334 if ( s_pmapDmyWndHwndProc->HasKey(hWnd) )
335 {
336 LPVOID P = (*s_pmapDmyWndHwndProc)[hWnd];
337 I = static_cast<IListener*>(P);
338 }
339 else
340 {
341 HANDLE h = ::GetCurrentThread();
342 if ( s_pmapDmyWndHwndProc->HasKey(h) )
343 {
344 LPVOID P = (*s_pmapDmyWndHwndProc)[h];
345 I = static_cast<IListener*>(P);
346 }
347 }
348 bool r = false;
349 if ( I != NULL )
350 {
351 r = I->OnWindowMessage(_result, hWnd, uMsg, wParam, lParam);
352 }
353 if ( uMsg == WM_DESTROY )
354 {
355 s_pmapDmyWndHwndProc->RemoveKey(hWnd);
356 if ( s_pmapDmyWndHwndProc->GetSize() == 0 )
357 {
358 if ( s_pmapDmyWndHwndProc != NULL )
359 {
360 delete s_pmapDmyWndHwndProc;
361 s_pmapDmyWndHwndProc = NULL;
362 }
363 if ( s_psyncDmyWnd != NULL )
364 {
365 s_psyncDmyWnd->Unlock();
366 delete s_psyncDmyWnd;
367 s_psyncDmyWnd = NULL;
368 }
369 }
370 }
371 if ( s_psyncDmyWnd != NULL )
372 {
373 s_psyncDmyWnd->Unlock();
374 }
375 return r;
376 }
377 static LRESULT CALLBACK ms_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
378 {
379 LRESULT r;
380 if ( ms_CmnProc(r, hWnd, uMsg, wParam, lParam) )
381 {
382 return r;
383 }
384 return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
385 }
387 static INT_PTR CALLBACK ms_DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
388 {
389 LRESULT r;
390 if ( ms_CmnProc(r, hWnd, uMsg, wParam, lParam) )
391 {
392 switch ( uMsg )
393 {
394 case WM_CHARTOITEM:
395 case WM_COMPAREITEM:
396 case WM_CTLCOLORBTN:
397 case WM_CTLCOLORDLG:
398 case WM_CTLCOLOREDIT:
399 case WM_CTLCOLORLISTBOX:
400 case WM_CTLCOLORSCROLLBAR:
401 case WM_CTLCOLORSTATIC:
402 case WM_CTLCOLORMSGBOX:
403 case WM_INITDIALOG:
404 case WM_QUERYDRAGICON:
405 case WM_VKEYTOITEM:
406 return r; // WM_CTLCOLOR〜 系などは DWL_MSGRESULT を使わず、戻り値で返す。
407 }
408 #ifdef DWL_MSGRESULT
409 ::SetWindowLong(hWnd, DWL_MSGRESULT, r);
410 #else
411 ::SetWindowLongPtr(hWnd, DWLP_MSGRESULT, r);
412 #endif
413 return TRUE;
414 }
415 return FALSE;
416 }
418 ATOM m_RegisterClass(void)
419 {
420 #ifndef _WIN32_WCE
421 WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
422 #else
423 WNDCLASS wc = { sizeof(WNDCLASS) };
424 #endif
425 wc.style = CS_HREDRAW | CS_VREDRAW;
426 wc.lpfnWndProc = ms_WndProc;
427 wc.hInstance = GetInstanceHandleByTnb(EI_Process);
428 wc.hbrBackground = m_brush;
429 wc.lpszClassName = m_className;
430 #ifndef _WIN32_WCE
431 return ::RegisterClassEx(&wc);
432 #else
433 return ::RegisterClass(&wc);
434 #endif
435 }
437 void m_UnregisterClass(void)
438 {
439 if ( ! m_className.IsEmpty() )
440 {
441 ::UnregisterClass(m_className, GetInstanceHandleByTnb(EI_Process));
442 m_className.Empty();
443 }
444 }
445 HWND m_hWnd;
446 CStr m_className;
447 HBRUSH m_brush;
448};
449
450
451
452
453};
簡易配列型情報管理関係のヘッダ
文字列管理関係のヘッダ
同期処理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
ダミーウィンドウクラス
DWORD GetStyle(void) const
[取得] スタイル取得.
~CDummyWindow(void)
デストラクタ
CDummyWindow(void)
コンストラクタ
bool ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags=0)
[設定] 拡張スタイル変更.
bool IsWindow(void) const
[確認] ウィンドウの有無.
DWORD GetStyleEx(void) const
[取得] 拡張スタイル取得.
void Destroy(void)
[操作] ウィンドウの破棄.
bool ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags=0)
[設定] スタイル変更.
void SetBrush(HBRUSH b)
[設定] 背景ブラシ設定.
bool Create(CDummyWindow::IListener *I, LPCTSTR lpszTitle=NULL, LPCTSTR lpszClassName=NULL, DWORD dwStyle=WS_OVERLAPPEDWINDOW, HMENU hMenu=NULL, HWND hParent=NULL)
[操作] ウィンドウの作成.
HWND GetSafeHwnd(void) const
[取得] ウィンドウハンドル.
bool CreateByTemplate(CDummyWindow::IListener *I, UINT templateId, HWND hParent=NULL)
[操作] ウィンドウの作成(テンプレート使用).
マップ型情報管理テンプレート
Definition: TnbSimpleMap.h:44
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
void Empty(void)
[削除] 空化
Definition: TnbStr.h:197
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
Definition: TnbStr.h:359
Section排他管理クラス
Definition: TnbSync.h:125
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.
Definition: TnbDef.h:1341
@ EI_Process
プロセス用
Definition: TnbDef.h:1259
TNB Library
Definition: TnbDoxyTitle.txt:2
ダミーウィンドウクラスのリスナーインターフェース
virtual ~IListener(void)
デストラクタ
virtual bool OnWindowMessage(LRESULT &_result, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)=0
[通知] 受信メッセージ通知.