/*
ColorControl.cpp
coded by: Hound
Example showing how to color listboxes, edits, radio/check boxes, and statics
there is also a combobox in this example, they are a combinaton of
an edit and a listbox window, hence the name combo..
NOTE: WM_CTLCOLORBTN does not work for pushbuttons
only for radio/checkbox style buttons, to color a button you have
to use an owner drawn button and handle ALL of the painting
*/
#include "ColorControls.h"
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
static COLORREF customcolors[16];
HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
static LOGBRUSH logbrush;
switch (iMsg)
{
case WM_CREATE:
//here is our combobox we do not need the HMENU parameter in this example
//since im not checking for any notifications here
combo = CreateWindow("combobox",NULL,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
15,15,130,200,hWnd,(HMENU)NULL,hInst,NULL);
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");
//here is our edit control
edit = CreateWindow("edit","edit control",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
15,40,130,25,hWnd,(HMENU)NULL,hInst,NULL);
//here is the listbox
list = CreateWindow("listbox","",WS_CHILD|WS_VISIBLE|LBS_STANDARD,15,70,100,150,
hWnd,(HMENU)NULL,hInst,NULL);
SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");
//here is the button
button = CreateWindow("button","Checkbox",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,15,230,100,20,hWnd,
(HMENU)NULL,hInst,NULL);
//static:
stat = CreateWindow("static","Hi, i'm a static\nColor me!",WS_CHILD|WS_VISIBLE|SS_CENTER,
150,80,150,50,hWnd,(HMENU)NULL,hInst,NULL);
//here is the buttons to change to colors
changetxtcolor = CreateWindow("button","Change Txt Color",WS_VISIBLE|WS_CHILD,
150,15,150,25,hWnd,(HMENU)CHANGETEXT,hInst,NULL);
changebkcolor = CreateWindow("button","Change BK Color",WS_VISIBLE|WS_CHILD,
150,45,150,25,hWnd,(HMENU)CHANGEBKG,hInst,NULL);
//we need to set up the brush
logbrush.lbHatch = 0;
logbrush.lbStyle = BS_SOLID;
logbrush.lbColor = RGB(255,255,255); //initially set it to black
hbrush = CreateBrushIndirect(&logbrush);
//and finally we set up the choosecolor struct for that common dialog
SetupChooseColor(&choosecolor,customcolors,hWnd,currentcolor);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
InvalidateRect(hWnd,NULL,true);
EndPaint(hWnd,&ps);
break;
//since all of the controls will have the same color attributes i can
//switch them all at once like so, use seperate ones if you need but return
//the brush for each of them
case WM_CTLCOLORSTATIC:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORBTN:
SetTextColor((HDC)wParam,textcolor);
SetBkMode((HDC)wParam,OPAQUE); //*look on the bottom for more info
SetBkColor((HDC)wParam,backcolor);
logbrush.lbColor = backcolor;
//now because ive changed the logbrush have to update the brush
//if your keeping your control at a constant color just set the text and
//return the brush, no need for this in that case
hbrush = CreateBrushIndirect(&logbrush);
return (long)hbrush;
break;
case WM_CLOSE:
DeleteObject(hbrush);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CHANGETEXT:
if(ChooseColor(&choosecolor))
{
currentcolor = choosecolor.rgbResult;//set currentcolor cause
//we need that variable to have value for the RGB_INIT
textcolor = currentcolor;
SendMessage(hWnd,WM_PAINT,0,0);
}
break;
case CHANGEBKG:
if(ChooseColor(&choosecolor))
{
currentcolor = choosecolor.rgbResult;//set currentcolor cause
//we need that variable to have value for the RGB_INIT
backcolor = currentcolor;
SendMessage(hWnd,WM_PAINT,0,0);
}
break;
}
}
return DefWindowProc(hWnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
const char *ClassName = "PCPP_ColorControls";
WNDCLASSEX wclass; //here we set up a variable for our window class
MSG msg; //this will hold the window messages later on
//Next we set up the properties of the window class
wclass.cbSize = sizeof(wclass); //the size
wclass.style = CS_HREDRAW | CS_VREDRAW; //style
wclass.lpfnWndProc = WndProc; //this tells windows the function to send messages to
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hInstance = hInstance; //the instance of your program
wclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); //which icon to use
wclass.hCursor = LoadCursor(NULL,IDC_ARROW); //the cursor to use
wclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //background color
wclass.lpszMenuName = NULL; //set the menu
wclass.lpszClassName = ClassName; //set classname
wclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); //set small icon
RegisterClassEx(&wclass); //this registers your window with windows
main = CreateWindow(ClassName, "Neato colors :-)",
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 200, 200, 320, 300,NULL, NULL, hInstance, NULL);
while(GetMessage(&msg,NULL,0,0)) //getmessage loops until a message is on the queue
{ //then it returns focus,peekmessage does the same except returns focus no matter what
TranslateMessage(&msg); //translate the message into its char equivelent
DispatchMessage(&msg); //dispatch to the window
}
return msg.wParam;
}
/*
*the reason i made the back of the text OPAQUE and set it to the same color as the
back of the control is only because the results are much better. the color you set to
the logbrush is the controls color, the color you set when you use SetBkColor is not,
that is the color of the text's background color. you CAN set the bacground mode to TRANSPARENT
and not worry about it but you might get wierd results at times like in the combo box.
*/