Re: Problem with DrawItem
i posted with wrong variables ,heer is teh correct code.
void CPLFilterBtn::DrawItem(LPDRAWITEMSTRUCT dis/*lpDrawItemStruct*/)
{
CBrush defBrush, selBrush;
COLORREF black = RGB(0, 0, 0);
selBrush.CreateSolidBrush(black);
CBrush defBrush1, selBrushOrange;
COLORREF Orange = RGB(243,156,25);
selBrushOrange.CreateSolidBrush(Orange);
CDC * dc = CDC::FromHandle(dis->hDC);
int save = dc->SaveDC();
CRect r(dis->rcItem);
CPoint offset(0, 0);
if(dis->itemState & ODS_SELECTED)
{ /* pushed */
dc->SelectObject(&selBrushOrange);
dc->DrawEdge(&r, EDGE_SUNKEN, BDR_RAISEDINNER );
//dc->FillSolidRect(&r, RGB(243,156,25)); //orange
//offset = CPoint(::GetSystemMetrics(SM_CXEDGE),
::GetSystemMetrics(SM_CYEDGE));
FillRect(dis->hDC, &dis->rcItem, selBrushOrange);//orange
SetBkColor(dis->hDC, RGB(243,156,25)); //orange
} /* pushed */
else
{ /* unpushed */
dc->DrawEdge(&r, EDGE_RAISED, BDR_RAISEDOUTER);
//dc->FillSolidRect(&r, ::GetSysColor(COLOR_3DFACE));
FillRect(dis->hDC, &dis->rcItem, selBrush);
SetBkColor(dis->hDC, CLICKED_COLOR); //black
} /* unpushed */
CString text;
GetWindowText(text);
dc->SetTextColor(RGB(255, 0, 0));
r.left += offset.x;
r.top += offset.y;
dc->DrawText(text, &r, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
dc->RestoreDC(save);
// TODO: Add your code to draw the specified item
// TODO: Add your code to draw the specified item
}
Joseph M. Newcomer wrote:
Complicated code for a simple task.
void CFilterBtn::DrawItem(LPDRAWITEMSTRUCT dis)
{
CDC * dc = CDC::FromHandle(dis->hDC);
int save = dc->SaveDC();
CRect r(dis->rcItem);
CPoint offset(0, 0);
#define CLICKED_COLOR RGB(0, 10, 10)
if(dis->itemState & ODS_SELECTED)
{ /* pushed */
dc->DrawEdge(&r, EDGE_SUNKEN, ...flags...);
dc->FillSolidRect(&r, CLICKED_COLOR);
offset = CPoint(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
} /* pushed */
else
{ /* unpushed */
dc->DrawEdge(&r, EDGE_RAISED, ...flags...);
dc->FillSolidRect(&r, ::GetSysColor(COLOR_3DFACE));
} /* unpushed */
CString text;
GetWindowText(text);
dc->SetTextColor(RGB(255, 0, 0));
r.left += offset.cx;
r.top += offset.cy;
dc->DrawText(text, &r, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
dc->RestoreDC(save);
}
Many fewer lines, simpler lines, doesn't require raw Win32 API calls, and please take note
of the use of the offset. You may not have noticed, but when you click a button, the text
moves down and to the right.
You can see something more complicated in my generic "Better Bitmap Button", from my MVP
Tips site, which does a lot more work but is fully general.
joe
On 13 Jun 2006 15:41:49 -0700, "Bhargs" <bhargavi_ks2001@yahoo.com> wrote:
I am trying to change the background color of button and when abutton
is pushed I will let the user know that its pushed .
I am able to change the background color but it does show pushed when
button clicks.
Can anyone fix this?
Problem with my code is
void CFilterBtn::DrawItem(LPDRAWITEMSTRUCT
lpDrawItemStruct/*lpDrawItemStruct*/)
{
CBrush defBrush, selBrush;
COLORREF Orange = RGB(0, 10, 10);
selBrush.CreateSolidBrush(Orange);
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
if(lpDrawItemStruct->itemAction == ODA_SELECT)
uStyle |= DFCS_PUSHED;
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
FillRect(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
selBrush);
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC,
RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
::SetBkColor(lpDrawItemStruct->hDC, RGB(0,0,0));
}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm