Re: Ownerdraw button's Drawitem not being called when button is ca



this is my code but its in MFC. But you can easily change it to what u see fit.

#include "stdafx.h"
#include "Kuryn.h"
#include "MyButton.h"

IMPLEMENT_DYNAMIC(CMyButton, CButton)

CMyButton::CMyButton()
: m_bOverControl(FALSE),
m_nTimerID(1)
{
}

CMyButton::~CMyButton()
{
}


BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
END_MESSAGE_MAP()

void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
if(!m_bOverControl)
{
TRACE0("Entering control\n");

m_bOverControl = TRUE;
Invalidate();

SetTimer(m_nTimerID, 100, NULL);
}
CButton::OnMouseMove(nFlags, point);
}

void CMyButton::OnTimer(UINT_PTR nIDEvent)
{
CPoint p(GetMessagePos());
ScreenToClient(&p);

CRect rect;
GetClientRect(rect);

if(!rect.PtInRect(p))
{
TRACE0("Leaving control\n");

m_bOverControl = FALSE;
KillTimer(m_nTimerID);

Invalidate();
}
CButton::OnTimer(nIDEvent);
}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT nState = lpDrawItemStruct->itemState;

CString strText;
GetWindowText(strText);

if(nState & ODS_SELECTED)
pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
else
pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH);

rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE),
GetSystemMetrics(SM_CYEDGE)));

if(m_bOverControl)
pDC->FillSolidRect(rect, RGB(0, 128, 255));

if(!strText.IsEmpty())
{
CSize extent = pDC->GetTextExtent(strText);
CPoint pt(rect.CenterPoint().x - extent.cx / 2,
rect.CenterPoint().y - extent.cy / 2);

if(nState & ODS_SELECTED)
pt.Offset(1, 1);

int nMode = pDC->SetBkMode(TRANSPARENT);

if(nState & ODS_DISABLED)
pDC->DrawState(pt, extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC->TextOut(pt.x, pt.y, strText);

pDC->SetBkMode(nMode);
}
}

void CMyButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}

"dave" wrote:

BOOL m_bIsPressed;

BOOL m_bIsFocused;

"RAB" <rabmissouri@xxxxxxxxx> wrote in message
news:646f62a2-2433-4aed-ae52-7f23897d64b0@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 16, 3:07 am, "dave" <dave@msnnews> wrote:
Hi, I have an ownerdraw button in my dialog, just the one. I have the
following code to draw the button. I expected the MFC DrawItem method
for the button to be called when the button is clicked or pressed
continuously.

I noticed that if i use the m_bIsFocused flag then when the button is
clicked
it always remains focussed. If i only use the m_bIsPressed then my button
doesn't
always turn blue when pressed. How do i need to handle this properly?

Thanks

void CColorbutton::DrawItem(LPDRAWITEMSTRUCT lpDrawItem)
{
CRect rect;
CDC *pDC;
pDC = CDC::FromHandle(lpDrawItem->hDC);
rect = lpDrawItem->rcItem;

m_bIsPressed = (lpDrawItem->itemState & ODS_SELECTED);
m_bIsFocused = (lpDrawItem->itemState & ODS_FOCUS);

pDC->SetBkMode(TRANSPARENT);

CBrush greybr(GREY);
CBrush bluebr(BLUE);

if ( m_bIsPressed )
{
pDC->FillRect(&rect, &bluebr);
}
else
{
pDC->FillRect(&rect, &greybr);
}



}- Hide quoted text -

- Show quoted text -



What object or variable does 'm_bIsPressed' and 'm_bIsFocused'
represent in your program?

RABMissouri2007






.


Loading