TNB Library
TnbMfcCommon.h
[詳解]
1#pragma once
11#include "TnbDynamicFunc.h"
12#include "TnbTextDrawer.h"
13#include "TnbBitmapImage.h"
14
15
16#ifndef __AFX_H__
17 #error
18#endif
19
20
21
22//TNB Library
23namespace TNB {
24namespace MFC {
25
26
27
29namespace MFCLIB
30{
31
42 inline UINT _deprecated StaticToDrawStyle(DWORD staticStyle)
43 {
44 return CTextDrawer::StaticToDrawStyle(staticStyle);
45 }
46
59 inline bool SetControlPos(CWnd* pWnd, int x, int y, bool boIsRepaint = false)
60 {
61 CWnd* pParent = pWnd->GetParent();
62 if ( pParent == NULL ) { return false; }
63 CRect rc;
64 pWnd->GetWindowRect(&rc);
65 pParent->ScreenToClient(&rc);
66 int xx = (x < 0) ? rc.left : x;
67 int yy = (y < 0) ? rc.top : y;
68 pWnd->MoveWindow(xx, yy, rc.Width(), rc.Height(), boIsRepaint);
69 return true;
70 }
71
80 inline bool GetControlPos(CWnd* pWnd, POINT& _po)
81 {
82 _po.x = _po.y = 0;
83 if ( ! ::IsWindow(pWnd->GetSafeHwnd()) ) { return false; }
84 CWnd* pParent = pWnd->GetParent();
85 if ( pParent == NULL ) { return false; }
86 CRect rc;
87 pWnd->GetWindowRect(&rc);
88 pParent->ScreenToClient(&rc);
89 _po = rc.TopLeft();
90 return true;
91 }
92
102 inline bool ChangeClientSize(CWnd* pWnd, int cx = -1, int cy = -1)
103 {
104 CRect rcWin;
105 CRect rcCli;
106 pWnd->GetWindowRect(&rcWin);
107 pWnd->GetClientRect(&rcCli);
108 int wx = rcWin.Width() - rcCli.Width();
109 int wy = rcWin.Height() - rcCli.Height();
110 int xx = (cx < 0) ? rcCli.Width() : cx;
111 int yy = (cy < 0) ? rcCli.Height() : cy;
112 return !! pWnd->SetWindowPos(NULL, 0, 0, wx + xx, wy + yy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
113 }
114
121 inline CPoint GetClientPoint(CWnd* pWnd)
122 {
123 CRect winRc;
124 pWnd->GetWindowRect(&winRc);
125 CRect cliRc;
126 pWnd->GetClientRect(&cliRc);
127 pWnd->ClientToScreen(&cliRc);
128 return cliRc.TopLeft() - winRc.TopLeft();
129 }
130
137 inline void DrawBox(CDC* pDC, const RECT& rect, CPen& pen)
138 {
139 CPen* pOldPen = pDC->SelectObject(&pen);
140 pDC->MoveTo(rect.left, rect.top);
141 pDC->LineTo(rect.right - 1, rect.top);
142 pDC->LineTo(rect.right - 1, rect.bottom - 1);
143 pDC->LineTo(rect.left, rect.bottom - 1);
144 pDC->LineTo(rect.left, rect.top);
145 pDC->SelectObject(pOldPen);
146 }
147
156 inline void DrawBox(CDC* pDC, const RECT& rect, int nPenStyle, int nWidth, COLORREF color)
157 {
158 CPen pen;
159 pen.CreatePen(nPenStyle, nWidth, color);
160 DrawBox(pDC, rect, pen);
161 }
162
169 inline void DrawDottedBox(CDC* pDC, const RECT& rect, COLORREF color)
170 {
171 DrawBox(pDC, rect, PS_DOT, 1, color);
172 }
173
180 inline void DrawParent(CWnd* pWndControl, bool boIsRedraw = true)
181 {
182 if ( pWndControl != NULL )
183 {
184 CRect rect;
185 pWndControl->GetWindowRect(&rect);
186 CWnd* pWnd = pWndControl->GetParent();
187 if ( pWnd != NULL )
188 {
189 pWnd->ScreenToClient(&rect);
190 if ( boIsRedraw )
191 {
192 pWnd->RedrawWindow(&rect);
193 }
194 else
195 {
196 pWnd->InvalidateRect(&rect);
197 }
198 }
199 }
200 }
201
215 inline void AdjustClientSize(CWnd* pWnd, CDC* pDC, UINT drawStyle, LPCTSTR lpsz, int margin = 2)
216 {
217 CString str = lpsz;
218 if ( str.IsEmpty() ) { return; }
219 CRect rect;
220 pWnd->GetClientRect(&rect);
221 CSize sz(rect.Width(), 0);
222 if ( CTextDrawer::CalcTextSize(sz, *pDC, drawStyle, str) )
223 {
224 if ( margin >= 0 )
225 {
226 ChangeClientSize(pWnd, sz.cx + margin, sz.cy + margin);
227 }
228 else
229 {
230 CPoint po;
231 ChangeClientSize(pWnd, sz.cx - margin * 2, sz.cy - margin * 2);
232 GetControlPos(pWnd, po);
233 SetControlPos(pWnd, po.x + margin, po.y + margin);
234 }
235 }
236 }
237
238#ifndef _WIN32_WCE
251 inline void AdjustClientSize(CDC* pDC, UINT drawStyle, LPCTSTR lpsz, int margin = 2)
252 {
253 AdjustClientSize(pDC->GetWindow(), pDC, drawStyle, lpsz, margin);
254 }
255#endif
256
266 inline CBitmapHandle GetParentImage(CWnd* pWndControl, COLORREF backColor = CLR_INVALID)
267 {
269 if ( pWndControl != NULL )
270 {
271 CWnd* pWnd = pWndControl->GetParent();
272 if ( pWnd != NULL )
273 {
274 CRect rc;
275 pWndControl->GetClientRect(rc);
276 pWndControl->ClientToScreen(rc);
277 pWnd->ScreenToClient(rc);
278 CBitmapImage bi;
279 bi.SetFromClient(*pWnd, rc, backColor);
280 b = bi.GetBitmapHandle();
281 }
282 }
283 return b;
284 }
285
297 inline void SetTextForStatic(CWnd* pWnd, LPCTSTR lpsz, int margin = 2)
298 {
299 CString str = lpsz;
300 pWnd->SetWindowText(str);
301 if ( str.IsEmpty() ) { return; }
302 UINT drawStyle = CTextDrawer::StaticToDrawStyle(pWnd->GetStyle());
303 CDC* pDC = pWnd->GetDC();
304 CGdiObject* pOld = pDC->SelectObject(pWnd->GetFont());
305 AdjustClientSize(pWnd, pDC, drawStyle, lpsz, margin);
306 pDC->SelectObject(pOld);
307 pWnd->ReleaseDC(pDC);
308 }
309
347 inline int SelectClipRgnOrg(CDC* pDC, HRGN rgn, int mode = RGN_COPY)
348 {
349 int r = ERROR;
350 CPoint offset = pDC->GetWindowOrg();
351 if ( offset.x == 0 && offset.y == 0 )
352 {
353 r = ::ExtSelectClipRgn(*pDC, rgn, mode);
354 }
355 else
356 {
357 HRGN tmp= ::CreateRectRgn(0, 0, 0, 0);
358 ::CombineRgn(tmp, rgn, NULL, RGN_COPY);
359 ::OffsetRgn(tmp, -offset.x, -offset.y);
360 r = ::ExtSelectClipRgn(*pDC, tmp, mode);
361 _DeleteObject(tmp);
362 }
363 return r;
364 }
365
371 {
375 };
376
389 inline bool SetLayeredWindow(CWnd* pWnd, COLORREF crKey = 0, BYTE bAlpha = 255, DWORD dwFlags = E_Alpha | E_SetLayeredStyle)
390 {
391 bool r = false;
392 if ( (dwFlags & E_SetLayeredStyle) != 0 )
393 {
394 dwFlags &= ~E_SetLayeredStyle;
395 pWnd->ModifyStyleEx(0, WS_EX_LAYERED);
396 }
397 try
398 {
399 typedef BOOL (__stdcall *PSETLAYEREDATTR)(HWND, COLORREF, BYTE, DWORD);
400 r = !! CDynamicFuncT<PSETLAYEREDATTR>("user32.dll", "SetLayeredWindowAttributes")()
401 (pWnd->m_hWnd, crKey, bAlpha, dwFlags);
402 }
403 catch (CNullPointerException& e)
404 {
405 e.OnCatch();
406 r = false;
407 }
408 return r;
409 }
410
411 #ifndef _TnbDOXYGEN //Document作成用シンボル
412 enum
413 {
414 _TnbProcessingFlag = WS_SYSMENU,
415 _TnbWndLong = GWL_STYLE
416 };
417
418 #endif
419
427 inline void SetProcessingFlag(HWND hWnd, bool isEnable)
428 {
429 LONG l = ::GetWindowLong(hWnd, _TnbWndLong);
430 if ( isEnable )
431 {
432 l |= _TnbProcessingFlag;
433 }
434 else
435 {
436 l &= ~_TnbProcessingFlag;
437 }
438 ::SetWindowLong(hWnd, _TnbWndLong, l);
439 ASSERT( l == ::GetWindowLong(hWnd, _TnbWndLong) );
440 }
441
450 inline bool IsProcessingFlag(HWND hWnd)
451 {
452 LONG l = ::GetWindowLong(hWnd, _TnbWndLong);
453 return (l & _TnbProcessingFlag) != 0;
454 }
455
459 inline void PumpMessage(void)
460 {
461 MSG msg;
462 while ( ::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE) )
463 {
464 AfxGetThread()->PumpMessage();
465 }
466 }
467
475 inline LRESULT SendCommandMessage(CWnd* pCtrl, UINT cmd)
476 {
477 CWnd* pParent = pCtrl->GetParent();
478 if ( pParent != NULL && ::IsWindow(pParent->GetSafeHwnd()) )
479 {
480 WPARAM wp = MAKEWPARAM(pCtrl->GetDlgCtrlID(), cmd);
481 LPARAM lp = reinterpret_cast<LPARAM>(pCtrl->GetSafeHwnd());
482 return pParent->SendMessage(WM_COMMAND, wp, lp);
483 }
484 return 0;
485 }
486
490}; //MFCLIB
491
492
493
507{
508public:
515 virtual bool OnFound(HWND hWnd) = 0;
516
525 bool Execute(HWND hWnd, bool bDeep = false)
526 {
527 for (HWND hChild = ::GetTopWindow(hWnd); hChild != NULL; hChild = ::GetNextWindow(hChild, GW_HWNDNEXT))
528 {
529 if ( ! OnFound(hChild) )
530 {
531 return false;
532 }
533 if (bDeep && ::GetTopWindow(hChild) != NULL)
534 {
535 // send to child windows after parent
536 if ( ! Execute(hChild, bDeep) )
537 {
538 return false;
539 }
540 }
541 }
542 return true;
543 }
544};
545
546
547
560{
561protected:
563 struct TColor
564 {
565 COLORREF forward;
566 COLORREF back;
568 TColor(void) : forward(::GetSysColor(COLOR_WINDOWTEXT)), back(CLR_INVALID) { }
570 TColor(COLORREF c) : forward(c), back(CLR_INVALID) { }
571 };
574 CPoint m_offset;
575
585 virtual void DrawText(CDC* pDC, const RECT& rect, UINT drawStyle, const TColor& c, LPCTSTR str)
586 {
587 if ( (drawStyle & DT_VCENTER) != 0 )
588 {
589 drawStyle &= ~DT_VCENTER;
590 int h = rect.bottom - rect.top;
591 CSize sz(rect.right - rect.left, h);
592 CTextDrawer::CalcTextSize(sz, *pDC, drawStyle, str);
593 CRect rc = rect;
594 rc.top += (h - sz.cy) / 2;
595 CTextDrawer::DrawTextRect(*pDC, rc, drawStyle, m_offset, c.forward, c.back, str);
596 return;
597 }
598 CTextDrawer::DrawTextRect(*pDC, rect, drawStyle, m_offset, c.forward, c.back, str);
599 }
600
610 void DrawTextEx(CDC* pDC, const RECT& rect, UINT drawStyle, bool boIsEnable, LPCTSTR str)
611 {
612 DrawText(pDC, rect, drawStyle, (boIsEnable ? m_normalColors : m_disableColors), str);
613 }
614
615public:
616
619 {
620 }
621
630 void SetTextDeepOffset(int x, int y)
631 {
632 m_offset.x = x;
633 m_offset.y = y;
634 }
635
641 void SetTextColor(COLORREF color1, COLORREF color2 = CLR_INVALID)
642 {
643 m_normalColors.forward = color1;
644 m_normalColors.back = color2;
645 }
646
652 void SetTextDisableColor(COLORREF color1, COLORREF color2 = CLR_INVALID)
653 {
654 m_disableColors.forward = color1;
655 m_disableColors.back = color2;
656 }
657};
658
659
660
661}; //MFC
662}; //TNB
663
664
ビットマップイメージ管理関係のヘッダ
#define _BIT(X)
BIT演算
Definition: TnbDef.h:307
動的関数管理関係のヘッダ
テキスト描画関係のヘッダ
ウィンドウ管理.
DWORD GetStyle(void) const
[取得] ウィンドウスタイル取得.
BOOL SetWindowPos(const CWnd *pWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags)
[設定] ウィンドウサイズポジション設定.
BOOL ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags=0)
[設定] ウィンドウ拡張スタイル変更.
HWND GetSafeHwnd(void) const
[取得] ウィンドウハンドル取得.
HBITMAP型ハンドルハンドル
ビットマップイメージ管理クラス
bool SetFromClient(HWND hWnd, const RECT &rect, COLORREF backColor=CLR_INVALID)
[設定] クライアントからイメージ設定.
CBitmapHandle GetBitmapHandle(void)
[取得] ビットマップハンドル取得
動的関数管理クラステンプレート
NULLポインタ例外
Definition: TnbException.h:172
static bool CalcTextSize(SIZE &_size, HDC dc, UINT drawStyle, LPCTSTR str)
[計算] 文字表示大きさ計算.
static bool DrawTextRect(RECT &_rect, HDC dc, DWORD drawStyle, const POINT &offset, COLORREF color1, COLORREF color2, LPCTSTR str)
[表示] 範囲文字表示.
static DWORD StaticToDrawStyle(DWORD staticStyle)
[変換] StaticスタイルをDrawTextスタイルに変換
void OnCatch(void) const
[表示] 内容表示
Definition: TnbException.h:69
テキスト描画ベースクラス
Definition: TnbMfcCommon.h:560
virtual void DrawText(CDC *pDC, const RECT &rect, UINT drawStyle, const TColor &c, LPCTSTR str)
[表示] 文字表示.
Definition: TnbMfcCommon.h:585
void SetTextDisableColor(COLORREF color1, COLORREF color2=CLR_INVALID)
[設定] 無効状態テキスト色指定.
Definition: TnbMfcCommon.h:652
CPoint m_offset
文字と文字のずれ
Definition: TnbMfcCommon.h:574
void SetTextDeepOffset(int x, int y)
[設定] テキストオフセット設定.
Definition: TnbMfcCommon.h:630
void SetTextColor(COLORREF color1, COLORREF color2=CLR_INVALID)
[設定] テキスト色指定.
Definition: TnbMfcCommon.h:641
TColor m_normalColors
標準状態色
Definition: TnbMfcCommon.h:572
void DrawTextEx(CDC *pDC, const RECT &rect, UINT drawStyle, bool boIsEnable, LPCTSTR str)
[表示] 文字表示.
Definition: TnbMfcCommon.h:610
TColor m_disableColors
無効状態色
Definition: TnbMfcCommon.h:573
CDrawTextBase(void)
コンストラクタ
Definition: TnbMfcCommon.h:618
コントロール一括処理クラス
Definition: TnbMfcCommon.h:507
virtual bool OnFound(HWND hWnd)=0
[通知] 発見.
bool Execute(HWND hWnd, bool bDeep=false)
[実行] 検索開始.
Definition: TnbMfcCommon.h:525
void DrawDottedBox(CDC *pDC, const RECT &rect, COLORREF color)
[表示] 破線箱表示.
Definition: TnbMfcCommon.h:169
void DrawBox(CDC *pDC, const RECT &rect, CPen &pen)
[表示] 箱表示.
Definition: TnbMfcCommon.h:137
void AdjustClientSize(CWnd *pWnd, CDC *pDC, UINT drawStyle, LPCTSTR lpsz, int margin=2)
[設定] クライアントサイズ調整.
Definition: TnbMfcCommon.h:215
ELayeredAction
アクションフラグ値.
Definition: TnbMfcCommon.h:371
bool GetControlPos(CWnd *pWnd, POINT &_po)
[取得] コントロール位置取得.
Definition: TnbMfcCommon.h:80
void SetProcessingFlag(HWND hWnd, bool isEnable)
[設定] 処理済フラグ操作.
Definition: TnbMfcCommon.h:427
bool IsProcessingFlag(HWND hWnd)
[取得] 処理済フラグ取得.
Definition: TnbMfcCommon.h:450
void SetTextForStatic(CWnd *pWnd, LPCTSTR lpsz, int margin=2)
[設定] STATIC 文字列設定&サイズ調整.
Definition: TnbMfcCommon.h:297
CBitmapHandle GetParentImage(CWnd *pWndControl, COLORREF backColor=CLR_INVALID)
[取得] 親画像取得.
Definition: TnbMfcCommon.h:266
LRESULT SendCommandMessage(CWnd *pCtrl, UINT cmd)
[処理] WM_COMMAND送信.
Definition: TnbMfcCommon.h:475
UINT _deprecated StaticToDrawStyle(DWORD staticStyle)
[変換] StaticスタイルをDrawTextスタイルに変換
Definition: TnbMfcCommon.h:42
CPoint GetClientPoint(CWnd *pWnd)
[取得] クライアント位置取得.
Definition: TnbMfcCommon.h:121
bool ChangeClientSize(CWnd *pWnd, int cx=-1, int cy=-1)
[設定] クライアントサイズ設定.
Definition: TnbMfcCommon.h:102
void DrawParent(CWnd *pWndControl, bool boIsRedraw=true)
[表示] 親表示.
Definition: TnbMfcCommon.h:180
bool SetControlPos(CWnd *pWnd, int x, int y, bool boIsRepaint=false)
[設定] コントロール位置設定.
Definition: TnbMfcCommon.h:59
int SelectClipRgnOrg(CDC *pDC, HRGN rgn, int mode=RGN_COPY)
[設定] リージョン設定.
Definition: TnbMfcCommon.h:347
void PumpMessage(void)
[処理] メッセージポンプ
Definition: TnbMfcCommon.h:459
bool SetLayeredWindow(CWnd *pWnd, COLORREF crKey=0, BYTE bAlpha=255, DWORD dwFlags=E_Alpha|E_SetLayeredStyle)
[設定] SetLayeredWindow.
Definition: TnbMfcCommon.h:389
@ E_SetLayeredStyle
WS_EX_LAYERED スタイルを付加します。
Definition: TnbMfcCommon.h:374
@ E_ColorKey
透明色として crKey を使います。
Definition: TnbMfcCommon.h:372
@ E_Alpha
bAlpha を使って、レイヤードウィンドウの不透明度を決定します。
Definition: TnbMfcCommon.h:373
TNB Library
Definition: TnbDoxyTitle.txt:2
カラーセット型
Definition: TnbMfcCommon.h:564
COLORREF forward
前の文字の色
Definition: TnbMfcCommon.h:565
TColor(void)
コンストラクタ
Definition: TnbMfcCommon.h:568
COLORREF back
後ろの文字の色
Definition: TnbMfcCommon.h:566
TColor(COLORREF c)
コンストラクタ
Definition: TnbMfcCommon.h:570