Is there anyway to "translate" the system's colors into their RGB components? Or at least into the positive long number it is represented with?
Printable View
Is there anyway to "translate" the system's colors into their RGB components? Or at least into the positive long number it is represented with?
Never mind... I've solved it already.
In case anyone is interested: TranslateColor - Convert a VB color or system color constant
In case you are or someone else is interested:
VB Code:
'in a module Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
It returns a direct long color value. Usage:
VB Code:
BackColor = GetSysColor(0) ForeColor = GetSysColor(28) 'this is the last index that gives a result
:)
I already knew that function, but was of no use. The parameter it needs is a constant (which usually starts with "COLOR_". This constants are positive numbers... what if the long you have is -2147483646 ? The GetSysColor API returns always 0.
Quote:
Originally posted by Merri
VB Code:
BackColor = GetSysColor(0) ForeColor = GetSysColor(28) 'this is the last index that gives a result
:)
If anyone knows any other... please post it here.VB Code:
Const COLOR_SCROLLBAR = 0 'The Scrollbar colour Const COLOR_BACKGROUND = 1 'Colour of the background with no wallpaper Const COLOR_ACTIVECAPTION = 2 'Caption of Active Window Const COLOR_INACTIVECAPTION = 3 'Caption of Inactive window Const COLOR_MENU = 4 'Menu Const COLOR_WINDOW = 5 'Windows background Const COLOR_WINDOWFRAME = 6 'Window frame Const COLOR_MENUTEXT = 7 'Window Text Const COLOR_WINDOWTEXT = 8 '3D dark shadow (Win95) Const COLOR_CAPTIONTEXT = 9 'Text in window caption Const COLOR_ACTIVEBORDER = 10 'Border of active window Const COLOR_INACTIVEBORDER = 11 'Border of inactive window Const COLOR_APPWORKSPACE = 12 'Background of MDI desktop Const COLOR_HIGHLIGHT = 13 'Selected item background Const COLOR_HIGHLIGHTTEXT = 14 'Selected menu item Const COLOR_BTNFACE = 15 'Button Const COLOR_BTNSHADOW = 16 '3D shading of button Const COLOR_GRAYTEXT = 17 'Grey text, of zero if dithering is used. Const COLOR_BTNTEXT = 18 'Button text Const COLOR_INACTIVECAPTIONTEXT = 19 'Text of inactive window Const COLOR_BTNHIGHLIGHT = 20 '3D highlight of button Const COLOR_INFOBK = 24 'Tooltip Const COLOR_2NDACTIVECAPTION = 27 'Win98 only: 2nd active window color Const COLOR_2NDINACTIVECAPTION = 28 'Win98 only: 2nd inactive window color
You mean this?
VB Code:
Public Function ProperColor(ByVal Color As Long) As Long If Color And &H80000000 Then 'it is a system color ProperColor = GetSysColor(Color And &HFF) Else 'make sure it is a proper color ProperColor = Color And &HFFFFFF End If End Sub
Didn't test but should work.
Also, you might find this document interesting: http://www.tomorrowssolutionsllc.com...4everyone.html
This code seems to work:Don't know if it works "always".VB Code:
Option Explicit Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" (ByVal OLE_COLOR _ As Long, ByVal HPALETTE As Long, pccolorref As Long) As Long Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long ' convert a VB color constant to a COLORREF ' accepts both RGB() values and system color constants Function TranslateColor(ByVal clr As Long) As Long If OleTranslateColor(clr, 0, TranslateColor) Then TranslateColor = -1 End If End Function Private Sub Form_Load() Debug.Print TranslateColor(-2147483646) Debug.Print ProperColor(-2147483646) End Sub Public Function ProperColor(ByVal Color As Long) As Long If Color And &H80000000 Then 'it is a system color ProperColor = GetSysColor(Color And &HFF) Else 'make sure it is a proper color ProperColor = Color And &HFFFFFF End If End Function
Well, my code works in each case, I just tested it. The code you posted gives an error when a given input is invalid, because a color can't be &HFFFFFFFF (= -1).
Anyways, I made a speed test as well, the speed of both ways is rather equal.
Do you mean the line TranslateColor = -1. Because that would be an error condition. You call the function and if you get -1... the input was invalid => stop processing it.Quote:
Originally posted by Merri
The code you posted gives an error when a given input is invalid, because a color can't be &HFFFFFFFF (= -1).
Well, I thought the whole point of the function would be to get a proper color, ie. the usage would be:
VB Code:
BackColor = ProperColor(AnyColorThatGoesIn)
This would save time checking for a color. If you wanted to check beforehand if you can use the color, a better function for that would be:
VB Code:
Public Function IsProperColor(ByVal Color As Long) As Boolean If Color And &H80000000 Then If (Color And &HFF) < 31 Then IsProperColor = True Else If Color < &H1000000 Then IsProperColor = True End If End Function
Just because it would be much faster than calling an API :)
I'll bear it in mind. Thanks!!