The following code represents my solution. It will be interesting to see how different it is from the future sample code.
// Patrick // CCustomItemCommandBtn // 6/14/05
class CCustomItemCommandBtn;
class CInplaceCommandBtn : public CButton { public: afx_msg LRESULT OnClick(WPARAM wParam, WPARAM lParam); afx_msg HBRUSH CtlColor(CDC* pDC, UINT /*nCtlColor*/); afx_msg void OnMove(int x, int y);
DECLARE_MESSAGE_MAP()
protected: CCustomItemCommandBtn* m_pItem; COLORREF m_clrBack; CBrush m_brBack;
friend class CCustomItemCommandBtn; };
class CCustomItemCommandBtn : public CXTPPropertyGridItem { public: CCustomItemCommandBtn(CString strCaption, LPCTSTR buttonTxt = NULL, HICON hIcon = NULL);
protected: CString m_buttonTxt; HICON m_hIcon; void Create();
protected: virtual BOOL OnDrawItemValue(CDC& dc, CRect rcValue); virtual void SetVisible(BOOL bVisible); private: CInplaceCommandBtn m_wndButton;
friend class CInplaceCommandBtn; };
//Patrick
// CCustomItemCommandBtn // 6/14/05
BEGIN_MESSAGE_MAP(CInplaceCommandBtn, CButton) ON_MESSAGE(BM_SETSTATE, OnClick) ON_WM_CTLCOLOR_REFLECT() ON_WM_MOVE() END_MESSAGE_MAP()
HBRUSH CInplaceCommandBtn::CtlColor(CDC* pDC, UINT /*nCtlColor*/) { class CGridView : public CXTPPropertyGridView { friend class CInplaceCommandBtn; };
CGridView* pGrid = (CGridView*)m_pItem->m_pGrid;
COLORREF clr = pGrid->m_clrBack;
if (clr != m_clrBack || !m_brBack.GetSafeHandle()) { m_brBack.DeleteObject(); m_brBack.CreateSolidBrush(clr); m_clrBack = clr; }
pDC->SetBkColor(m_clrBack); return m_brBack; }
LRESULT CInplaceCommandBtn::OnClick(WPARAM wParam, WPARAM lParam) { if (!wParam) m_pItem->OnValueChanged(_T("")); return CButton::DefWindowProc(BM_SETSTATE, wParam, lParam); }
void CInplaceCommandBtn::OnMove(int, int) { Invalidate(); }
CCustomItemCommandBtn::CCustomItemCommandBtn(CString strCaption, LPCTSTR buttonTxt /*=NULL*/, HICON hIcon/*=NULL*/) : CXTPPropertyGridItem(strCaption) { m_wndButton.m_pItem = this; m_nFlags = 0; m_buttonTxt = buttonTxt; m_hIcon = hIcon; }
void CCustomItemCommandBtn::Create() { DWORD style = WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE | (m_hIcon ? BS_ICON : 0); m_wndButton.Create(m_buttonTxt, style, GetValueRect(), (CWnd*)m_pGrid, 0); if (m_hIcon) { m_wndButton.SetIcon(m_hIcon); } }
BOOL CCustomItemCommandBtn::OnDrawItemValue(CDC&, CRect) { if (!m_wndButton.m_hWnd) { Create(); } m_wndButton.MoveWindow(GetValueRect()); return TRUE; }
void CCustomItemCommandBtn::SetVisible(BOOL bVisible) { CXTPPropertyGridItem::SetVisible(bVisible); if (m_wndButton.m_hWnd) { m_wndButton.ShowWindow(bVisible ? SW_SHOW : SW_HIDE); } else { Create(); } }
|