?
Solved

How to Use SystemParametersInfo API

Posted on 2006-05-04
4
Medium Priority
?
3,406 Views
Last Modified: 2008-01-09
I'm writing a program for my company that will run and automatically reset the screen saver timeout. Working through the registry will allow changes but only with a reboot, which won't work for us.

I know that setting the timeout and applying it immediately is possible through the SystemParametersInfo API but I am unable to find a working VB .NET example of it.

If anyone can supply a working example I'd appreciate it.

This is very important, so 500 points to a working example! =)
0
Comment
Question by:FIIS-VSO
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16607739
Working example:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class SystemMetricsInfo
  Public BorderWidth As Integer
  Public CaptionHeight As Integer
  Public CaptionWidth As Integer
  Public MenuHeight As Integer
  Public MenuWidth As Integer
  Public ScrollHeight As Integer
  Public ScrollWidth As Integer
  Public SmCaptionHeight As Integer
  Public SmCaptionWidth As Integer
  Public FontCaption As Font
  Public FontMenu As Font
  Public FontMessage As Font
  Public FontSmCaption As Font
  Public FontStatus As Font
End Class

Public Class SystemParameters

  Private Enum ParamOption
    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
    SPI_GETMOUSESPEED = 112
    SPI_SETMOUSESPEED = 113
    SPI_GETSCREENSAVERRUNNING = 114
    SPI_GETDESKWALLPAPER = 115
    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

  Private Const LOGPIXELSY As Integer = &H5A

  Private m_hDC As Integer

  Private Declare Auto Function SystemParametersInfo _
   Lib "user32" Alias "SystemParametersInfoA" _
   (ByVal uiAction As ParamOption, ByVal uiParam As Integer, _
   ByRef pvParam As NONCLIENTMETRICS, ByVal fWinIni As Integer) As Integer


  Private Declare Auto Function SystemParametersInfo _
   Lib "user32" Alias "SystemParametersInfoW" _
   (ByVal uiAction As ParamOption, ByVal uiParam As Integer, _
   ByVal pvParam As StringBuilder, ByVal fWinIni As Integer) As Integer


  Private Declare Function MulDiv _
   Lib "kernel32" _
   (ByVal nNumber As Integer, ByVal nNumerator As Integer, _
   ByVal nDenominator As Integer) As Integer


  Private Declare Function GetDeviceCaps _
   Lib "gdi32" _
   (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer


  Private Declare Function GetActiveWindow Lib "user32" () As Integer

  Private Declare Function GetDC _
   Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer

  Private Declare Function ReleaseDC _
   Lib "user32" Alias "ReleaseDC" _
   (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer

  <StructLayout(LayoutKind.Sequential)> _
  Private Structure LOGFONT
    Public lfHeight As Integer
    Public lfWidth As Integer
    Public lfEscapement As Integer
    Public lfOrientation As Integer
    Public lfWeight As Integer
    Public lfItalic As Byte
    Public lfUnderline As Byte
    Public lfStrikeOut As Byte
    Public lfCharSet As Byte
    Public lfOutPrecision As Byte
    Public lfClipPrecision As Byte
    Public lfQuality As Byte
    Public lfPitchAndFamily As Byte
    <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=32)> _
    Public lfFaceName As String
  End Structure

  <StructLayout(LayoutKind.Sequential)> _
  Private Structure NONCLIENTMETRICS
    Public cbSize As Integer
    Public iBorderWidth As Integer
    Public iScrollWidth As Integer
    Public iScrollHeight As Integer
    Public iCaptionWidth As Integer
    Public iCaptionHeight As Integer
    Public lfCaptionFont As LOGFONT
    Public iSmCaptionWidth As Integer
    Public iSmCaptionHeight As Integer
    Public lfSmCaptionFont As LOGFONT
    Public iMenuWidth As Integer
    Public iMenuHeight As Integer
    Public lfMenuFont As LOGFONT
    Public lfStatusFont As LOGFONT
    Public lfMessageFont As LOGFONT
  End Structure


  Public ReadOnly Property SystemMetrics() As SystemMetricsInfo

    Get

      Dim hWnd As Integer = GetActiveWindow()

      m_hDC = GetDC(hWnd)

      Dim ncm As NONCLIENTMETRICS
      ncm.cbSize = Marshal.SizeOf(ncm)

      Dim result As Integer = _
       SystemParametersInfo(ParamOption.SPI_GETNONCLIENTMETRICS, _
       ncm.cbSize, ncm, 0)

      Dim info As New SystemMetricsInfo

      If result <> 0 Then

        info.BorderWidth = ncm.iBorderWidth
        info.CaptionHeight = ncm.iCaptionHeight
        info.CaptionWidth = ncm.iCaptionWidth
        info.MenuHeight = ncm.iMenuHeight
        info.MenuWidth = ncm.iMenuWidth
        info.ScrollHeight = ncm.iScrollHeight
        info.ScrollWidth = ncm.iScrollWidth
        info.SmCaptionHeight = ncm.iSmCaptionHeight
        info.SmCaptionWidth = ncm.iSmCaptionWidth

        info.FontCaption = MakeFont(ncm.lfCaptionFont)
        info.FontMenu = MakeFont(ncm.lfMenuFont)
        info.FontMessage = MakeFont(ncm.lfMessageFont)
        info.FontSmCaption = MakeFont(ncm.lfSmCaptionFont)
        info.FontStatus = MakeFont(ncm.lfStatusFont)

      End If

      ReleaseDC(hWnd, m_hDC)

      Return info
    End Get

  End Property   'SystemMetrics


  Private Function MakeFont(ByVal apiFont As LOGFONT) As Font

    Dim pixelsY As Integer = GetDeviceCaps(m_hDC, LOGPIXELSY)

    Dim fontSize As Integer = MulDiv(-apiFont.lfHeight, 72, pixelsY)

    Return New Font(apiFont.lfFaceName, fontSize)

  End Function   'MakeFont


  Public Shared ReadOnly Property DesktopWallpaper() As String
    Get
      Try

        Dim name As New StringBuilder(260)

        Dim result As Integer = _
         SystemParametersInfo(ParamOption.SPI_GETDESKWALLPAPER, _
         name.Capacity, name, 0)

        Return name.ToString()

      Catch ex As Exception

        MessageBox.Show(ex.ToString)
      End Try

    End Get
  End Property   'DesktopWallpaper

End Class


Bob
0
 
LVL 2

Author Comment

by:FIIS-VSO
ID: 16609684
Wow...

I meant an example specific to the Screen Saver timeout...
That's a bit much to filter through for one parameter!
^_^
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16616592
Private Const SPI_GETSCREENSAVERRUNNING As Int32 = 114

Private Declare Auto Function SystemParametersInfo _
   Lib "user32" Alias "SystemParametersInfoA" _
   (ByVal uiAction As ParamOption, ByVal uiParam As Int32, _
   ByRef isActive As Boolean, ByVal fWinIni As Int32) As Int32

Private Declare Auto Function SystemParametersInfo _
   Lib "user32" Alias "SystemParametersInfoA" _
   (ByVal uiAction As ParamOption, ByVal uiParam As Int32, _
   ByRef timeOut As Int32, ByVal fWinIni As Int32) As Int32

  Public Shared Function ScreenSaverActive()
    Dim isActive As Boolean
    Dim result As Int32 = SystemParametersInfo(ParamOption.SPI_GETSCREENSAVERRUNNING, 0, isActive, 0)
    Return isActive
  End Function

  Public Shared Function GetScreenSaverTimeout() As Int32
    Dim timeOut As Int32
    Dim result As Int32 = SystemParametersInfo(ParamOption.SPI_GETSCREENSAVETIMEOUT, 0, timeOut, 0)
    Return timeOut
  End Function

  Public Shared Function SetScreenSaverTimeout(ByVal seconds As Int32, ByVal permanent As Boolean) As Boolean
    Const SPIF_SENDWININICHANGE As Int32 = &H2
    Const SPIF_UPDATEINIFILE As Int32 = &H1
    Dim fuWinIni As Int32
    If permanent Then
      fuWinIni = SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE
    End If
    Return SystemParametersInfo(ParamOption.SPI_SETSCREENSAVETIMEOUT, seconds, 0, fuWinIni) <> 0
  End Function

Bob
0
 
LVL 2

Author Comment

by:FIIS-VSO
ID: 16617795
Ah ha!
Excellent!
That worked great!
Thanks, Bob!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question