   [Win32] Create' Paint style' Coloured Button
  HelloI am trying to create a coloured button like you see in the Paint program where you select a colour by clicking on it. So I have a button I have created & I am trying to change its background colour. If I left click the button, a colour dialog appears & I can select a new colour. When I have selected a new colour, the button background will be this colour.My Problems:- Compile error when I use GCLP_ HBRBACKGROUND inside the function SetClassLongPtr( stBk, GCLP_ HBRBACKGROUND, (LONG_PTR)&newBrush ); See WNDPROC for more information on compile error. Does my code below compile for you? If yes, what editor are you using, I am using Dev c++.- Is this how you change the background colour of a button, is there an easier better way? See my WNDPROC to see how I change the bk colour of a button./*  Application: */#include <windows.h>#include <windowsx.h>#include <stdio.h>#include <cmath>#include <vector>#include <string>#include <cstdlib>using namespace std;#define UBP_CONTROLBKCOL 50001static HINSTANCE gInstance;UINT controlMsgs[] = {};COLORREF colour = RGB(0,0,0);// Functions List //LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);bool openColourDialog(HWND hwnd, UINT id);LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){   switch(msg)    {   case WM_CREATE:   { // Create GUI HWND stBkColour   = CreateWindowEx(0,"Static","Background Colour",   WS_BORDER|WS_CHILD|WS_VISIBLE,   18,20,120,20,hwnd,NULL,gInstance,   NULL);    HWND bkColour= CreateWindowEx(0,"Button","",WS_BORDER|WS_CHILD|   WS_VISIBLE|BS_OWNERDRAW,150,15,50,    28,hwnd,(HMENU)UBP_CONTROLBKCOL,   gInstance,NULL);   }  break;   case WM_COMMAND:    {    // Open Colour dialog to change button colour   switch(LOWORD(wParam))    {    case UBP_CONTROLBKCOL:    {    openColourDialog(hwnd,LOWORD(wParam));    }    break;    default:    break; }}  break;   case WM_DRAWITEM:    {    // Draw button background colour   switch(LOWORD(wParam))    {    case UBP_CONTROLBKCOL:    {    HWND stBk = GetDlgItem( hwnd, LOWORD(wParam) );    HBRUSH newBrush = CreateSolidBrush( colour );    SetClassLongPtr( stBk, GCLP_ HBRBACKGROUND, (LONG_PTR)&newBrush );    // I get an ERROR in the above line    // Error = 'GCLP undeclared(first use of this function'    // So my program wont compile, is this the way I can change// the colour of a button?    }    break;    default:    break; }}   break;   case WM_CLOSE:  DestroyWindow(hwnd);   break;   case WM_DESTROY:  PostQuitMessage(0);   break;   default: return DefWindowProc(hwnd, msg, wParam, lParam);   break;    }}bool openColourDialog(HWND hwnd, UINT id){   // Post: Open colour dialog to allow user to pick a new colour for updateBox    //  control attribute CHOOSECOLOR cc;  // common dialog box structure    static COLORREF acrCustClr[16]; // array of custom colors    static DWORD rgbCurrent;   // initial color selection  // Initialize CHOOSECOLOR    ZeroMemory(&cc, sizeof(cc));   cc.lStructSize = sizeof(cc);   cc.hwndOwner = hwnd;   cc.lpCustColors = (LPDWORD) acrCustClr;   cc.rgbResult = rgbCurrent;   cc.Flags = CC_FULLOPEN | CC_RGBINIT;   if (ChooseColor(&cc)==TRUE)    {  rgbCurrent = cc.rgbResult;  colour = rgbCurrent;  SendMessage(hwnd,WM_DRAWITEM,(WPARAM)id,0); // Send message to change button colour  return true;    } return false;}int WINAPI WinMain(HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){    WNDCLASSEX wc;    HWND hwnd;    MSG Msg;    //Step 1: Registering the Window Class    wc.cbSize   = sizeof(WNDCLASSEX);    wc.style    = 0;    wc.lpfnWndProc   = WndProc;    wc.cbClsExtra    = 0;    wc.cbWndExtra    = 0;    wc.hInstance= gInstance;    wc.hIcon    = LoadIcon(NULL, IDI_APPLICATION);    wc.hCursor  = LoadCursor(NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH);    wc.lpszMenuName  = NULL;    wc.lpszClassName = "Custom Class";    wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION);    // if registration of main class fails    if(!RegisterClassEx(&wc))    {   MessageBox(NULL, "Window Registration Failed!", "Error!",  MB_ICONEXCLAMATION | MB_OK);   return 0;    }    // Step 2: Creating the Window    hwnd = CreateWindowEx(   WS_EX_CLIENTEDGE,   "Custom Class",   "App Name",   WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU,   CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,   NULL, NULL, gInstance, NULL);    if(hwnd == NULL)    {   MessageBox(NULL, "Window Creation Failed!", "Error!",  MB_ICONEXCLAMATION | MB_OK);   return 0;    }   ShowWindow(hwnd, nCmdShow);    UpdateWindow(hwnd);   // Step 3: The Message Loop    while(GetMessage(&Msg, NULL, 0, 0) > 0)    {   TranslateMessage(&Msg);   DispatchMessage(&Msg);    }    return Msg.wParam;}
  I copied and pasted your code exactly and tried to compile with Visual C++ 10, and this is the output:1>c:\users\mjp\desktop\wintest\main.cpp(18): error C2466: cannot allocate an array of constant size 01>c:\users\mjp\desktop\wintest\main.cpp(73): error C2065: 'GCLP_' : undeclared identifier1>c:\users\mjp\desktop\wintest\main.cpp(73): error C2146: syntax error : missing ')' before identifier 'HBRBACKGROUND'1>c:\users\mjp\desktop\wintest\main.cpp(73): error C2059: syntax error : ')'The first is self-explanatory. The second is because you have a space where there shouldn't be one in "GCLP_HBRBACKGROUND". The third and fourth are a result of the second error.You're also not returning a value for all of your cases in your WndProc. Return values have different meanings for different messages, you'll have to check the documentation for each if you want to return something special. Otherwise you should return the value from DefWindowProc.As for Dev-C++, if you're not already aware you should know that it's a project that was dead years ago and by now is pretty severely outdated. Plenty of more info on that here.   ;  Thanks for the reply, :P, I should really look at my code better, should have found that space lol.Well it compiles. Although the button colour is not changing. I think that my method is not the way you change a button colour.My method:- In WM_DRAWITEM: obtain hwnd to button- create new HBRUSH with new colour- Set button's new colour using SetClassLongPtr( stBk, GCLP_HBRBACKGROUND, (LONG_PTR)&newBrush );Is this the way I change the colour of a button in Win32?   ;  My compiler actually showed that line 73 SetClassLongPtr' undeclared (first use this function) on Dev C++ I would like to use Microsoft Visual C++ 2010, but I have a 8 year old IBM T30 with 40 hdd space.   ;  A little off topic, but in regards to the compilers, you can download MinGW (a newer version of the compiler than the one Dev-C++ uses) from http://sourceforge.net/projects/mingw/files/, and you can download Code::Blocks, which is a code editor (similar to Dev-C++) from http://www.codeblocks.org/downloads/5 that'll automatically detect MinGW for you. MinGW comes in at a whole 71MB when installed, and Code::Blocks is 29MB when installed, which is 100MB in total (which is nothing in comparison to 40GB).   