Arquivo da tag: Tela

VBA – Alterando o tempo de espera da Proteção de Tela do Windows

A necessidades que aparecem na construção de aplicativos vão das mais óbvias até as mais bizarras.

Pois bem, surgiu a necessidade em um dos sistemas do qual participei a construção de poder alterar o tempo de espera da proteção de espera no Windows. Esquisito, mas necessário e era preciso arrumar um jeito de fazê-lo. Funçando na net, encontrei num canto o código abaixo que faz o trabalho de um jeito não muito simples, mas efetivo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
                                              (ByVal uiAction As Integer, _
                                               ByVal uiParam As Integer, _
                                               ByVal pvParam As Integer, _
                                               ByVal fWinIni As Integer) As Integer
 
Private Declare Function SystemParametersInfoPointer Lib "user32" Alias "SystemParametersInfoA" _
                                                     (ByVal uiAction As Integer, _
                                                      ByVal uiParam As Integer, _
                                                      ByRef pvParam As Integer, _
                                                      ByVal fWinIni As Integer) As Integer
 
Private Const SPI_SETSCREENSAVETIMEOUT As Integer = 15
Private Const SPI_GETSCREENSAVETIMEOUT As Integer = 14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDWININICHANGE As Integer = &H2
 
Public Function GetScreenSaverTimeout() As Integer
    Dim ptSaverTimeOut As Integer
    Dim Result As Integer
 
    Result = SystemParametersInfoPointer(SPI_GETSCREENSAVETIMEOUT, 0, ptSaverTimeOut, 0)
    If Result <> 0 Then
        GetScreenSaverTimeout = ptSaverTimeOut
    Else
        GetScreenSaverTimeout = -1
    End If
End Function
 
Public Function SetScreensaverTimeout(ByVal tm As Integer) As Boolean
    If (tm < 60) Or (tm > 3600) Then
        Exit Function
    End If
    Dim Result As Integer
    Result = SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, tm, 0, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
 
    If Result = 0 Then
        SetScreensaverTimeout = False
    End If
 
    If GetScreenSaverTimeout() = tm Then
        SetScreensaverTimeout = True
    Else
        SetScreensaverTimeout = False
    End If
End Function

O código faz uso de chamadas de API, o que é de certa forma uma tarefa cuidadosa. Os método em destaque é o SystemParametersInfo, que além de executar a função de alterar as configurações da proteção de tela, é possível através dela promover alterações em todas as configurações acessadas geralmente pelo Painel de Controle do Windows.

Os métodos abaixo são um exemplo de chamada para recuperar ou configurar o tempo de espera da proteção de tela do Windows. Vale lembrar que o tempo é sempre definido em segundos.

1
2
3
4
5
6
7
Public Sub PegaTempoEsperaAtual()
     MsgBox GetScreenSaverTimeout
End Sub
 
Public Sub DefineTempoEspera()
     MsgBox SetScreensaverTimeout(300)
End Sub

Basta colocar todo o código em um módulo tradicional para fazer funcioná-lo sem problemas.

Bom proveito!

Tomás Vásquez
www.tomasvasquez.com.br