Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleGet or set the current screen saver
DescriptionThis example shows how to get or set the current screen saver in Visual Basic 6. It reads and updates System.ini. The "boot" section's "SCRNSAVE.EXE" value determines the current screen saver.
Keywordsscreen saver, INI, INI file, get INI value, Windows directory, get Windows directory
CategoriesWindows, Files and Directories
 
When the program starts, it sets the Path property of two FileListBox controls to the system's Windows and System directories. Those are the directories where screen saver programs are usually placed. Screen savers have a .scr extension so the FileListBoxes display files with that extension.

The program then calls subroutine ShowScreenSaver to display the current screen saver.

 
Private Sub Form_Load()
    ' List screen saver files.
    filWindows.Path = GetWindowsDir()
    filSystem.Path = GetSystemDir()

    ShowScreenSaver
End Sub
 
The GetWindowsDir helper function uses the GetWindowsDirectory API function to find the system's Windows directory. Similarly the GetSystemDir function uses the GetSystemDirectory API function to return the System directory.
 
' Return the Windows directory path.
Private Function GetWindowsDir() As String
Const MAX_PATH_LEN As Integer = 260
Dim win_dir As String
Dim ret_len As Long

    ' Get the windows directory.
    win_dir = Space$(MAX_PATH_LEN)
    ret_len = GetWindowsDirectory( _
        win_dir, Len(win_dir))
    GetWindowsDir = Left$(win_dir, ret_len)
End Function

' Return the System directory path.
Private Function GetSystemDir() As String
Const MAX_PATH_LEN As Integer = 260
Dim win_dir As String
Dim ret_len As Long

    ' Get the windows directory.
    win_dir = Space$(MAX_PATH_LEN)
    ret_len = GetSystemDirectory( _
        win_dir, Len(win_dir))
    GetSystemDir = Left$(win_dir, ret_len)
End Function
 
Subroutine ShowScreenSaver uses the GetIniString function to display the value stored in the System.ini file's "boot" section's "SCRNSAVE.EXE" value.

The GetIniString helper function uses the GetPrivateProfileString API function to read values from an INI file.

 
' Display the current screen saver.
Private Sub ShowScreenSaver()
Dim screen_saver As String

    screen_saver = GetIniString( _
        GetWindowsDir() & "\System.ini", _
        "boot", "SCRNSAVE.EXE", "<none>")
    lblScreenSaver.Caption = screen_saver
End Sub

Private Function GetIniString(ByVal file_name As String, _
    ByVal section As String, ByVal key_name As String, _
    ByVal default As String) As String
Const MAX_LEN As Integer = 2408
Dim ret_buffer As String
Dim ret_len As Long

    ret_buffer = Space$(MAX_LEN)
    ret_len = GetPrivateProfileString( _
        section, key_name, default, _
        ret_buffer, Len(ret_buffer), file_name)
    GetIniString = Left$(ret_buffer, ret_len)
End Function
 
When the user selects a screen saver file from the Windows or System FileListBox, the program enables the corresponding Set button. When the user clicks this button, the program calls function ShortFileName to convert the selected file's name into its short version (for example, "C:\WINDOWS\SYSTEM\3D Text.scr" becomes "C:\WINDOWS\SYSTEM\3DTEXT~1.SCR").

It then uses the SetIniString subroutine to set the System.ini file's "boot" section's "SCRNSAVE.EXE" value to the short file name.

 
Private Sub cmdSetWindows_Click()
Dim screen_saver As String

    screen_saver = ShortFileName( _
        GetWindowsDir() & "\" & filWindows.FileName)
    SetIniString _
        GetWindowsDir() & "\System.ini", _
        "boot", "SCRNSAVE.EXE", screen_saver

    ShowScreenSaver
End Sub
 
The ShortFileName helper function uses the GetShortPathName API function to convert a long file name into a short file name.
 
' Return the short file name for a long file name.
Public Function ShortFileName(ByVal long_name As String) As _
    String
Dim length As Long
Dim short_name As String

    short_name = Space$(1024)
    length = GetShortPathName( _
        long_name, short_name, _
        Len(short_name))
    ShortFileName = Left$(short_name, length)
End Function
 
The SetIniString helper subroutine uses the WritePrivateProfileString API function to set an INI file's value.
 
Private Sub SetIniString(ByVal file_name As String, ByVal _
    section As String, ByVal key_name As String, ByVal _
    value As String)
    WritePrivateProfileString section, key_name, _
        value, file_name
End Sub
 
Finally, when the user clicks the Run button, the program activates the current screen saver by sending the WM_SYSCOMMAND message with the SC_SCREENSAVE parameter.
 
Private Sub cmdRun_Click()
    SendMessage hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, ByVal 0&
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated