Thread: Confused with COLOR_ constants

  1. #1
    Registered User ZaidK's Avatar
    Join Date
    Nov 2012
    Location
    Mumbai
    Posts
    3

    Post Confused with COLOR_ constants

    Books(the Charles Petzold one) say that there is a need of adding +1 while using the System colours like..

    wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1)

    I don't understand the need of that + 1 ? Can anyone helped in that. I am new to VC++ Programs

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This is not a C++ question. It is a windows programming question.

    The short answer is "because Microsoft says so in their documentation for the WNDCLASSEX structure", as per this link.

    The more precise answer is that Microsoft developers messed up in defining a few things (something that is understandable with a large team developing a very large system or API) and this in one of them. HBRUSH is a pointer type and the first COLOR_ parameter defined has a value of zero. Adding the 1 is necessary to ensure all valid COLOR_xxx values correspond to a non-NULL HBRUSH.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User ZaidK's Avatar
    Join Date
    Nov 2012
    Location
    Mumbai
    Posts
    3
    Ok ! I got that HBRUSH is a pointer type but what about "Adding the 1 is necessary to ensure all valid COLOR_xxx values correspond to a non-NULL HBRUSH."? Is it in the MS documentation or just a logic!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is logic. Converting a value of zero to a pointer yields a null pointer - a pointer (or memory address) that is guaranteed not to point at a valid object. To dereference a pointer, it is necessary for it to be non-null. If the HBRUSH corresponding to a COLOR_xxx value is NULL, then that brush cannot be used.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by ZaidK View Post
    Books(the Charles Petzold one) say that there is a need of adding +1 while using the System colours like..

    wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1)

    I don't understand the need of that + 1 ? Can anyone helped in that. I am new to VC++ Programs
    The system colours vary depending on the current style / theme.

    These colours are stored in an array and the IDs (like COLOR_BTNFACE) are defined integer values.

    The first element of the array is 0 (in C / C++) but you need a way to tell the OS that you ment to get an element of the array, not 'no colour' (ie NULL).

    So the +1 is used to tell the difference between (HBRUSH)NULL and (HBRUSH)0 (which means get me the system colour from the array at index zero)


    http://social.msdn.microsoft.com/for...-7d78338c17d2/
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User ZaidK's Avatar
    Join Date
    Nov 2012
    Location
    Mumbai
    Posts
    3
    It is logic. Converting a value of zero to a pointer yields a null pointer - a pointer (or memory address) that is guaranteed not to point at a valid object. To dereference a pointer, it is necessary for it to be non-null. If the HBRUSH corresponding to a COLOR_xxx value is NULL, then that brush cannot be used.

    Ok ! u mean to say that the first value of COLOR_(which i think is COLOR_SCROLLBAR
    ) will get treated as a NULL pointer!Got it !Thnks @grumpy @novacain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constants
    By GokhanK in forum C Programming
    Replies: 9
    Last Post: 02-15-2011, 02:01 AM
  2. COnstants
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 09-01-2007, 04:59 AM
  3. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  4. XOR-on constants
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2002, 07:20 AM
  5. Constants
    By Marky_Mark in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2001, 06:39 PM

Tags for this Thread