Thread: Coloring Buttons

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Arrow Coloring Buttons

    Does anyone know how to color buttons?
    I know that you have to go:

    case WM_CTLCOLORBTN:

    but what do i put under that?
    I searched this site and found nothing

    If you know how to color thing plz tell me.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Unfortunately that message doesn't work as described, windows just goes ahead and sets the colour to its default value; I think this is described in Petzold's book.

    If you want to colour buttons you could do one of three things:

    1. Make the button a bitmap button ie give it the BS_BITMAP style, create a bitmap that fits the button (including any text you need) and attach the bitmap to the button by sending it a BM_SETIMAGE message.

    2. Make the button owner-drawn ie give it the BS_OWNERDRAW style and handle the WM_MEASUREITEM and WM_DRAWITEM messages of the parent window to effect drawing/painting.

    3. Subclass the button control ie provide your own window procedure for the button - you would have to handle the appearance of the button in all its states, sending unhandled messages to the default wndproc for the BUTTON class by using CallWindowProc - also use this if you want some default handling anyway. If all you want to do is alter the look of the button then 2. above is probably simpler to implement.

    In general, for any of the WM_CTL**** type messages, you must return an HBRUSH which windows uses to paint the control. You are responsible for freeing resources used by this brush (DeleteObject).

    edit: typos
    Last edited by Ken Fitlike; 03-17-2002 at 05:46 AM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    2. Make the button owner-drawn ie give it the BS_OWNERDRAW style and handle the WM_MEASUREITEM and WM_DRAWITEM messages of the parent window to effect drawing/painting.
    How would i do this?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Here's a win32 API approach, mfc will be similar but obviously involve mfc class objects:

    1. Create a couple of coloured rectangle bitmaps (same size) to act as 'button up' and 'button down'. Make sure you shade them to look right. Include these bitmaps as resources in your project; give them id's like eg IDB_BTNUP and IDB_BTNDOWN

    2.When you create the button control with CreateWindow/CreateWindowEx give it the BS_OWNERDRAW style.

    3. In the wndproc for your main window, handle the WM_DRAWITEM. The lParam of that message is a pointer to a DRAWITEMSTRUCT. So do something like:
    Code:
    case WM_DRAWITEM:
    {
    DRAWITEMSTRUCT* lpDrawItem=(DRAWITEMSTRUCT*)lParam;
    HBITMAP hBmp;
    if (lpDrawItem->itemState&ODS_SELECTED)
      {
      hBmp=LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_BTNDOWN));
      }
    else
      {
      hBmp=LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_BTNUP));
      }
    BITMAP bm;
    GetObject(hBmp,sizeof(bm),&bm);
    HDC hMemDC=CreateCompatibleDC(NULL);
    HBITMAP hOldBmp=(HBITMAP)SelectObject(hMemDC,hBmp);
    StretchBlt( lpDrawItem->hDC,
                      lpDrawItem->rcItem.left, // x upper left 
                      lpDrawItem->rcItem.top,
                      lpDrawItem->rcItem.right-lpDrawItem->rcItem.left, 
                      lpDrawItem->rcItem.bottom-lpDrawItem->rcItem.top,
                      hMemDC,
                      0,0,
                      bm.bmWidth,
                      bm.bmHeight,
                      SRCCOPY);
    SelectObject(hMemDC,hOldBmp);
    DeleteDC(hMemDC);
    DeleteObject(hBmp);
    }
    You can set your button dimensions to any size and the fn will expand/shrink the bitmaps to fit (might not always look too good, though). Ideally, you should use LoadImage and not LoadBitmap.
    If you need to add text you can either add that when you create the bitmap or you can do it dynamically within the WM_DRAWITEM handler by using any of the text writing API fns like TextOut, ExtTextOut, DrawText etc. The dimensions of the button and of the bitmap are available within the fn body so working out how to position the text shouldn't be too difficult.

    Hope that helps.
    Last edited by Ken Fitlike; 03-17-2002 at 06:57 AM.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    wow, do i have to do that for every button? or just once?

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If you had different buttons you would create up/down bitmap pairs for each type you want. The WM_DRAWITEM msg gets the control handle so you would decide within the body of that handler which bitmap pair needed to be 'attached' to which control. How you implement the specific details is entirely up to you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2009, 02:31 AM
  2. Ownerdraw buttons (2)
    By maes in forum Windows Programming
    Replies: 7
    Last Post: 09-11-2003, 05:50 AM
  3. Radio Buttons in Visual C++ 6
    By Ripper1 in forum Windows Programming
    Replies: 22
    Last Post: 05-16-2003, 07:54 AM
  4. (Ken Fitlike) buttons
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2002, 01:21 PM
  5. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM