Objective Grid: How to change the face color of a push button.

Article ID: 637
Last updated: 11 Apr, 2018
Article ID: 637
Last updated: 11 Apr, 2018
Revision: 3
Views: 3232
Posted: 11 Jan, 2001
by Meltreger B.
Updated: 11 Apr, 2018
by Meltreger B.

Problem


The GX_IDS_CTRL_PUSHBTN control uses the system COLOR_BTNFACE color for its button face. This article shows how to derive a control from CGXPushButton to use the color specified by the cell style's SetInterior() function to determine the button's face color.


Cause



Action


The following code derives a control from CGXPushButton overriding its Draw() method and getting the face color from the cell style object if it is available.

// declarations
class CGXMyPushbutton: public CGXPushbutton
{
	DECLARE_CONTROL(CGXMyPushbutton)
	public:
	CGXMyPushbutton(CGXGridCore* pGrid) :CGXPushbutton(pGrid){};

	virtual void Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle);
 
};

//implementation
IMPLEMENT_CONTROL(CGXMyPushbutton, CGXPushbutton) 


void CGXMyPushbutton::Draw(CDC* pDC, CRect rect, ROWCOL nRow, ROWCOL nCol, const CGXStyle& style, const CGXStyle* pStandardStyle)
{
	if (rect.right <= rect.left || rect.Width() <= 1 || rect.Height() <= 1)
			return;

	// Select font
	CFont* pOldFont = LoadFont(pDC, style, pStandardStyle);

	// Borders
	DrawFrame(pDC, rect, style);

	pDC->SetTextColor(style.GetTextColor());

	WORD nState = 0;

	CGXButton* pBut = (CGXButton*) m_pButton;


	BOOL bActive = Grid()->IsCurrentCell(nRow, nCol) && !Grid()->IsPrinting() && Grid()->IsActiveFrame();

	if (bActive && pBut->HasFocus())
	{
		nState |= GX_BTNFOCUS;
		nState |= GX_BTNPRESSED;
	}

	COLORREF rgbFace = GXGetSysData()->GetSysColor(COLOR_BTNFACE);
	
	if( style.GetIncludeInterior())
	{
		rgbFace = style.GetInterior().GetColor();
	}

	GXPatB(pDC, rect, rgbFace);

	if (nState & GX_BTNPRESSED)
		GXDrawEdge(pDC, rect, BDR_SUNKENOUTER | BDR_SUNKENINNER);
	else
		GXDrawEdge(pDC, rect, BDR_RAISEDOUTER | BDR_RAISEDINNER);

	CRect faceRect(rect.left+1, rect.top+1, rect.right-2, rect.bottom-2);
	if (nState & GX_BTNPRESSED)
		faceRect += CPoint(1,1);

	GXDrawFocusText(pDC, faceRect, nState & GX_BTNFOCUS, style.GetChoiceListRef(), DT_WORDBREAK);

	if (pOldFont)
		pDC-&SelectObject(pOldFont);
}


// then in your OnInitialUpdate, 
     RegisterControl ( IDS_CTRL_MYBUTTON, new CGXMyPushbutton(this) );

     SetStyleRange( CGXRange( 6, 2),
             CGXStyle().SetControl( IDS_CTRL_MYBUTTON ) 
                          .SetChoiceList(_T(Yellow))   
		          .SetInterior(RGB(255,255,0))
			 );
     SetStyleRange( CGXRange( 6, 1 ),
             CGXStyle().SetControl( IDS_CTRL_MYBUTTON ) 
			   .SetChoiceList(_T(Green))
			   .SetInterior(RGB(0,255,0))
			 );

This article was:   Helpful | Not helpful
Report an issue
Article ID: 637
Last updated: 11 Apr, 2018
Revision: 3
Views: 3232
Posted: 11 Jan, 2001 by Meltreger B.
Updated: 11 Apr, 2018 by Meltreger B.

Others in this category