Thread: predefined button color

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    predefined button color

    I've been working through my petzold book, and I am a little mystified. I'm working on the button chapter as well as adding button to my project, and the example in the book, BUTTONLOOK, shows a picture of all the buttons with gray background. When I run the compiled program that came with my book, it is truly gray. However, when I compile petzold's source code, and run it, all the buttons except for PUSHBUTTON and DEFPUSHBUTTON are white background.

    I've done it with two different computers running two different versions of windows.

    I use watcom 11 C/C++ to compile with.

    Is it some sort of compiler switch I should be using?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    My bet is that it's the way in which one of the buttons are set, but I can't remember all the properties. I'll look it up - in the mean time - post the source code.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    Here's the source

    I hope this comes out okay. It's my first time to post code.


    Code:
    /*----------------------------------------
       BTNLOOK.C -- Button Look Program
                    (c) Charles Petzold, 1996
      ----------------------------------------*/
    
    #include <windows.h>
    
    struct
         {
         long style ;
         char *text ;
         }
         button[] =
         {
         BS_PUSHBUTTON,      "PUSHBUTTON",
         BS_DEFPUSHBUTTON,   "DEFPUSHBUTTON",
         BS_CHECKBOX,        "CHECKBOX", 
         BS_AUTOCHECKBOX,    "AUTOCHECKBOX",
         BS_RADIOBUTTON,     "RADIOBUTTON",
         BS_3STATE,          "3STATE",
         BS_AUTO3STATE,      "AUTO3STATE",
         BS_GROUPBOX,        "GROUPBOX",
         BS_AUTORADIOBUTTON, "AUTORADIO",
         BS_OWNERDRAW,       "OWNERDRAW"
         } ;
    
    #define NUM (sizeof button / sizeof button[0])
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
         {
         static char szAppName[] = "BtnLook" ;
         HWND        hwnd ;
         MSG         msg ;
         WNDCLASSEX  wndclass ;
    
    	 wndclass.cbSize        = sizeof (wndclass) ;
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
    	 wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
    
         RegisterClassEx (&wndclass) ;
    
         hwnd = CreateWindow (szAppName, "Button Look",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
         while (GetMessage (&msg, NULL, 0, 0))
              {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
              }
         return msg.wParam ;
         }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
         {
         static char  szTop[]    = "iMsg            wParam       lParam",
                      szUnd[]    = "____            ______       ______",
                      szFormat[] = "%-16s%04X-%04X    %04X-%04X",
                      szBuffer[50] ;
         static HWND  hwndButton[NUM] ;
         static RECT  rect ;
         static int   cxChar, cyChar ;
         HDC          hdc ;
         PAINTSTRUCT  ps ;
         int          i ;
         TEXTMETRIC   tm ;
    
         switch (iMsg)
              {
              case WM_CREATE :
                   hdc = GetDC (hwnd) ;
                   SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
                   GetTextMetrics (hdc, &tm) ;
                   cxChar = tm.tmAveCharWidth ;
                   cyChar = tm.tmHeight + tm.tmExternalLeading ;
                   ReleaseDC (hwnd, hdc) ;
    
                   for (i = 0 ; i < NUM ; i++)
                        hwndButton[i] = CreateWindow ("button", button[i].text,
                                  WS_CHILD | WS_VISIBLE | button[i].style,
                                  cxChar, cyChar * (1 + 2 * i),
                                  20 * cxChar, 7 * cyChar / 4,
                                  hwnd, (HMENU) i,
                                  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
                   return 0 ;
    
              case WM_SIZE :
                   rect.left   = 24 * cxChar ;
                   rect.top    =  2 * cyChar ;
                   rect.right  = LOWORD (lParam) ;
                   rect.bottom = HIWORD (lParam) ;
                   return 0 ;
    
              case WM_PAINT :
                   InvalidateRect (hwnd, &rect, TRUE) ;
    
                   hdc = BeginPaint (hwnd, &ps) ;
                   SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
                   SetBkMode (hdc, TRANSPARENT) ;
    
                   TextOut (hdc, 24 * cxChar, cyChar, szTop, sizeof (szTop) - 1) ;
                   TextOut (hdc, 24 * cxChar, cyChar, szUnd, sizeof (szUnd) - 1) ;
    
                   EndPaint (hwnd, &ps) ;
                   return 0 ;
    
              case WM_DRAWITEM :
              case WM_COMMAND :
                   ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
    
                   hdc = GetDC (hwnd) ;
                   SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
    
                   TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
                            szBuffer,
                        wsprintf (szBuffer, szFormat,
                            iMsg == WM_DRAWITEM ? "WM_DRAWITEM" : "WM_COMMAND",
                            HIWORD (wParam), LOWORD (wParam),
                            HIWORD (lParam), LOWORD (lParam))) ;
    
                   ReleaseDC (hwnd, hdc) ;
                   ValidateRect (hwnd, &rect) ;
    
                   break ;
    
              case WM_DESTROY :
                   PostQuitMessage (0) ;
                   return 0 ;
              }
         return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
         }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    Talking I figured it out.

    Thanks for the help.

    I went through all my compiler switches in the IDE and found it was compiling for NT instead of 95.

    This brings me to a question that is off on a tangent.

    Is there a way to have it use the white background when you want and then the gray background when you want from within the source code? I don't mean by compiling some for NT and compiling others for 95 then linking together, is there a correct way to toggle it from the source?

    Just wondering. From everything I've read, it seems you'd just need to create your own "button" class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. screen color detection and button pressing
    By cwn11 in forum C Programming
    Replies: 1
    Last Post: 11-23-2008, 10:39 PM
  2. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 06:23 PM
  3. VC++ setting text color of a button
    By 7stud in forum Windows Programming
    Replies: 5
    Last Post: 10-14-2004, 11:04 AM
  4. MFC: Change the color of static text with button press?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2004, 11:03 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM