• Advertisement
Sign in to follow this  

What is COLOR_WINDOW + 1

This topic is 3952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What does this do, and what does COLOR_WINDOW mean wndclass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 ) Thanks, Glen http://cavelectronics.tripod.com

Share this post


Link to post
Share on other sites
Advertisement
It is a member of the WNDCLASS or WNDCLASSEX structure that describes the background of a window using that window class. The COLOR_WINDOW + 1 bit of that line says that the background color of a window using that window class should be a system color; that gives the advantage that when a user changes the color scheme on his/her computer, the color you used will change as well if I'm not mistaken. Just experiment with the addition to COLOR_WINDOW and see what kind of colors you can get :P

Share this post


Link to post
Share on other sites
Quote:
Original post by Colin Jeanne
Quote:
Original post by rogierpennink
Just experiment with the addition to COLOR_WINDOW and see what kind of colors you can get :P

Instead of experimenting to see what works (and having no idea why or what implications your experiment has) you could check the documentation to see what colors exist.

That's undisputedly a good resource but I don't see what's wrong with a little experimenting either. In a worst-case scenario your program will crash and you will have to investigate what went wrong. Such investigation often leads to understanding and valuable experience. If nobody experimented and 'checked the documentation' only, we probably wouldn't even be able to discuss this on computers over the internet...

Share this post


Link to post
Share on other sites
Quote:
Original post by boolean010
What does this do, and what does COLOR_WINDOW mean

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


That's either a really fucked up API or very bad coding style ;-)

Check the docs as Colin says, and if there aren't any constants to use, better create some. COLOR_WINDOW + 1 is nonsense.

Share this post


Link to post
Share on other sites
Quote:
Original post by rogierpennink
In a worst-case scenario your program will crash and you will have to investigate what went wrong.

No, that's the best-case scenario. If you've done much software debugging, you'll know what I mean.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by rogierpennink
In a worst-case scenario your program will crash and you will have to investigate what went wrong.

No, that's the best-case scenario. If you've done much software debugging, you'll know what I mean.

I won't challenge you on that, undoubtedly you have done more software debugging than I have. On the other hand, this is a thread by someone who is apparently learning the win32 API. I may be wrong again, but when I was learning such basics (yes, I experimented with the values for hbrBackground as well) nothing unrepairable would happen. I'm fairly confident that a little experimenting on the OP's part won't hurt too much and it might as well be a learning experience. "I had better not do this again or my program won't work!" - sort of experiences...

Share this post


Link to post
Share on other sites
Quote:
Original post by Ahnfelt
Quote:
Original post by boolean010
What does this do, and what does COLOR_WINDOW mean

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


That's either a really fucked up API or very bad coding style ;-)

both.
Quote:

Check the docs as Colin says, and if there aren't any constants to use, better create some. COLOR_WINDOW + 1 is nonsense.

it is nonsense. looking here you'll see that COLOR_WINDOW + 1 would be COLOR_WINDOWFRAME.

edit: apparently the code is right and only the api is nonsense. evil steve explained below.

[Edited by - gumpy macdrunken on March 25, 2007 10:55:19 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by rogierpennink
when I was learning such basics (yes, I experimented with the values for hbrBackground as well) nothing unrepairable would happen.

It's not a matter of unrepairable things happening. That's pretty rare. What is much more common is something working perfectly on your computer. You leave it in, and forget about it. Three months later, your customers using the Japanese-language version of Windows XP start reporting crashes that you can't reproduce. At this point you don't even remember doing the COLOR_WINDOW thing, or that you did it without fully understanding it, or where it ranks among the dozens of other things that you did without fully understanding them. That's the worst case scenario.

There's nothing wrong with experimenting, no. But between reading documentation and experimenting (assuming good software development practices on the part of the API authors) experimentation will steer you wrong much more.

Share this post


Link to post
Share on other sites
Quote:
Original post by boolean010
What does this do, and what does COLOR_WINDOW mean

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


Hi,

MSDN says: (http://msdn2.microsoft.com/en-us/library/ms633576.aspx)

hbrBackground

Handle to the class background brush. This member can be a handle to the physical brush to be used for painting the background, or it can be a color value. A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). If a color value is given, you must convert it to one of the following HBRUSH types:

..., COLOR_WINDOW, ...


That means:

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

sets the hbrBackground to COLOR_WINDOW.

Share this post


Link to post
Share on other sites
The reason you have to add 1 to the value is that COLOR_SCROLLBAR has the value of 0, which would be the same as passing NULL for the value if you tried to use it. The +1 causes the colours to be offset so that COLOR_SCROLLBAR can be used without Windows thinking that you're passing it a NULL brush (Which means that you don't want Windows to paint your background for you).

Share this post


Link to post
Share on other sites
As rogierpennink pointed out those constants indicate that a system color should be used. My guess is that COLOR_WINDOW serves as an index into a table of brushes and that in the early days of Windows someone coded that table incorrectly and that "+1" was needed as a workaround. It's been maintained that way ever since for reasons of backwards compatibility.

// edit - Evil Steve explained it.

Share this post


Link to post
Share on other sites
Quote:
Original post by rogierpennink
That's undisputedly a good resource but I don't see what's wrong with a little experimenting either. In a worst-case scenario your program will crash and you will have to investigate what went wrong. Such investigation often leads to understanding and valuable experience. If nobody experimented and 'checked the documentation' only, we probably wouldn't even be able to discuss this on computers over the internet...

I should have explained a bit more of why I said that. The reason I say that it is a bad idea to experiment with an API and use something that might be undocumented is because since what you're doing is undocumented there is no contractual obligation to the API to maintain that behavior. What you're doing might work perfectly fine on your computer today. It might work just the same on my computer tomorrow. Then again, it might not work at all on my computer tomorrow, or yours for that matter. You really have no idea.

Relying on undocumented behavior creates a liability for you in the future.

I agree that experimentation is good, but not when experimenting with input to somebody else's API. They're making the assumption that you are doing what they have told you to do and you are breaking that assumption.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement