TNB Library
TnbMfcPressButton.h
[詳解]
1#pragma once
11#include "TnbMfcCommon.h"
12
13
14
15#ifdef _WIN32_WCE
16 #error not support "PressButton"
17#endif // _WIN32_WCE
18
19
20
21//TNB Library
22namespace TNB {
23namespace MFC {
24
25
26
27#ifndef _TnbDOXYGEN //Document作成用シンボル
28 #ifndef BN_PUSHED
29 #define BN_PUSHED BN_HILITE
30 #endif
31 #ifndef BN_UNPUSHED
32 #define BN_UNPUSHED BN_UNHILITE
33 #endif
34#endif //_TnbDOXYGEN
35
36
37
54class CPressButton : public CButton
55{
56 DEFSUPER(CButton);
57 COLORREF m_colorBack;
58 bool m_boIsPushed;
59
60 void PostCommand(UINT uCmd)
61 {
63// CWnd* pcWnd = GetParent();
64// if ( pcWnd != NULL )
65// {
66// pcWnd->PostMessage(WM_COMMAND, uCmd * 0x10000 | GetDlgCtrlID(),
67// reinterpret_cast<LPARAM>(m_hWnd));
68// }
69 }
70 virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
71 {
72 return _super::OnCommand(wParam, lParam);
73 }
74
84 virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
85 {
86 if ( message == WM_LBUTTONDOWN )
87 {
88 if ( ! m_boIsPushed )
89 {
90 m_boIsPushed = true;
91 RedrawWindow();
92 PostCommand(BN_PUSHED);
93 }
94 return 0;
95 }
96 else if ( message == WM_LBUTTONUP )
97 {
98 if ( m_boIsPushed )
99 {
100 m_boIsPushed = false;
101 RedrawWindow();
102 PostCommand(BN_UNPUSHED);
103 PostCommand(BN_CLICKED);
104 }
105 return 0;
106 }
107 else if ( message == WM_LBUTTONDBLCLK )
108 {
109 if ( ! m_boIsPushed )
110 {
111 PostCommand(BN_PUSHED);
112 }
113 m_boIsPushed = false;
114 RedrawWindow();
115 PostCommand(BN_UNPUSHED);
116 PostCommand(BN_DOUBLECLICKED);
117 return 0;
118 }
119 else if ( message == WM_SETFOCUS || message == WM_KILLFOCUS )
120 {
121 UpdateWindow();
122 }
123 return _super::WindowProc(message, wParam, lParam);
124 }
125
131 virtual void PreSubclassWindow(void)
132 {
133 UINT uiDefBtnSte = _super::GetButtonStyle() & 0xFF;
134 if ( uiDefBtnSte == BS_PUSHBUTTON || uiDefBtnSte == BS_DEFPUSHBUTTON )
135 {
136 _super::SetButtonStyle(BS_AUTOCHECKBOX);
137 _super::ModifyStyle(0, BS_OWNERDRAW);
138 }
139 _super::PreSubclassWindow();
140 }
141
148 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
149 {
150 if ( lpDrawItemStruct->CtlType != ODT_BUTTON || lpDrawItemStruct->hwndItem != _super::GetSafeHwnd() )
151 {
152 return;
153 }
154 CDC *dc = CDC::FromHandle(lpDrawItemStruct->hDC);
155 dc->SaveDC();
156 UINT uItemState = lpDrawItemStruct->itemState;
157
158 //== ボタンフレーム描画
159 UINT uState = DFCS_BUTTONPUSH | 0x0800/*DFCS_TRANSPARENT*/;
160 if ( m_boIsPushed )
161 {
162 uState |= DFCS_PUSHED;
163 }
164 dc->DrawFrameControl(&(lpDrawItemStruct->rcItem), DFC_BUTTON, uState);
165 //===== 内枠FILL
166 if ( ToInt(m_colorBack) >= 0 )
167 {
168 CRect cRect = lpDrawItemStruct->rcItem;
169 cRect.DeflateRect(2, 2, 2, 2);
170 dc->FillSolidRect(cRect, m_colorBack);
171 }
172 //== 文字
173 CString strText;
174 GetWindowText(strText);
175 if ( ! strText.IsEmpty() )
176 {
177 CRect cRect = lpDrawItemStruct->rcItem;
178 cRect.DeflateRect(4, 4, 4, 4);
179 if ( m_boIsPushed )
180 {
181 cRect.OffsetRect(1, 1);
182 }
183 dc->SetBkMode(TRANSPARENT); // バックカラーは変更なし
184 if ( (uItemState & ODS_DISABLED) != 0 )
185 {
186 dc->SetTextColor(GetSysColor(COLOR_GRAYTEXT)); //グレイ
187 }
188 dc->DrawText(strText, cRect, DT_CENTER | DT_VCENTER);
189 }
190// //== フォーカス点線
191// if ( ::GetFocus() == m_hWnd )
192// {
193// CRect cRect = lpDrawItemStruct->rcItem;
194// cRect.DeflateRect(3,3,3,3);
195// if ( m_boIsPushed )
196// {
197// cRect.OffsetRect(1,1);
198// }
199// dc->DrawFocusRect(&cRect);
200// }
201 dc->RestoreDC(-1);
202 }
203
204public:
205
208 : m_colorBack(COLORREF(-1))
209 , m_boIsPushed(false)
210 {
211 }
212
218 void SetBkColor(COLORREF color = -1)
219 {
220 m_colorBack = color;
221 }
222
228 bool IsPushed(void) const
229 {
230 return m_boIsPushed;
231 }
232
236 void Push(void)
237 {
238 PostMessage(WM_LBUTTONDOWN);
239 }
240
244 void Unpush(void)
245 {
246 PostMessage(WM_LBUTTONUP);
247 }
248
249};
250
251
252
253}; //MFC
254}; //TNB
255
256
257
MFCコントロール共通のヘッダ
ボタンコントロール.
ボタンコントロール
void Unpush(void)
[設定] ボタン非押下状態
void SetBkColor(COLORREF color=-1)
[設定] ボタン色
bool IsPushed(void) const
[取得] ボタン状態
CPressButton(void)
コンストラクタ
void Push(void)
[設定] ボタン押下状態
LRESULT SendCommandMessage(CWnd *pCtrl, UINT cmd)
[処理] WM_COMMAND送信.
Definition: TnbMfcCommon.h:475
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
TNB Library
Definition: TnbDoxyTitle.txt:2