Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the system colours for 1 VB6 app only. 2

Status
Not open for further replies.

KirkJewell

Programmer
Oct 6, 2000
54
GB
I want to change the default system colours for one of my applications. However I don't want to change any of the button images (i.e. add a new 'skin' using something like WindowsBlinds or ActiveSkin).

I'm trying to ensure a unique colour scheme that works regardless of the user system setting. Can it be done?
(I'm just going to use shades of yellow or blue (instead of the windows default grey (that's gray to u yanks out there).

I have found that if you set the background or foreground colours to anything but the windows system a clash with some user somewheres settings is certain.

Regards
Kirk.
 
Yes you can. You need to use API calls and a Hook. Basically you save the original color scheme, set the new one and then restore the original if the program loses focus
, minimized, or closed.


This code goes in the form load module:



'Store handle to this form's window.
gHW = Me.hwnd
'Call procedure to begin capturing messages for this window.
Hook
'Call procedure to save the original color settings.
SaveOriginalColors
'Call procedure to set the new colors as the ones in use.
SetNewColors


'Following code goes in a "Bas" module

Declare Function SetSysColors Lib "user32" (ByVal nChanges As Long, lpSysColor As Long, lpColorValues As Long) As Long

Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Public Sub RestoreColors()
SetSysColors 21, IndexArray(0), OriginalColors(0)
End Sub

Public Sub SetNewColors()
SetSysColors 21, IndexArray(0), NewColors(0)
End Sub

Public Sub SaveOriginalColors()
Dim i As Long
'Retrieve all current color settings.
'Set new to original
'(makes it easier to change just a couple of colors)
'Init index array
For i = 0 To 20
IndexArray(i) = i
OriginalColors(i) = GetSysColor(i)
NewColors(i) = OriginalColors(i)
Next i
End Sub


Public Sub Hook()
'Establish a hook to capture messages to this window.
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub Unhook()
Dim temp As Long

'Reset the message handler for this window.
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Check for the ActivateApp message.
If uMsg = WM_ACTIVATEAPP Then
'Check to see if Activating the application.
If wParam <> 0 Then
'Check to see if application is minimized.
If frmMain.WindowState <> vbMinimized Then
'Use custom colors.
SetNewColors
End If
Else
'Application is DeActivating so restore normal colors.
RestoreColors
End If
End If

' 'Pass message on to the original window message handler.
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, _
' lParam)
End Function
 
Thanks for the advice and the code - I shall test it first thing.

 
Everythings seems OK except 3 things:-

1, I declared the handle var as a variant (i use option explicit)

2, I changed the windowproc = callwindowproc to windproc = windproc (It looks like a type I often make myself, I removed the 'call').

3, 'SetWindowLong' doesn't exist as a function. Is there another library I should be linking too?

Regards
Kirk.

 
Kirk -

Handles should be declared as Long, not variant. And, here is the declare for the SetWindowLong Win32 api call (from the API Text Viewer)

[tt]Public Declare Function SetWindowLong Lib &quot;user32&quot; Alias &quot;SetWindowLongA&quot; (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long[/tt]

Hope this helps.
Chip H.
 
Thanks. I managed to suss the SetWindowLong myself (I learnt about the API View at the same time).

Unfortunately my girl says its time I 'stopped playing ' on my computers (sheesh!) and paid her some attention.

I shall make the changes to the handle declaration and check my SetWindowLong declaration with yours. I will also set up some values in my NewColors (the code works without complaints but doesn't actually change any colours yet.

Thanks Again.
Kirk.
 
Aha! It works (Thankyou). However it changes the color of everything (in the test I did, I demonic shades of red) including the windows desktop (I guess thats what I asked for).

Whilst it is not what I'm looking for - its a very interesting effect and I have learnt more about the API and API viewer.

Is there a way to change the system colours for just the application and not the rest of windows (someone suggested using the palette method but that seems to be more relevant to 256 colour based displays).

Regards
Kirk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top