Detecting Windows Settings Changes

This tip demonstrates how to detect windows settings changes in your application. You will need to have installed and registered the Subclassing and Timer Assistant (SSubTmr.DLL for VB5 and SSubTmr6.DLL for VB6), available from this site at Subclassing without the crashes to run this sample.

Changes which can be detected using this code include system colour changes, non-client metric sizes and a whole host of other changes (as you will see from the enum of possible changes in the source code below!). The code is invaluable if you are creating your own graphics or controls and want to ensure they display correctly through system settings changes (which is part of the Windows logo requirements.)

Start a new project and choose Project->References. Look for "Subclassing and Timer Assistant (with multiple control support and timer bug fix)" if you're using VB5 or "VB6 Subclassing and Timer Assistant" if you're using VB6 in the references list. If it is there, select it and click ok. If it isn't, choose Browse, locate SSubTmr.DLL for VB5 or SSubTmr6.DLL for VB6 on your disk, then select that.

Once that is done, add a Class module. Rename the Class module to cSettingsChange and then add the following code:

Private Const WM_WININICHANGE = &H1A&
' Constant name changed only for 9x/NT4/2000 apps!
Private Const WM_SETTINGCHANGE = WM_WININICHANGE
Private Const WM_DESTROY = &H2

Implements ISubclass

Private m_hWnd As Long

Public Enum ESPISystemParamtersInfoConstants
   SPI__NONSYSTEMPARAMETERSINFOCHANGE = 0

   SPI_GETBEEP = 1
   SPI_SETBEEP = 2
   SPI_GETMOUSE = 3
   SPI_SETMOUSE = 4
   SPI_GETBORDER = 5
   SPI_SETBORDER = 6
   SPI_GETKEYBOARDSPEED = 10
   SPI_SETKEYBOARDSPEED = 11
   SPI_LANGDRIVER = 12
   SPI_ICONHORIZONTALSPACING = 13
   SPI_GETSCREENSAVETIMEOUT = 14
   SPI_SETSCREENSAVETIMEOUT = 15
   SPI_GETSCREENSAVEACTIVE = 16
   SPI_SETSCREENSAVEACTIVE = 17
   SPI_GETGRIDGRANULARITY = 18
   SPI_SETGRIDGRANULARITY = 19
   SPI_SETDESKWALLPAPER = 20
   SPI_SETDESKPATTERN = 21
   SPI_GETKEYBOARDDELAY = 22
   SPI_SETKEYBOARDDELAY = 23
   SPI_ICONVERTICALSPACING = 24
   SPI_GETICONTITLEWRAP = 25
   SPI_SETICONTITLEWRAP = 26
   SPI_GETMENUDROPALIGNMENT = 27
   SPI_SETMENUDROPALIGNMENT = 28
   SPI_SETDOUBLECLKWIDTH = 29
   SPI_SETDOUBLECLKHEIGHT = 30
   SPI_GETICONTITLELOGFONT = 31
   SPI_SETDOUBLECLICKTIME = 32
   SPI_SETMOUSEBUTTONSWAP = 33
   SPI_SETICONTITLELOGFONT = 34
   SPI_GETFASTTASKSWITCH = 35
   SPI_SETFASTTASKSWITCH = 36
   SPI_SETDRAGFULLWINDOWS = 37
   SPI_GETDRAGFULLWINDOWS = 38
   SPI_GETNONCLIENTMETRICS = 41
   SPI_SETNONCLIENTMETRICS = 42
   SPI_GETMINIMIZEDMETRICS = 43
   SPI_SETMINIMIZEDMETRICS = 44
   SPI_GETICONMETRICS = 45
   SPI_SETICONMETRICS = 46
   SPI_SETWORKAREA = 47
   SPI_GETWORKAREA = 48
   SPI_SETPENWINDOWS = 49

   SPI_GETHIGHCONTRAST = 66
   SPI_SETHIGHCONTRAST = 67
   SPI_GETKEYBOARDPREF = 68
   SPI_SETKEYBOARDPREF = 69
   SPI_GETSCREENREADER = 70
   SPI_SETSCREENREADER = 71
   SPI_GETANIMATION = 72
   SPI_SETANIMATION = 73
   SPI_GETFONTSMOOTHING = 74
   SPI_SETFONTSMOOTHING = 75
   SPI_SETDRAGWIDTH = 76
   SPI_SETDRAGHEIGHT = 77
   SPI_SETHANDHELD = 78
   SPI_GETLOWPOWERTIMEOUT = 79
   SPI_GETPOWEROFFTIMEOUT = 80
   SPI_SETLOWPOWERTIMEOUT = 81
   SPI_SETPOWEROFFTIMEOUT = 82
   SPI_GETLOWPOWERACTIVE = 83
   SPI_GETPOWEROFFACTIVE = 84
   SPI_SETLOWPOWERACTIVE = 85
   SPI_SETPOWEROFFACTIVE = 86
   SPI_SETCURSORS = 87
   SPI_SETICONS = 88
   SPI_GETDEFAULTINPUTLANG = 89
   SPI_SETDEFAULTINPUTLANG = 90
   SPI_SETLANGTOGGLE = 91
   SPI_GETWINDOWSEXTENSION = 92
   SPI_SETMOUSETRAILS = 93
   SPI_GETMOUSETRAILS = 94
   SPI_SETSCREENSAVERRUNNING = 97
   SPI_SCREENSAVERRUNNING = SPI_SETSCREENSAVERRUNNING
   SPI_GETFILTERKEYS = 50
   SPI_SETFILTERKEYS = 51
   SPI_GETTOGGLEKEYS = 52
   SPI_SETTOGGLEKEYS = 53
   SPI_GETMOUSEKEYS = 54
   SPI_SETMOUSEKEYS = 55
   SPI_GETSHOWSOUNDS = 56
   SPI_SETSHOWSOUNDS = 57
   SPI_GETSTICKYKEYS = 58
   SPI_SETSTICKYKEYS = 59
   SPI_GETACCESSTIMEOUT = 60
   SPI_SETACCESSTIMEOUT = 61
   SPI_GETSERIALKEYS = 62
   SPI_SETSERIALKEYS = 63
   SPI_GETSOUNDSENTRY = 64
   SPI_SETSOUNDSENTRY = 65
   SPI_GETSNAPTODEFBUTTON = 95
   SPI_SETSNAPTODEFBUTTON = 96
   SPI_GETMOUSEHOVERWIDTH = 98
   SPI_SETMOUSEHOVERWIDTH = 99
   SPI_GETMOUSEHOVERHEIGHT = 100
   SPI_SETMOUSEHOVERHEIGHT = 101
   SPI_GETMOUSEHOVERTIME = 102
   SPI_SETMOUSEHOVERTIME = 103
   SPI_GETWHEELSCROLLLINES = 104
   SPI_SETWHEELSCROLLLINES = 105
   SPI_GETMENUSHOWDELAY = 106
   SPI_SETMENUSHOWDELAY = 107
   SPI_GETSHOWIMEUI = 110
   SPI_SETSHOWIMEUI = 111

   ' Win98/2000 only:
   SPI_GETMOUSESPEED = 112
   SPI_SETMOUSESPEED = 113
   SPI_GETSCREENSAVERRUNNING = 114

   SPI_GETACTIVEWINDOWTRACKING = &H1000&
   SPI_SETACTIVEWINDOWTRACKING = &H1001&
   SPI_GETMENUANIMATION = &H1002&
   SPI_SETMENUANIMATION = &H1003&
   SPI_GETCOMBOBOXANIMATION = &H1004&
   SPI_SETCOMBOBOXANIMATION = &H1005&
   SPI_GETLISTBOXSMOOTHSCROLLING = &H1006&
   SPI_SETLISTBOXSMOOTHSCROLLING = &H1007&
   SPI_GETGRADIENTCAPTIONS = &H1008&
   SPI_SETGRADIENTCAPTIONS = &H1009&
   SPI_GETKEYBOARDCUES = &H100A&
   SPI_SETKEYBOARDCUES = &H100B&
   SPI_GETMENUUNDERLINES = SPI_GETKEYBOARDCUES
   SPI_SETMENUUNDERLINES = SPI_SETKEYBOARDCUES
   SPI_GETACTIVEWNDTRKZORDER = &H100C&
   SPI_SETACTIVEWNDTRKZORDER = &H100D&
   SPI_GETHOTTRACKING = &H100E&
   SPI_SETHOTTRACKING = &H100F&
   SPI_GETMENUFADE = &H1012&
   SPI_SETMENUFADE = &H1013&
   SPI_GETSELECTIONFADE = &H1014&
   SPI_SETSELECTIONFADE = &H1015&
   SPI_GETTOOLTIPANIMATION = &H1016&
   SPI_SETTOOLTIPANIMATION = &H1017&
   SPI_GETTOOLTIPFADE = &H1018&
   SPI_SETTOOLTIPFADE = &H1019&
   SPI_GETCURSORSHADOW = &H101A&
   SPI_SETCURSORSHADOW = &H101B&

   SPI_GETUIEFFECTS = &H103E&
   SPI_SETUIEFFECTS = &H103F&

   SPI_GETFOREGROUNDLOCKTIMEOUT = &H2000&
   SPI_SETFOREGROUNDLOCKTIMEOUT = &H2001&
   SPI_GETACTIVEWNDTRKTIMEOUT = &H2002&
   SPI_SETACTIVEWNDTRKTIMEOUT = &H2003&
   SPI_GETFOREGROUNDFLASHCOUNT = &H2004&
   SPI_SETFOREGROUNDFLASHCOUNT = &H2005&
   SPI_GETCARETWIDTH = &H2006&
   SPI_SETCARETWIDTH = &H2007&
End Enum
Public Event SettingsChange(ByVal wParam As ESPISystemParamtersInfoConstants)


Public Sub Attach(ByVal hWndA As Long)
   Detach
   m_hWnd = hWndA
   AttachMessage Me, m_hWnd, WM_SETTINGCHANGE
   AttachMessage Me, m_hWnd, WM_DESTROY
End Sub
Public Sub Detach()
   If Not m_hWnd = 0 Then
      DetachMessage Me, m_hWnd, WM_SETTINGCHANGE
      DetachMessage Me, m_hWnd, WM_DESTROY
      m_hWnd = 0
   End If
End Sub

Private Sub Class_Terminate()
   Detach
End Sub

Private Property Let ISubClass_MsgResponse(ByVal RHS As EMsgResponse)
   '
End Property

Private Property Get ISubClass_MsgResponse() As EMsgResponse
   ISubClass_MsgResponse = emrPreprocess
End Property

Private Function ISubClass_WindowProc( _
      ByVal hwnd As Long, ByVal iMsg As Long, _
      ByVal wParam As Long, ByVal lParam As Long) As Long
   Select Case iMsg
   Case WM_SETTINGCHANGE
      RaiseEvent SettingsChange(wParam)
   Case WM_DESTROY
      Detach
   End Select
End Function

To test the class, add a ListBox control to your project's form and then add the following code to the form:

Option Explicit

Private WithEvents m_c As cSettingsChange

Private Sub Form_Load()
   Set m_c = New cSettingsChange
   m_c.Attach Me.hwnd
End Sub

Private Sub m_c_SettingsChange(ByVal wParam As ESPISystemParamtersInfoConstants)
   If wParam > SPI__NONSYSTEMPARAMETERSINFOCHANGE Then
      List1.AddItem "SystemParametersInfo Change: " & wParam
   Else
      List1.AddItem "Settings Change"
   End If
End Sub

Run the project. Bring up the system display settings and change some of the colours on the appearance tab. When you choose Apply or OK, an event will be generated and displayed in the ListBox.