Dave Green Level: Professor

 Registered: 20-10-2005 Posts: 90
|
Getting System Colour Constants
Hi all
Just thought I'd share this with you as it may come in handy if you ever create images that sit on forms or controls which have the standard system colours as background (you know that beautiful grey !!!). 
With the normal-colour pallette all the colours are represented by a hex number (e.g. &H00C000D8&) . Note that there are FOUR groups of 32 bits, whereas RGB values only require 3. Don't panic the RGB is there, simply drop off the first 32 bits "00", giving C0, 00, D8. However VB (for some inexplicable reason) have arranged these so they read BGR instead of the industry standard RGB, so they need to be read backwards otherwise you'll end up with Blue instead of Green and vice versa. Important note here, because of this don't use the constants such as vbBlue in code that is destined for DHTML /IE applications as these use the standard RGB arrangement and will produce the wrong colour, even so you will have to reverse the VB arrangement.
Anyway that is the easy bit , it gets a bit more complex with system color constants (such as vbActiveBorder). If you look at the hex code for that it is &H8000000A&, which if you ignore the first 32 bits as above is not the grey you would expect, but virtual black (just a shade off). Looking at those first 32 bits we have Hex 80 (10000000 binary), instead of Hex 00 as in the colour pallette example above. This "80" is a flag in VB to tell it to go to the system to get the colour for this setting and for it not to use the remaining three 32bit groups as an RGB (sorry BGR) value. Of course this makes sense when you consider that users can set their own window colour preferences.
But what if for some reason you want to drop an image on a form and to have its background exactly matching the form colour (vbButtonFace : &H8000000A&). You can get the colour returned as a THREE 32bit BGR value by using the API as follows:
Declare in a module (not on a form) the following:
Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Create a text box on a form and in its click event:
Private Sub Text1_Click()
Text1.Text = Hex(GetSysColor(10))
End Sub
Where do I get the "10" from - look at the right hand 32bit group, for vbButtonFace its 0A, which is 10 decimal. This is an index which you pass to GetSysColor denoting what sytem colour you want to get.
The result as BGR is C8,D0,D4, you could now convert these to decimal and paint the background of your image to match the form/control. But remember you couldn't distribute it if you thought your end user(s) might change the window colour settings.
Phew! Sorry if I've told you how to suck eggs, but it might help someone out there.
Regards
Dave
[Edited by Dave Green on 20-10-2005 at 08:54 PM GMT]
____________________________
While Breath.Count>0
Live(gbRelax)
Wend
|