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.
Printable View
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.
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
How would i do this?Quote:
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.
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:
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.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);
}
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.
wow, do i have to do that for every button? or just once?
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.