giovedì 11 dicembre 2014

Creare uno screensaver per Windows

Gli screensaver (salvaschermo) sono programmi che si avviano quando l'utente non fa più nulla sullo schermo per un lungo periodo. Erano nati con i televisori a tubo catodico che si rovinavano con le immagini ferme.
Windows utilizza come salvaschermo i file .SCR, i quali non sono altro che dei normali programmi .EXE rinominati.
Livecode è lo strumento più adatto per creare un programma del genere.
Prima di tutto creiamo lo stack principale che useremo solo quando programmiamo con Livecode, del tipo:


e mettiamo le pulsante Impostazioni un semplice:

on mouseUp
   go to stack "impostazioni"
end mouseUp


mentre per il pulsante Avvia salvaschermo mettiamo il seguente codice:

on mouseUp
   set the stato of card 1 of stack "salvaschermo" to "test"
   go to stack "salvaschermo"
end mouseUp


Creiamo un sotto stack per le impostazioni, al momento il nostro salvaschermo è talmente semplice che non servirebbe, ma così in futuro possiamo aggiungere al salvaschermo tutte le opzioni che vogliamo.


Ora creiamo un sotto-stack che rappresenterà il salvaschermo e lo chiameremo salvaschermo. Facciamolo con lo schermo nero e con l'ora e la data che fluttuano a caso per lo schermo, quindi ci basterà fare il fondo nero dello stack, mettere un campo di testo e mettere un messaggio ciclico da chiamare, ad esempio ciclos:

on opencard
   put the screenrect into schermo
   put item 3 of schermo into tx
   put item 4 of schermo into ty
   switch the stato of me
      case "test"
         put round(tx / 2) into tx
         put round(ty / 2) into ty
         set the rect of this stack to 0,0,tx,ty
         break
      case "preview"
         set the rect of this stack to (0,0,tx / 10,ty / 10)
         set the loc of this stack to tx/2,ty/2
         break
      default
         set the rect of this stack to the screenrect
   end switch   
   ciclos
end opencard

on ciclos
   set the text of field 1 to ( (the long system time) & " - " & (the long system date))
   set the width of field 1 to (the formattedwidth of field 1)
   set the foregroundcolor of field 1 to (random(255),random(255),random(255))
   put the width of me into tx
   put the height of me into ty
   move field 1 to (random(tx),random(ty)) without waiting
   send ciclos to me in 1 sec
end ciclos

on mouseDown
   if the stato of me is not "test" then    cancellatutto
end mouseDown

on mouseMove
   if the stato of me is not "test" then    cancellatutto
end mouseMove

on rawkeyDown
   cancellatutto
end rawkeyDown

on cancellatutto  
   if the stato of me is "test" then     
      cancelmessage ciclos
      close this stack
   else
      quit
   end if
end cancellatutto

on cancelmessage myMex
   repeat for each line templ in the pendingmessages
      if item 3 of templ is myMex then   
         cancel item 1 of templ
      end if
   end repeat
end cancelmessage


Come vedete ho messo anche i messaggi per  controllare se stiamo facendo delle prove o se lo screensaver è lanciato da windows. Se lo stiamo utilizzando noi la proprietà personalizzata stato ha il valore "test", e quindi lo screensaver se ne va solo premendo un pulsante, altrimenti anche muovendo il mouse. Stessa cosa quando chiudiamo il programma: in un caso chiude solo lo screensaver, nell'altro chiude tutto (quit).
levare il programma.
Il nostro screensaver animatodovrebbe avere un aspetto simile a questo:


A questo punto il nostro screensaver è quasi pronto, quando viene posto trai gli screensaver dentro la cartella C:\Windows\System32 viene chiamato con delle opzioni:
  • /p vuol dire che vuole la preview piccola nello schermo
  • /c vuol dire che vuole la finestra di configurazione (può essere seguito da numeri tipo /c:75829)
  • /s vuol dire modalità salvaschermo normale
Quindi se viene chiamato con /p dobbiamo creare uno stack piccolo piccolo per la piccola preview nel monitor di esempio; se viene chiamato con la /c bisogna avere una finestra di configurazione, con /s deve avviarsi il salvaschermo. Per questo motivo ci conviene mettere il seguente codice nello stack principale:

on PreOpenStack
   put char 1 to 2 of $1 into temp
   switch temp
      case "/p"
         set the stato of card 1 of stack "salvaschermo" to "prewiev"
         hide me
         go to stack "salvaschermo"
         break
      case "/c"
         hide me
         go to stack "Impostazioni"
         break
      case "/s"         
         hide me
         set the stato of card 1 of stack "salvaschermo" to empty
         go to stack "salvaschermo"
      default
         #qui possiamo mettere altre azioni da fare all'avio                     
   end switch   
end PreOpenStack


Ora possiamo creare l'eseguibile .EXE, rinominarlo in .SCR e metterlo nella cartella C:\Windows\System32. Provato e funziona, voi ora potete sbizzarrirvi, mettendo la vostra foto, una frase a caso letta da internete, effetti grafici, ecc.

Nessun commento:

Posta un commento