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