Hello...
I have created a BS_OWNERDRAW button and handled the WM_DRAWITEM message exactly as Charles Petzold indicated in his book. It workds exactly as I want, but when I click it, the background becomes white instead of the background color.
When I release it is ok. If I click it, drag away from it and release, it stays white until I click it again or I click another control.
I set the background for the text to transparent to avoid seeing a gray background above the white, but it is definitely not what I want...
I'm not sure what I did wrong...
If anyone could help me, I would really be grateful...
This is my button creation code:
Code:
//Create the rollout button
itsRolloutButtonHandle = ::CreateWindow("button", itsCaption.c_str(), (WS_CHILD | WS_VISIBLE | BS_OWNERDRAW), 0, 0, 128, 18, itsHandle, (HMENU)IDC_ROLLOUT_BUTTON, itsHInstance, NULL);
if (itsRolloutButtonHandle == 0 || !IsWindow(itsRolloutButtonHandle))
{
return 0;
}
//Change the cursor
if (::SetClassLong(itsRolloutButtonHandle, GCL_HCURSOR, (LONG)LoadCursor(0, IDC_HAND)) == 0)
{
return 0;
}
This is the WM_DRAWITEM from the window proc
(the code isn't optimal, I'm just trying to make it work first)
Code:
case WM_DRAWITEM:
{
//The draw item struct
LPDRAWITEMSTRUCT currDrawItemStructPtr = (LPDRAWITEMSTRUCT) currLParam;
//Create new color, pen, brushes and fonts
COLORREF currTextColor = ::GetSysColor(COLOR_BTNTEXT);
COLORREF currBackgroundColor = ::GetSysColor(COLOR_MENU);
HPEN currPen = ::CreatePen(PS_SOLID, 1, currTextColor);
HBRUSH currBrush = ::CreateSolidBrush(currBackgroundColor);
HFONT currFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
HFONT currExpandFont = (HFONT)::GetStockObject(SYSTEM_FONT);
//Store the former rect color, pen, brushes and fonts and select the new ones
RECT formerRect = currDrawItemStructPtr->rcItem;
COLORREF formerTextColor = ::SetTextColor(currDrawItemStructPtr->hDC, currTextColor);
::SetBkMode(currDrawItemStructPtr->hDC, TRANSPARENT);
HPEN formerPen = (HPEN)::SelectObject(currDrawItemStructPtr->hDC, currPen);
HFONT formerFont = SelectFont(currDrawItemStructPtr->hDC, currFont);
//Fill the rect
::FillRect(currDrawItemStructPtr->hDC, &currDrawItemStructPtr->rcItem, currBrush);
//Frame the rect
::Rectangle(currDrawItemStructPtr->hDC, currDrawItemStructPtr->rcItem.left, currDrawItemStructPtr->rcItem.top, currDrawItemStructPtr->rcItem.right, currDrawItemStructPtr->rcItem.bottom);
//Draw the caption
currDrawItemStructPtr->rcItem.left += (constRolloutButtonCaptionPadding + 2);
currDrawItemStructPtr->rcItem.right -= constRolloutButtonCaptionPadding;
currDrawItemStructPtr->rcItem.top += 1;
currDrawItemStructPtr->rcItem.bottom += 1;
std::string currCaption;
char currBuffer[512];
if (::GetDlgItemText(currHandle, IDC_ROLLOUT_BUTTON, currBuffer, 512) != 0)
{
currCaption = std::string(currBuffer);
}
else
{
currCaption = "";
}
::DrawText(currDrawItemStructPtr->hDC, currCaption.c_str(), -1, &currDrawItemStructPtr->rcItem, (DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_END_ELLIPSIS | DT_INTERNAL));
//Draw the expand sign
currDrawItemStructPtr->rcItem.left -= (constRolloutButtonCaptionPadding - 1);
currDrawItemStructPtr->rcItem.right = currDrawItemStructPtr->rcItem.left + constRolloutButtonExpandSignWidth;
currDrawItemStructPtr->rcItem.top -= 1;
currDrawItemStructPtr->rcItem.bottom -= 1;
std::string currExpandSign = "-";
if (currWinRolloutPtr->getRolledUpFlag() == true) { currExpandSign = "+"; }
SelectFont(currDrawItemStructPtr->hDC, currExpandFont);
::DrawText(currDrawItemStructPtr->hDC, currExpandSign.c_str(), -1, &currDrawItemStructPtr->rcItem, (DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_INTERNAL));
//Restore the rect
currDrawItemStructPtr->rcItem = formerRect;
//Select back the former items
::SetTextColor(currDrawItemStructPtr->hDC, formerTextColor);
::SetBkMode(currDrawItemStructPtr->hDC, OPAQUE);
::SelectObject(currDrawItemStructPtr->hDC, formerPen);
SelectFont(currDrawItemStructPtr->hDC, formerFont);
//Delete the brush and pen
::DeleteObject(currPen);
::DeleteObject(currBrush);
return TRUE;
}