Check if screen saver is active (Windows)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Check if screen saver is active (Windows)

Post by Michael Vogel »

I wrote a 'smart home' program which shows a map with the light state of dozens of bulbs. Checking all states means a short peak of network traffic, so this is regularly done only when there is user interaction. Otherwise I will do only a check every minute, but I 'd like to slow done this to lower rates, when the screen saver is on (and do a full update when the screen saver will stop)...

Don't think there's a windows message that a screen saving will be activated or stopped...
...but is there any other possibility to get some information about that?
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Check if screen saver is active (Windows)

Post by PureLust »

Hi Michael,

I found a blog with some Infos about how to check if a Screensaver is active :

https://devblogs.microsoft.com/scriptin ... %20running.

Zitat: "If we want to know whether or not the screen saver is running all we have to do is check and see if there are any processes running that have an executable name ending in .scr. If there are, that probably means the screen saver is running."

Gesendet von meinem LYA-L29 mit Tapatalk
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: Check if screen saver is active (Windows)

Post by Mijikai »

U can check if a screensaver is running.

Code: Select all

Debug SystemParametersInfo_(#SPI_GETSCREENSAVERRUNNING,#Null,#Null,#Null)
Mby also useful:

Code: Select all

Procedure.i IdleTimeInMilliseconds()
  Protected info.LASTINPUTINFO
  info\cbSize = SizeOf(LASTINPUTINFO)
  GetLastInputInfo_(@info)
  ProcedureReturn GetTickCount_() - info\dwTime
EndProcedure
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Check if screen saver is active (Windows)

Post by RASHAD »

Hi MV
Compile the next snippet as exe
Run the exe
Adapt it in your application

Code: Select all

Prototype SplashMessageSetup(hWnd,lpText$,lpCaption$,uType, wLanguageID.w,dwMilliseconds)
Procedure SplashMessage(title$,text$,ms=1000,icon=#MB_ICONINFORMATION)
  lib=OpenLibrary(#PB_Any,"user32.dll")
  If lib
    SplashMessageSetup.SplashMessageSetup=GetFunction(lib, "MessageBoxTimeoutW")
    SplashMessageSetup(GetForegroundWindow_(),text$,title$, #MB_SETFOREGROUND|#MB_TOPMOST|#MB_TASKMODAL|icon,0, ms)
    CloseLibrary(lib)
  EndIf
EndProcedure

Procedure WndProc(hwnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  Select uMsg
      
    Case #WM_SYSCOMMAND 
      Select wParam
        Case #SC_SCREENSAVE
          SplashMessage("Info","    Screen Saver is Running",2000)
          
      EndSelect      
      
  EndSelect   
  ProcedureReturn result 
EndProcedure

flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Test",Flags)
StickyWindow(0,1)

SetWindowCallback(@WndProc())
Repeat           
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit = 1
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Check if screen saver is active (Windows)

Post by Michael Vogel »

Thanks, will check all your hints, there's just one point (which I haven't mentioned in the post above)...
...some PC's don't start an explicit screen saver program, but do show a black screen after a while.

Anyhow I have checked SystemParametersInfo_(#SPI_GETSCREENSAVERRUNNING,0,0,0) some days before, but this never returned a value different to 0 :(

Update - seems that my initial question wasn't very precise, I've checked some PC's now and found most of them haven't enabled a screen saver but have configured a "monitor off" mode in the energy options. So the trick for IdleTimeInMilliseconds() could be the best now...
Last edited by Michael Vogel on Sun Jan 31, 2021 6:32 pm, edited 1 time in total.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Check if screen saver is active (Windows)

Post by PureLust »

Michael wrote: ...some PC's don't start an explicit screen saver program, but do show a black screen after a while.
Sounds like Monitor Powersaving mode.

I'm not sure, but I think I've posted a function to check for Powersaving modes a while ago.
I'll have a look if I can find it.


Gesendet von meinem LYA-L29 mit Tapatalk
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Check if screen saver is active (Windows)

Post by Michael Vogel »

'Powersave' sounds good as well...

..actually I'll try to implement something like to following - when going to active, the refresh rate will be high, in idle mode no refreshing will be done then:

Code: Select all


Procedure.i IdleOrActive()
	
	Protected info.LASTINPUTINFO
	Protected n,t
	
	Shared ioa_last
	Shared ioa_trend
	
	info\cbSize = SizeOf(LASTINPUTINFO)
  	GetLastInputInfo_(@info)
  	
  	n=GetTickCount_()-info\dwTime
  	t=ioa_trend
  	ioa_trend=Bool(n<=ioa_last)
  	ioa_last=n
  	
  	ProcedureReturn Bool(t<>ioa_trend)
  	
EndProcedure


For i=1 To 1000
	If IdleOrActive()
		Debug StringField("IDLE.ACTIVE",ioa_trend+1,".")+", "+Str(ioa_last)
	Else
		Debug "."
	EndIf
	Delay(1000)
Next i

User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Check if screen saver is active (Windows)

Post by Lunasole »

Yes, using lastinput info should be a best way to control user interaction.

I'm having slightly different code running at background -- it automatically enables screensaver with a short timeout when mouse pointer is parked at the top of the screen (Y <= 6), and disables else.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply