Prev: Set the audio progarmatically in Win 7
Next: the equivalents of some Win APIs and filter driver in Linux OS
From: Jimbo on 14 May 2010 05:26 Hello Can you help me figure out how to use WM_DRAWITEM to change a buttons colour This does nothing, when I though it would change the buttons colour: case UBP_CONTROLBKCOL: { HWND stBk = GetDlgItem( hwnd, LOWORD(wParam) ); HBRUSH newBrush = CreateSolidBrush( colour ); SetClassLongPtr( stBk, GCLP_ HBRBACKGROUND, (LONG_PTR)&newBrush ); } break; My Main program that compiles & works, except the button colour does not change? /* Application: */ #include <windows.h> #include <windowsx.h> #include <stdio.h> #include <cmath> #include <vector> #include <string> #include <cstdlib> using namespace std; #define UBP_CONTROLBKCOL 50001 static 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; }
From: Dee Earley on 14 May 2010 06:36 On 14/05/2010 10:26, Jimbo wrote: > Hello > > Can you help me figure out how to use WM_DRAWITEM to change a buttons > colour > > This does nothing, when I though it would change the buttons colour: > > case UBP_CONTROLBKCOL: > { > HWND stBk = GetDlgItem( hwnd, > LOWORD(wParam) ); > HBRUSH newBrush = CreateSolidBrush( colour ); > SetClassLongPtr( stBk, GCLP_ HBRBACKGROUND, > (LONG_PTR)&newBrush ); > > } > break; > > > My Main program that compiles& works, except the button colour does > not change? Is it a themed button (Xp, Vista, 7)? You can't change just the colour as that is drawn by the theme. You will need to completely draw it yourself if you want it to look different to everything else on the system. -- Dee Earley (dee.earley(a)icode.co.uk) i-Catcher Development Team iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.)
From: winapi on 14 May 2010 06:44
http://www.alsprogrammingresource.com/win32_tutorials.html Scroll down to. . . . . Color Buttons Example Program Source Code Listing |