AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

basic screensaver
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Sat Sep 23, 2006 2:12 pm    Post subject: basic screensaver Reply with quote

iīve made a little script, that acts like a screensaver.
If compiled and renamed to *.scr it will be launched by windows
like any other screensaver. So far it will only show a blank screen.
But i guess it can be a base for making own screensavers.

this is the 3rd version actually:

Code:
; Simple (Non-)Screensaver framework V3.1(can be used to create own Screensavers by compiling and renaming to .scr).  This script requires v1.0.44.12 or later
; This script consists of :
; many code-snippets from the AHK-Help File and of certain lines from this forum ;-)
; it was inspired by the input from this Forum, the Scrrensaver tutorial at http://www.wischik.com/scr/howtoscr.html and the MSDN help pages
; so itīs mostly not my code
; the updated version contains contribution from: Murple & majkinetor
;--------------------------------------------------------------------------------------------------------------------------------------
#NoTrayIcon
#SingleInstance force
CoordMode, Mouse, Screen
;--------------------------------------------------------------------------------------------------------------------------------------
;this is the main section, which will run on startup
;as the behaviour expected of the saver depends on the command-line arguments it is given
;first the command-line arguments are checked using the
;build in vars 0,1,2,3,... ,where 0 contains the number of command-line arguments given
;there are several rules for reacting to command-line arguments
;if the command line arguments are invalid, then the saver should terminate immediately without doing anything.
;if argument = /c, /c ####, or no arguments at all - in response to any of these the saver should pop up its configuration dialog.
;if argument = /s - this indicates that the saver should run itself as a full-screen saver.
;if argument = /p ####, or /l #### - here the saver should treat the #### as the decimal representation of an HWND, and  pop up a child window to run in preview mode inside that window.
;if argument = /a #### - this argument is only used in '95 and Plus! The saver should pop up a password-configuration dialog.
;the command-line options may appear as lower-case or upper-case, and that there might be either a forward slash or a hyphen prefixing the letter.
; this script does not support all the modes
; it will show no pw-change window under W9x
;--------------------------------------------------------------------------------------------------------------------------------------
if 0 = 0 ; checks if no command-line arguments have been passed
{
runmode = config ; the var runmode is used to decide what action the screensaver shall perform
confparam = false ; confparam will be false if no valid cmd-line arguments have been passed
}
else
{
   Loop, %0%  ; For each parameter:
   {
      param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
      if param contains s,S
      {
         runmode = show
         confparam = true
         break
      }
      if param contains c,C
      {
         runmode = config
         confparam = true
         nextparam := A_Index + 1
         ParentHwnd := %nextparam% ;Decimal representation of optional HWND 
        SetFormat, integer, hex
        ParentHwnd += 0       ;Standard hex HWND
        SetFormat, integer, d
         break
      }
      if param contains p,P,l,L
      {
        runmode = preview
        confparam = true
      nextparam := A_Index + 1
        ParentHwnd := %nextparam% ;Decimal representation of HWND for SSDEMOPARENT1 control of Display Properties window.
       SetFormat, integer, hex
       ParentHwnd += 0       ;Standard hex HWND for SSDEMOPARENT1
       SetFormat, integer, d
        break
      }
      if param contains a,A
      {
         runmode = pwconfig
         confparam = true
         break
      }
      else
      {
         runmode = undefined
      }
   }
}
; load the configuration via the getconf subroutine
GoSub, getconf
; decide what mode to start
; right now only show, config and preview (thanks to murple) are supported, all other modes will exit the saver
if runmode = show
{
   GoSub, svrshow
   return
}
if runmode = config
{
   GoSub, svrconfig
   return
}
if runmode = preview
{
   GoSub, svrpreview
   return
}
if runmode = pwconfig
{
   GoSub, svrend
   return
}
if runmode = undefined
{
   GoSub, svrend
   return
}
GoSub, svrend
return
;--------------------------------------------------------------------------------------------------------------------------------------
 ; the getconfig subroutine should contain all the internal and external configuration
; it is called by the startup section prior to all other subroutines
;--------------------------------------------------------------------------------------------------------------------------------------
getconf:
MouseGetPos, MousePosX, MousePosY ; stores original coursor-position to put it back there before stopping the saver
;here the saver configuration is read from the registry and set to default values if no configuration data is present in the registry
; make shure to chose your own values for CompanyName and Product Name
RegRead, CustomColor, HKEY_CURRENT_USER, Software\AHKSaver\MyAHKSaver\, BGColor
if not CustomColor
{
CustomColor = 000000  ; Can be any RGB color (000000 is black)
}
RegRead, Mouseoffset, HKEY_CURRENT_USER, Software\AHKSaver\MyAHKSaver\, MOffset
if not Mouseoffset
{
Mouseoffset = 3  ; Can be any full number
}
return
;--------------------------------------------------------------------------------------------------------------------------------------
; the ButtonOK label is run when the OK Button from scrconfig is pressed
; it stores the configuration in the registry
;.It should be saved in the registry in the standard location: HKEY_CURRENT_USER\Software\MyCompany\MyProduct\.
;--------------------------------------------------------------------------------------------------------------------------------------
ButtonOK:
Gui, Submit
; make shure to chose your own values for CompanyName and Product Name
RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\AHKSaver\MyAHKSaver\, BGColor, %config1%
RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\AHKSaver\MyAHKSaver\, MOffset, %config2%
Gosub, svrend
return
;--------------------------------------------------------------------------------------------------------------------------------------
; WM_ENDAPP is the handler for the incoming Windows Messages
; it will call the svrend subroutine to end the screensaver if any of the Messages above will arrive
;--------------------------------------------------------------------------------------------------------------------------------------
WM_ENDAPP()
{
Gosub, svrend
return
}
;--------------------------------------------------------------------------------------------------------------------------------------
; CHECKENDAPP is the handler for the incoming Windows Message WM_MOUSEMOVE
; it will call the svrend subroutine to end the screensaver if the Mouse has left the parking position
;--------------------------------------------------------------------------------------------------------------------------------------
WM_CHECKEND()
{
MouseGetPos, MouseX, MouseY
; first make the global parking position var accessible in this funktion
global LowerLimitX
global LowerLimitY
global UpperLimitX
global UpperLimitY
; then check if mouse has left the parking position
if MouseX not between %LowerLimitX% and %UpperLimitX%
GoSub, svrend
if MouseY not between %LowerLimitY% and %UpperLimitY%
GoSub, svrend
return
}
;--------------------------------------------------------------------------------------------------------------------------------------
;This is the programm closing subroutine, all other subroutines or funtions will call this to exit the saver
; GuiClose and svrend will both execute the following
;--------------------------------------------------------------------------------------------------------------------------------------
GuiClose: ;  in case the gui was aborted
svrend:
;the next line should unblock Ctrl+Alt+Esc and Alt+Tab under Win9x, but itīs not tested and only neccesary with the corresponding DllCall-Line above
;DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "0", "UInt*", previousstate, "UInt", "0")
Gui, Destroy
if (runmode = "config") ; reenable the underlying window, that was disabled by the svrconfig:-subrutine
{
   WinSet, Enable, , ahk_id %ParentHWND%
   WinActivate, ahk_id %ParentHWND%
}
if (runmode = "show") ; only reset mouseposition if in show-mode
{
   DllCall("SetCursorPos", int, MousePosX, int, MousePosY) ; moves the Cursor back to where it was before screensaver started
}
ExitApp
;--------------------------------------------------------------------------------------------------------------------------------------
; svrconfig shows the configuration dialog of the saver
;the ButtonOK subroutine stores the changes if OK is pressed
; this mode is NOT implemented, as following:
; With /c  as an argument, use the ForegroundWindow as its parent.
;With /c ####  the saver should treat #### as the decimal representation of an HWND, and use this as its parent.
;If there are no arguments then NULL should be used as the parent.
;Since this behaviour is a little different than other screensavers actually show, it is commented out rigth now, uncomment the marked lines if you like
; instead the underlying window is disabled and reenabled when the server ends.
;--------------------------------------------------------------------------------------------------------------------------------------
svrconfig:
; choose the right parent window
if (ParentHWND = "" OR !WinExist("ahk_id " . ParentHWND))
{
   ParentHWND := WinExist("A")   
}
if (confparam = "false")
{
   ParentHWND := 0
}
; creat config Gui
Gui, Add, Text,, Backgroundcolor in RGB-Hex (000000 is black) :
Gui, Add, Edit, vconfig1, %CustomColor%
Gui, Add, Text,, Minimum movement the Mouse has to make to quit the saver (in pixel) :
Gui, Add, Edit, vconfig2, %Mouseoffset%
Gui, Add, Button, default, OK  ; The label ButtonOK  will be run when the button is pressed.
Gui + Lastfound
WinSet, Disable, , ahk_id %ParentHWND%
;DllCall( "SetParent", "uint", WinExist(), "uint", ParentHwnd) ; UNCOMMENT IF YOU LIKE: make the preview-Gui a child of the HWND passed through the command-line
Gui, Show, , Simple Saver Configuration
return
;--------------------------------------------------------------------------------------------------------------------------------------
;svrpreview previews the screensaver inside the Display Properties window.
;therefore it should end with an endless Loop
;the saver terminates itself through the GuiClose:-subroutine when the owning window is destroyed by the system,
;the Preview-GUI -as a child-window- is destroyed, too. So the GuiClose:-subroutine is triggered
;
; attention, the preview in this example actually displays a picture not related to the actual screensaver
;--------------------------------------------------------------------------------------------------------------------------------------
svrpreview:
ControlGetPos, , , ssW, ssH, , ahk_id %ParentHwnd% ; get the Size of the window, thatīs supposed to be the parent of the preview window
Gui, -Caption
Gui, Add, Picture, x0 y0 w%ssW% h%ssH% , %windir%\winnt.bmp ; %ssW% and %ssH% are the Size and Width of the preview-window, use them to scale the controls
Gui +LastFound ; prepare for the following DDL-Call
DllCall( "SetParent", "uint", WinExist(), "uint", ParentHwnd) ; make the preview-Gui a child of the HWND passed through the command-line
Gui, Show, w%ssW% h%ssH% x0 y0 ; show the preview-GUI relative to the owning control
Loop
{
   sleep 5000
}
return
;--------------------------------------------------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------------------------------------------------
;this is the actual screensaver
;the svrshow subroutine will display the fullscreen saver
;therefore it should end with an endless Loop
;the saver terminates itself by incoming Windows Messages, so
;there is no need to continuously check the mouse positon or any keystrokes
;--------------------------------------------------------------------------------------------------------------------------------------
svrshow:
; this block creates a fullscreen window without any borders in any color set in the var "CustomColor"
Gui, +AlwaysOnTop +LastFound +Owner  ; +Owner prevents a taskbar button from appearing.
Gui, Color, %CustomColor%
Gui, -Caption  ; Remove the title bar and window borders.
Gui, Show, x0 y0 Maximize
DllCall("SetCursorPos", int, A_ScreenWidth+Mouseoffset, int, A_ScreenHeight+Mouseoffset) ; sets the mouse to a position outside the visible sreen and thus making the cursor unvisible
sleep, 100
; this block checks the actual mouse position and creates defines the parking position of the mouse
MouseGetPos, MouseX, MouseY
LowerLimitY := (MouseY - Mouseoffset)
UpperLimitY := (MouseY + Mouseoffset)
LowerLimitX := (MouseX - Mouseoffset)
UpperLimitX := (MouseX + Mouseoffset)
; the following OnMessage commands make the Screensaver listen to several Window Messages
; to get information if the mouse was moved, a mouse-button pressed or a system key or non-system key on the keybord was pressed
; whenever the system sends one of the messages below , the script will start the WM_ENDAPP() funktion
; this will eventually exit the saver
OnMessage(0x201, "WM_ENDAPP") ; WM_LBUTTONDOWN : indicates that the left mouse button was pressed
OnMessage(0x204, "WM_ENDAPP") ; WM_RBUTTONDOWN : indicates that the right mouse button was pressed
OnMessage(0x207, "WM_ENDAPP") ; WM_MBUTTONDOWN : indicates that the middle mouse button was pressed
OnMessage(0x200, "WM_CHECKEND") ; WM_MOUSEMOVE : indicates that the mouse was moved
OnMessage(0x100, "WM_ENDAPP") ; WM_KEYDOWN : indicates that a non-system key was pressed
OnMessage(0x104, "WM_ENDAPP") ; WM_SYSKEYDOWN : indicates that a system key was pressed
OnMessage(0x1C, "WM_ENDAPP") ; WM_ACTIVATEAPP : indicates that another application needs the focus
OnMessage(0x06, "WM_ENDAPP") ; WM_ACTIVATE : indicates that another application needs the focus
OnMessage(0x10, "WM_ENDAPP") ; WM_CLOSE : indicates that the system wants to close the program
;the next line should block Ctrl+Alt+Esc and Alt+Tab under Win9x, but itīs not tested and only neccesary for pw-protection under Win9x
;DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "1", "UInt*", previousstate, "UInt", "0")
Loop
{
   sleep, 5000
}
return


this post was updated with new code, so the answers may refer to nonexisting code
_________________
1) All my code can be reused in ANY way. 2) Please check the help and the forum-search, before posting questions; the answer is out there...


Last edited by Zed Gecko on Thu Jan 08, 2009 6:27 pm; edited 3 times in total
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10667

PostPosted: Sun Sep 24, 2006 10:40 pm    Post subject: Reply with quote

Nice script. Although I haven't tried it, I think your approach appeals to people who like simple, working solutions (like me).
Back to top
View user's profile Send private message Send e-mail
PhiLho



Joined: 27 Dec 2005
Posts: 6723
Location: France (near Paris)

PostPosted: Mon Sep 25, 2006 11:12 am    Post subject: Reply with quote

Well, it looks like a screen saver, but isn't one by Windows standards... See How to write a 32bit screen saver for the gory details...

Among other things, it should handle at least the /c /p (or /l) and /s parameters (plus /a), manage password in Win9x, call SystemParametersInfo(SPI_SETSCREENSAVERRUNNING,TRUE, ...) to lock Ctrl+Alt+Del), and so on...
Note also that theoritically, WinXP awaken the screen saver itself, ie. no need for monitoring the mouse or keyboard.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Sat Sep 30, 2006 11:59 pm    Post subject: Reply with quote

Thanks for the link, itīs quite interesting but difficult.
I think reacting to command-line options is mostly harmless, but
the SystemParemetersInfo call is probably way out of my league.

After several hours of reaserch i came up with this line:
Code:

DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "1", "Str", "&oldval", "UInt", "0")

to indicate the screensaver is running and
Code:
DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "0", "Str", "&oldval", "UInt", "1")

to indicate it has stoped again.

Both DllCalls return the Errorlevel 0, so it should be a success,
but i canīt observe any effect.

Am I just wrong, or is it trickier?
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6723
Location: France (near Paris)

PostPosted: Sun Oct 01, 2006 10:52 am    Post subject: Reply with quote

Well, if I understood correctly, these calls should make the screen saver immune to the use of Ctrl+Alt+Del to bypass the password entry phase, plus it allows other programs to check if the SS is running or not.

Thanks for reseaching. Being fully compliant isn't probably mandatory, I have seen many SSs written in Visual Basic that felt in the same pitfall. But then, they are unusable in some cases, because they offer no protection.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Sun Oct 01, 2006 5:36 pm    Post subject: Reply with quote

I found now the right article on msdn http://support.microsoft.com/?scid=kb%3Ben-us%3B226359&x=19&y=11
it says:
Quote:
SUMMARY
This article describes how to disable task switching and other system functions accessed through key combinations such as CTRL+ESC and ALT+TAB on Win32 Platforms.
Back to the top Back to the top
Windows 95 and Windows 98
Applications can enable and disable ALT+TAB and CTRL+ESC, for example, by calling SystemParametersInfo (SPI_SETSCREENSAVERRUNNING). To disable ALT+TAB and CTRL+ESC, set the uiParam parameter to TRUE; to enable the key combinations, set the parameter to FALSE:

UINT nPreviousState;

// Disables task switching
SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, TRUE, &nPreviousState, 0);

// Enables task switching
SystemParametersInfo (SPI_SETSCREENSAVERRUNNING, FALSE, &nPreviousState, 0);


Note Applications that use SystemParametersInfo (SPI_SETSCREENSAVERRUNNING) to disable task switching must enable task switching before exiting or task switching remains disabled after the process terminates.


So I found out that

1. SPI_SETSCREENSAVERRUNNING is only neccessary for Win9x Systems.
My Win2000 System blocks Alt+Tab, etc. with anything run as a Screensaver (even a MsgBox). So I guess, all WinNT-based Systems will block these Key-Combinations on their own, when the screensaver is running.

2. My Code-Guess was incorrect, i know believe that it should be
Code:
DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "1", "UInt*", previousstate, "UInt", "0")

for blocking , and
Code:
DllCall("SystemParametersInfo", "Str", "SPI_SETSCREENSAVERRUNNING", "UInt", "0", "UInt*", previousstate, "UInt", "0")

for unblocking, where "previousstate" is just the name of the var which will contain the previous state of SPI_SETSCREENSAVERRUNNING.
but this is still untested, caused by the lack of a Win9x System
Back to top
View user's profile Send private message
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Mon Oct 02, 2006 5:39 am    Post subject: Version 2 of my little try Reply with quote

After some more research and downloading the newest ahk version Wink
i came up with a new version
it uses OnMessage instead of timed subroutines and
supports (partially) the command-line arguments
it can be configured and saves the configuration in the registry
only about 10 lines of code have survived

Code:
See this topic's top post for latest code.
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6723
Location: France (near Paris)

PostPosted: Mon Oct 02, 2006 10:24 am    Post subject: Reply with quote

Great, you are a persistent guy, and you created a real framework now.
I suggest that you edit the first message and either you replace the first script, or point to the "final" one.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Mon Oct 02, 2006 7:15 pm    Post subject: Reply with quote

thnx,
and i have learned a lot by doing this
Back to top
View user's profile Send private message
Maurice118



Joined: 04 Dec 2006
Posts: 1

PostPosted: Mon Dec 04, 2006 12:14 am    Post subject: Reply with quote

Very Happy I want to say thank you for this nice script. It helped me alot to write my own screensaver that can start a program.
Saved me alot of time to do this not from scratch.

Thanks Zed Gecko

Maurice Cool
Back to top
View user's profile Send private message
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Fri Dec 08, 2006 5:05 am    Post subject: Reply with quote

thnx a lot
Back to top
View user's profile Send private message
gmoney
Guest





PostPosted: Mon Jan 29, 2007 11:43 pm    Post subject: run a command on screen saver exit? Reply with quote

Is it possible to run a command on screensaver exit?

I tried inserting a "Run" command in the WM_ENDAPP() subroutine, but it does not work. Also tried WM_CHECKEND() and tried at the end of the script right before "ExitApp".

The funny thing is if I test from the command line or use the "Preview" button it works fine. When the system launches the screensaver it doesn't work.

Any idea how to fix? or any idea what is different about the system calling the screensaver as opposed to calling in manually?
Back to top
Zed Gecko



Joined: 23 Sep 2006
Posts: 120

PostPosted: Thu Feb 08, 2007 5:22 am    Post subject: Reply with quote

Well, to run a command on screensaver exit, i suggest to
add a OnExit subrutine to the code.
http://www.autohotkey.com/docs/commands/OnExit.htm

This is probably the savest way.
To be honest, i donīt know how the system stops
the screensaver when itīs really running.
I just followed the guidelines Philo posted above.
And from what i found out about screensavers, there are several
things that work different on the different versions of Windows.
So i would not rely on the functions triggered by Windows Messages,
maybe some versions of Windows just kill the process.
Back to top
View user's profile Send private message
Murp|e



Joined: 12 Jan 2007
Posts: 474
Location: Norway

PostPosted: Tue Dec 16, 2008 11:08 am    Post subject: Reply with quote

Has anyone actually used this framework to create a screensaver and/or updated the framework itself? I searched the forums for "screensaver" and -oddly- didn't find this thread, but when I searched the documentation I did. This framework is exactly what I've been looking for and more than I had hoped for!

I briefly tested it yesterday and it seems to work fine out of the box. Is there any way to preview a small version of the screensaver in the Display Properties window itself? As shown here.

Alternatively, does anyone know how I could an image in the Display Properties window? I think somehow a BMP image would need to be included in the .EXE file as a resource for this to work (ResourceHacker type thing.) Would it be possible to update this image dynamically or would it be static?
Back to top
View user's profile Send private message Visit poster's website
Z_Gecko
Guest





PostPosted: Tue Dec 16, 2008 3:12 pm    Post subject: Reply with quote

I think it is possible to show a live preview in the Display Properties Screen.
To qoute myself:
Code:
;if argument = /p ####, or /l #### - here the saver should treat the #### as the decimal representation of an HWND, and  pop up a child window to run in preview mode inside that window.


So you need to react on that cmd-line parameter and display a small gui, and make it a child of the Display Properties Screen. Iīm not shure how to position it(not even if itīs neccessary), but you could try to get the coordinates of the Static1-Control.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group