Click to See Complete Forum and Search --> : Setting a button's color WITHOUT MFC (yes I've searched!)
m0zzie
April 24th, 2003, 03:38 AM
I've asked around so many places (IRC, other people, other forums, etc) on how I can change the color of a button WITHOUT using MFC, except they can't seem to grasp that "WITHOUT using MFC" part and keep reffering me to pages such as http://www.codeguru.com/buttonctrl/ClrButton.html and http://www.codeguru.com/buttonctrl/color_button.shtml
I've also searched ALOT on google on how to do this, and come up with nothing.
I know I can create my own Bitmap which acts like a button, but I refuse to do that, because it adds size to my program, and I KNOW that it's possible to do it another way.
This is how I create my button:
hButton= CreateWindow ("BUTTON", "Exit",
WS_CHILD | WS_VISIBLE,
131, 44, 41, 16, hwnd, NULL,
g_hInstance, NULL);
Any help on this is much appreciated!
rxbagain
April 24th, 2003, 04:18 AM
There is no easy way of setting the color of button (eg. using SetBkColor).
If you want to change the color of a button, you hanve to make it BS_OWNERDRAW, and the in your DialogProc, handle the WM_DRAWITEM message to draw the button image by yourself.
rxbagain
April 24th, 2003, 04:22 AM
Here's a sample from MSDN
BOOL CALLBACK OwnDrawProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdcMem;
LPDRAWITEMSTRUCT lpdis;
switch (message) {
case WM_INITDIALOG:
// hinst, hbm1 and hbm2 are defined globally.
hbm1 = LoadBitmap((HANDLE) hinst, "OwnBit1");
hbm2 = LoadBitmap((HANDLE) hinst, "OwnBit2");
return TRUE;
case WM_DRAWITEM:
lpdis = (LPDRAWITEMSTRUCT) lParam;
hdcMem = CreateCompatibleDC(lpdis->hDC);
if (lpdis->itemState & ODS_SELECTED) // if selected
SelectObject(hdcMem, hbm2);
else
SelectObject(hdcMem, hbm1);
// Destination
StretchBlt(
lpdis->hDC, // destination DC
lpdis->rcItem.left, // x upper left
lpdis->rcItem.top, // y upper left
// The next two lines specify the width and height.
lpdis->rcItem.right - lpdis->rcItem.left,
lpdis->rcItem.bottom - lpdis->rcItem.top,
hdcMem, // source device context
0, 0, // x and y upper left
32, // source bitmap width
32, // source bitmap height
SRCCOPY); // raster operation
DeleteDC(hdcMem);
return TRUE;
case WM_COMMAND:
if (wParam == IDOK || wParam == IDCANCEL) {
EndDialog(hDlg, TRUE);
return TRUE;
}
if (HIWORD(wParam) == BN_CLICKED) {
switch (LOWORD(wParam)) {
case IDC_OWNERDRAW:
// application-defined processing
break;
}
}
break;
case WM_DESTROY:
DeleteObject(hbm1); // delete bitmaps
DeleteObject(hbm2);
break;
}
return FALSE;
}
Hope this will help you
Bengi
April 24th, 2003, 04:53 AM
u can play with:
The WM_CTLCOLORBTN message is sent to the parent window of a button when the button is about to be drawn. By responding to this message, the parent window can set a button's text and background colors.
WM_CTLCOLORBTN
hdcButton = (HDC) wParam; // handle of button display context
hwndButton = (HWND) lParam; // handle of button
mdmd
April 24th, 2003, 07:18 PM
Originally posted by Bengi
u can play with:
The WM_CTLCOLORBTN message is sent to the parent window of a button when the button is about to be drawn. By responding to this message, the parent window can set a button's text and background colors.
Not for push buttons. They are drawn with several different
brushes and CtlColor only returns one, it is ignored. To change
the color and text of a push button it must be owner drawn as
described above.
The OP can still look at the mfc samples that change the color
and easily convert to win32 api without mfc and use in the
function above.
owiley
November 4th, 2004, 06:55 AM
You Could Also Use Setsystemcolor() Function But That Will Change All The Buttons Colors
neo_the_1
November 4th, 2004, 09:53 AM
try SetClassLong with GCL_HBRBACKGROUND.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.