My BS_OWNERDRAW button draw as white when clicked...

This is a discussion on My BS_OWNERDRAW button draw as white when clicked... within the Windows Programming forums, part of the Platform Specific Boards category; Hello... I have created a BS_OWNERDRAW button and handled the WM_DRAWITEM message exactly as Charles Petzold indicated in his book. ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    My BS_OWNERDRAW button draw as white when clicked...

    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;
                }

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    Nevermind... Quite a stupid error, I forgot to set a NULL_BRUSH for the Rectangle around the button...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  3. exit while loop when button clicked
    By shav in forum Windows Programming
    Replies: 18
    Last Post: 02-02-2005, 04:46 AM
  4. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM
  5. Owner draw button with bitmap
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 11-30-2003, 02:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21