OK, rather than search again for where I found that code, here's mine, minus
most of the code I put in myself, so what's left is hopefully pretty
default, except that my button class is derived from COddButton which
defines IsDefault(). So take what you want and remove what you don't (or
doesn't work)
void CMyButt::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); //Get device context object
CRect rt;
rt = lpDrawItemStruct->rcItem; //Get button rect
dc.FillSolidRect(rt, GetSysColor(COLOR_BTNFACE)); //Fill button with color
UINT state = lpDrawItemStruct->itemState; //Get state of the button
if (this->IsDefault())
{
CBrush brushBlack(RGB(0, 0, 0));
dc.FrameRect( &rt, &brushBlack );
int iChange = 1;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;
}
if ( state & ODS_SELECTED) // If it is pressed
{
dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT); // Draw a sunken face
}
else
{
dc.DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face
}
dc.SetTextColor(GetSysColor(COLOR_BTNTEXT));
// Set the color of the caption to be yellow
CString strTemp;
GetWindowText(strTemp);
// Get the caption which have been set
CRect rtText(rt);
if (state & ODS_SELECTED) // If it is pressed
{
rtText.top += 2;
rtText.left += 2;
rtText.right += 2;
rtText.bottom += 2;
}
dc.DrawText(strTemp,rtText,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
// Draw out the caption
if ( (state & ODS_FOCUS ) ) // If the button is focused
{
// Draw a focus rect which indicates the user
// that the button is focused
int iChange = 3;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;
dc.DrawFocusRect(rt);
}
dc.Detach();
}
"Lost Johnny" <barnesbyanothern...@hotmail.co.uk> wrote in message
news:45e6fe3e$0$22124$db0fefd9@news.zen.co.uk...
Whoops, sorry, my mistake - the really good implementation of DrawItem
isn't within COddButton - I got it from somewhere else. I'll reply to this
when I find where I got it from. I got confused because I found the code
for DrawItem at the same time as I found COddButton because I needed both.
Sorry.
Lost Johnny
"Lost Johnny" <barnesbyanothern...@hotmail.co.uk> wrote in message
news:45e6fb19$0$32013$fa0fcedb@news.zen.co.uk...
I suggest you take a look at COddButton on CodeProject.
http://www.codeproject.com/buttonctrl/oddbutton.asp
It does use DrawItem but it draws a button that looks very similar to how
buttons are by default. In fact, the only thing I can see that's
different is that the text isn't moved down a few pixels when the button
is pressed, but it's a very trivial edit to make that happen. So you
might use the code in COddButton as the starting point of your own
DrawItem and add to it.
By the way, the aim of COddButton is to put back some of the stuff you
(very annoyingly) lose when you use the BS_OWNERDRAW style, so you might
find you'll want this functionality as well.
Lost Johnny
"Franco" <cumin...@yahoo.com> wrote in message
news:1172761002.346299.258570@s48g2000cws.googlegroups.com...
Hello,
I'd like to have a class derived from CButton that has the same
functionality of a push button, except that I need to do some extra
drawing. I need the operative system to draw the button first, then I
do some of my own drawing on top. I have looked at codeguru.com, but
didn't find what I need. Typically, the custom CButtons from codeguru
are owner-draw and you override the method DrawItem. People either
draw a very basic button frame, or they call something like
DrawFrameControl, which draws a crude button. What I need is for the
OS to draw the button just like it would draw any regular push button.
How do I tell Windows: draw a push button? Any ideas?
Thanks. I guess Ajay answered my question.