/* program : a simple screen saver
* file name : screensaver.cs
* author : tran khanh hien
* date : 06/20/2001
* email : hientk@yahoo.com
*/
namespace screen_saver
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.winforms;
using system.data;
/// <summary>
/// summary description for form1.
/// </summary>
public class screensaver : system.winforms.form
{
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.container components;
private system.winforms.timer timersaver;
private system.winforms.label lblmarquee;
private int ispeed = 2;
private string strmarqueetext="c sharp screen saver";
private system.drawing.font fontmarquee = new system.drawing.font ("arial", 20, system.drawing.fontstyle.bold);
private color colormarquee = system.drawing.color.fromargb(255,255,255);
private int idistance;
private int ixstart= 0;
private int iystart= 0;
public screensaver()
{
initializecomponent();
lblmarquee.font=fontmarquee;
lblmarquee.forecolor=colormarquee;
system.drawing.cursor.hide();
}
/// <summary>
/// clean up any resources being used.
/// </summary>
public override void dispose()
{
base.dispose();
components.dispose();
}
/// <summary>
/// required method for designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
system.resources.resourcemanager resources = new system.resources.resourcemanager (typeof(screensaver));
this.components = new system.componentmodel.container ();
this.timersaver = new system.winforms.timer (this.components);
this.lblmarquee = new system.winforms.label ();
//@this.trayheight = 90;
//@this.traylargeicon = false;
//@this.trayautoarrange = true;
//@timersaver.setlocation (new system.drawing.point (7, 7));
timersaver.interval = 1;
timersaver.enabled = true;
timersaver.tick += new system.eventhandler (this.timersaver_tick);
lblmarquee.location = new system.drawing.point (88, 0);
lblmarquee.size = new system.drawing.size (128, 48);
lblmarquee.forecolor = system.drawing.color.white;
lblmarquee.tabindex = 0;
lblmarquee.visible = false;
this.maximizebox = false;
this.startposition = system.winforms.formstartposition.manual;
this.autoscalebasesize = new system.drawing.size (5, 13);
this.borderstyle = system.winforms.formborderstyle.none;
this.keypreview = true;
this.windowstate = system.winforms.formwindowstate.maximized;
this.showintaskbar = false;
this.icon = (system.drawing.icon) resources.getobject ("$this.icon");
this.controlbox = false;
this.minimizebox = false;
this.backcolor = system.drawing.color.black;
this.clientsize = new system.drawing.size (300, 300);
this.keydown += new system.winforms.keyeventhandler (this.form1_keydown);
this.mousedown += new system.winforms.mouseeventhandler (this.form1_mousedown);
this.mousemove += new system.winforms.mouseeventhandler (this.form1_mousemove);
this.controls.add (this.lblmarquee);
}
protected void timersaver_tick (object sender, system.eventargs e)
{
lblmarquee.text=strmarqueetext;
lblmarquee.height=lblmarquee.font.height;
lblmarquee.width=lblmarquee.text.length*(int)lblmarquee.font.size;
playscreensaver();
}
private void playscreensaver()
{
//get the working area of the the computer screen.
system.drawing.rectangle ssworkarea = system.winforms.screen.getworkingarea(this);
lblmarquee.location=new system.drawing.point(ssworkarea.width – idistance,
lblmarquee.location.y);
//make the label visible if it is not currently visible.
lblmarquee.visible=true;
// increment the label distance based on the speed set by the user.
idistance += ispeed;
// if the label is offscreen, then we want to reposition it to the right.
if (lblmarquee.location.x <= -(lblmarquee.width))
{
//reset the distance to 0.
idistance = 0;
//if the label is at the top, move it to the middle.
if (lblmarquee.location.y == 0)
lblmarquee.location=new system.drawing.point(lblmarquee.location.x,(ssworkarea.height / 2));
// if label is in the middle of the screen move it to the bottom.
else if(lblmarquee.location.y== ssworkarea.height /2)
lblmarquee.location=new system.drawing.point(lblmarquee.location.x,ssworkarea.height – lblmarquee.height);
//move the label back to the top.
else
lblmarquee.location=new system.drawing.point(lblmarquee.location.x,0);
}
}
protected void form1_mousedown (object sender, system.winforms.mouseeventargs e)
{
stopscreensaver();
}
protected void form1_mousemove (object sender, system.winforms.mouseeventargs e)
{
// determine if the mouse cursor position has been stored previously.
if (ixstart == 0 && iystart == 0)
{
//store the mouse cursor coordinates.
ixstart = e.x;
iystart = e.y;
return;
}
// has the mouse cursor moved since the screen saver was started?
else if (e.x != ixstart || e.y != iystart)
stopscreensaver();
}
private void stopscreensaver()
{
system.drawing.cursor.show();
timersaver.enabled=false;
application.exit();
}
protected void form1_keydown (object sender, system.winforms.keyeventargs e)
{
stopscreensaver();
}
/// <summary>
/// the main entry point for the application.
/// </summary>
public static void main(string[] args)
{
if (args.length==1)
{
//display the options dialog box.
if (args[0].substring(0,2).equals("/c"))
{
messagebox.show("options are not available for this screen saver",
" c# screen saver",
messagebox.iconinformation);
application.exit();
}
//start the screen saver normally.
else if (args[0]=="/s")
application.run(new screensaver());
//diaplay the password dialog
else if (args[0]=="/a")
{
messagebox.show("passwords are not available for this screen saver",
" c# screen saver",
messagebox.iconinformation);
application.exit();
}
}
//for any other args –> start.
else
application.run(new screensaver());
}
}
}
