Sure
(please forgive me for the "crap" in the body of the message,
but my mail server does not allow attachments for newsgroups)
Whenever you see:
this.Image2Show.OutImg=new Bitmap(10,10);
Instead of "new Bitmap(10,10);" I have an image as embedded
resource in my project so that if the image specified doesnot
exist, it would still draw something.
I changed it to blank bitmap 10x10, so that there is no need
for you to worry about embedded resources.
Here is the source (3 files):
Program.cs (main entry point for the program):
using System;
using System.Windows.Forms;
using System.Globalization;
namespace NurchiScreenSaver
{
/// <summary>
/// Summary description for Program.
/// </summary>
public class Program
{
public Program()
{
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if ((args!=null)&&(args.Length>0))
{
string
arg=args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2);
switch (arg)
{
case "/p": // preview in the window
//Do nothing for now
break;
case "/s": // Show
(new ScreenSaverForm()).ShowDialog();
break;
case "/c": // Open Settings window
//(new SettingsForm()).ShowDialog();
//If you need source code for this too,
//I will send it.
break;
default:
CommonStuff.ShowUsageInfo();
break;
}
}
else
//CommonStuff.ShowUsageInfo();
//(new SettingsForm()).ShowDialog();
(new ScreenSaverForm()).ShowDialog();
//Application.Run(new ScreenSaverForm());
}
}
}
--------------------------------------------------------------------------------------------------------
ScreenSaverForm.cs:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace NurchiScreenSaver
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ScreenSaverForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public ScreenSaverForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Cursor.Hide();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// ScreenSaverForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 192);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "ScreenSaverForm";
this.ShowInTaskbar = false;
this.Text = "Nurchi\'s Screen Saver";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.ScreenSaverForm_MouseDown);
this.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.ScreenSaverForm_KeyPress);
this.Load += new System.EventHandler(this.ScreenSaverForm_Load);
this.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.ScreenSaverForm_MouseMove);
}
#endregion
#region User stuff
#region Variables
private float AngularSpeed;
private int RotateInterval;
private float Angle;
private bool IsActive;
private Bitmap _backBuffer;
private Point MouseLocation;
private string SettingsFileName=
"C:\\NurchiScreenSaver.Settings.xml";
OutputText[] Text2Show;
OutputImage Image2Show;
#endregion
#region Methods
#region Load Settings
private void LoadSettings()
{
System.Xml.XmlDocument xml=
new System.Xml.XmlDocument();
System.Xml.XmlNode RootNode, tmpNode;
if (System.IO.File.Exists(this.SettingsFileName))
{
xml.Load(this.SettingsFileName);
RootNode=xml.FirstChild.NextSibling;
tmpNode=RootNode.FirstChild;
this.Text2Show=new OutputText[RootNode.ChildNodes.Count-2];
int i=0;
for (i=0; i<this.Text2Show.Length; i++)
{
if (tmpNode.Name=="output")
{
this.Text2Show
.OutStr=tmpNode.FirstChild.InnerText;
this.Text2Show.OutX=int.Parse(tmpNode.FirstChild.NextSibling.InnerText);
this.Text2Show.OutY=int.Parse(tmpNode.FirstChild.NextSibling.NextSibling.InnerText);
this.Text2Show.FontSize=float.Parse(tmpNode.FirstChild.NextSibling.NextSibling.NextSibling.InnerText);
}
tmpNode=tmpNode.NextSibling;
}
MessageBox.Show("hello");
if (System.IO.File.Exists(tmpNode.FirstChild.InnerText))
this.Image2Show.OutImg=
Image.FromFile(tmpNode.FirstChild.InnerText);
else
this.Image2Show.OutImg=
new Bitmap(10,10);
this.Image2Show.OutX=int.Parse(tmpNode.FirstChild.NextSibling.InnerText);
this.Image2Show.OutY=int.Parse(tmpNode.FirstChild.NextSibling.NextSibling.InnerText);
this.Image2Show.size=
new Size(
int.Parse(tmpNode.FirstChild.NextSibling.NextSibling.NextSibling.InnerText),
int.Parse(tmpNode.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText));
tmpNode=tmpNode.NextSibling;
this.AngularSpeed=float.Parse(tmpNode.FirstChild.InnerText);
this.RotateInterval=int.Parse(tmpNode.FirstChild.NextSibling.InnerText);
this.timer1.Interval=(this.RotateInterval>0)?
this.RotateInterval:100;
}
else
{
this.Text2Show=new OutputText[3];
this.Image2Show=new OutputImage();
this.Text2Show[0].OutStr="Nurchi is a geek, isn't he?";
this.Text2Show[0].OutX=20;
this.Text2Show[0].OutY=20;
this.Text2Show[0].FontSize=30;
this.Text2Show[0].Brush=Brushes.Blue;
this.Text2Show[1].OutStr="Oh yes, he is!!!";
this.Text2Show[1].OutX=20;
this.Text2Show[1].OutY=-90;
this.Text2Show[1].FontSize=30;
this.Text2Show[1].Brush=Brushes.Blue;
this.Text2Show[2].OutStr="He made this!";
this.Text2Show[2].OutX=20;
this.Text2Show[2].OutY=-55;
this.Text2Show[2].FontSize=30;
this.Text2Show[2].Brush=Brushes.Blue;
this.Image2Show.OutImg=
new Bitmap(10,10);
this.Image2Show.OutX=250;
this.Image2Show.OutY=-100;
this.Image2Show.size=new Size(350, 350);
this.AngularSpeed=1;
this.RotateInterval=100;
this.timer1.Interval=this.RotateInterval;
CommonStuff.CreateXML(this.SettingsFileName);
}
}
#endregion
#region Show Text/Image
private void ShowText(OutputText d, Graphics g)
{
System.Drawing.Font f=
new Font(
"Times",
d.FontSize,
System.Drawing.FontStyle.Bold);
g.DrawString(
d.OutStr, f,
Brushes.Blue,
d.OutX,
d.OutY);
}
private void ShowText(OutputText d, Graphics g, Brush b)
{
System.Drawing.Font f=
new Font(
"Times",
d.FontSize,
System.Drawing.FontStyle.Bold);
g.DrawString(
d.OutStr, f,
b,
d.OutX,
d.OutY);
}
private void ShowText(OutputText d, Graphics g, Font f)
{
g.DrawString(
d.OutStr, f,
Brushes.Blue,
d.OutX,
d.OutY);
}
private void ShowText(OutputText d, Graphics g, Font f, Brush b)
{
g.DrawString(
d.OutStr, f,
b,
d.OutX,
d.OutY);
}
private void ShowImage(OutputImage img, Graphics g)
{
Size sz=this.FitImage(
img.OutImg, new Size(img.size.Width, img.size.Height));
g.FillRectangle(
Brushes.Blue,
-sz.Width/2-5, -sz.Height/2-5,
sz.Width+10, sz.Height+10);
g.DrawImage(
img.OutImg,
-sz.Width/2, -sz.Height/2,
sz.Width, sz.Height);
}
#endregion
#region Other methods/functions
private Size FitImage(Image img, Size sz)
{
int
sw=sz.Width, sh=sz.Height;
float
screenR=((float)sh)/((float)sw),
imageR=((float)img.Height)/((float)img.Width);
try
{
if (imageR<screenR)
img=new Bitmap(
img,
sw,
(int)(sw*imageR));
else if (imageR>screenR)
img=new Bitmap(
img,
(int)(sh/imageR),
sh);
else
img=new Bitmap(img, sw, sh);
}
catch{}
return img.Size;
}
#endregion
#endregion
#endregion
private void timer1_Tick(object sender, System.EventArgs e)
{
Angle+=this.AngularSpeed;
if(Angle>359)
Angle=0;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if(_backBuffer==null)
{
_backBuffer=
new Bitmap(
this.ClientSize.Width,
this.ClientSize.Height);
}
Graphics g=null;
g=Graphics.FromImage(_backBuffer);
g.Clear(Color.Black);
g.SmoothingMode=SmoothingMode.AntiAlias;
for (int i=0; i<this.Text2Show.Length; i++)
this.ShowText(this.Text2Show, g);
Matrix mx;
//Red rectangle
mx=new Matrix();
mx.Rotate(Angle,MatrixOrder.Append);
mx.Translate(this.ClientSize.Width/2-150,this.ClientSize.Height/2,MatrixOrder.Append);
g.Transform=mx;
g.FillRectangle(Brushes.Red,-150,-70,300,55);
System.Drawing.Font f=
new Font(
"Times",
30,
System.Drawing.FontStyle.Bold);
g.DrawString("Made by Nurchi", f, Brushes.Blue, -145, -65);
//Sub red rectangle
g.FillRectangle(Brushes.Purple, -150, 0, 300, 30);
f=
new Font(
"Times",
17,
System.Drawing.FontStyle.Bold);
g.DrawString("www.ece.ualberta.ca/~nurchi", f, Brushes.Blue, -145, 0);
//Blue rectangle
mx=new Matrix();
mx.Rotate(-Angle,MatrixOrder.Append);
//mx.Translate(this.ClientSize.Width/2+200,this.ClientSize.Height/2,MatrixOrder.Append);
mx.Translate(
this.ClientSize.Width/2+this.Image2Show.RelativeCorner.X,
this.ClientSize.Height/2+this.Image2Show.RelativeCorner.Y,
MatrixOrder.Append);
g.Transform=mx;
this.ShowImage(this.Image2Show, g);
g.Dispose();
e.Graphics.DrawImageUnscaled(_backBuffer,0,0);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//Don't allow the background to paint
}
protected override void OnSizeChanged(EventArgs e)
{
if(_backBuffer!=null)
{
_backBuffer.Dispose();
_backBuffer=null;
}
base.OnSizeChanged (e);
}
private void ScreenSaverForm_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (!this.IsActive)
{
this.MouseLocation=MousePosition;
this.IsActive=true;
}
else
{
if ((Math.Abs(MousePosition.X - this.MouseLocation.X) > 10) ||
(Math.Abs(MousePosition.Y - this.MouseLocation.Y) > 10))
{
this.Close();
}
}
}
private void ScreenSaverForm_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.Close();
}
private void ScreenSaverForm_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
this.Close();
}
private void ScreenSaverForm_Load(object sender, System.EventArgs e)
{
OutputText.s=this.Size;
this.LoadSettings();
}
}
}
--------------------------------------------------------------------------------------------------------
CommonStuff.cs:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace NurchiScreenSaver
{
#region Structs
public struct OutputText
{
public OutputText(
string Str,
int x, int y,
float fSize,
System.Drawing.Brush brush)
{
this.OutStr=Str;
this.outX=0;
this.outY=0;
this.FontSize=fSize;
this.Brush=brush;
this.OutX=x;
this.OutY=y;
}
public static Size s=new Size(0,0);
public string OutStr;
private int outX,outY;
public float FontSize;
public System.Drawing.Brush Brush;
public int OutX
{
get
{
return this.outX;
}
set
{
this.outX=
(value>=0)?value:OutputText.s.Width+value;
}
}
public int OutY
{
get
{
return this.outY;
}
set
{
this.outY=
(value>=0)?value:OutputText.s.Height+value;
}
}
}
public struct OutputImage
{
public OutputImage(
Image img, Point TopLeftCorner, Size s)
{
this.OutImg=img;
this.outX=0;
this.outY=0;
this.size=s;
this.imgFile="";
this.OutX=TopLeftCorner.X;
this.OutY=TopLeftCorner.Y;
}
public OutputImage(
string img, Point TopLeftCorner, Size s)
{
this.OutImg=null;
this.outX=0;
this.outY=0;
this.size=s;
this.imgFile="";
this.OutX=TopLeftCorner.X;
this.OutY=TopLeftCorner.Y;
this.ImgFileName=img;
}
public Size size;
public Image OutImg;
private string imgFile;
private int outX,outY;
public Point RelativeCorner
{
get
{
return new Point(
this.outX, this.outY);
}
}
public Point RealCorner
{
get
{
return new Point(
this.OutX, this.OutY);
}
}
public int OutX
{
get
{
return
(this.outX>=0)?
this.outX:
OutputText.s.Width+this.outX;
}
set
{
this.outX=value;
}
}
public int OutY
{
get
{
return
(this.outY>=0)?
this.outY:
OutputText.s.Height+this.outY;
}
set
{
this.outY=value;
}
}
public string ImgFileName
{
get
{
return this.imgFile;
}
set
{
if (System.IO.File.Exists(value))
{
this.imgFile=value;
this.OutImg=Image.FromFile(value);
}
else
{
this.OutImg=null;
this.imgFile="";
}
}
}
}
#endregion
public class CommonStuff
{
public static void ShowUsageInfo()
{
MessageBox.Show(
"Usage: NurchiScreenSaver [/s|/c]\r\n"+
"\t/s:\tShow the screen saver\r\n"+
"\t/c:\tOpen settings window\r\n"+
"Example:\r\n"+
"NurchiScreenSaver /s\r\n"+
"NurchiScreenSaver /c\r\n"+
"-----OR-----\r\n"+
"Unzip both files into the same folder\r\n"+
"Then run 'install.bat' file\r\n"+
"and open screen properties,\r\n"+
"click on Screen Saver tab and\r\n"+
"choose NurchiScreenSaver from the list",
"Usage of NurchiScreenSaver",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
public static void CreateXML(string SettingsFileName)
{
System.IO.TextWriter tw=
System.IO.File.CreateText(SettingsFileName);
tw.Write(
"<?xml version=\"1.0\" encoding=\"windows-1251\" ?>"+"\r\n"+
"<NurchiScreenSaver.Settings>"+"\r\n"+
"\t<output>"+"\r\n"+
"\t\t<text>Nurchi is a geek, isn't he?</text>"+"\r\n"+
"\t\t<x>20</x>"+"\r\n"+
"\t\t<y>20</y>"+"\r\n"+
"\t\t<size>30</size>"+"\r\n"+
"\t</output>"+"\r\n"+
"\t<output>"+"\r\n"+
"\t\t<text>Oh yes, he is!!!</text>"+"\r\n"+
"\t\t<x>20</x>"+"\r\n"+
"\t\t<y>-90</y>"+"\r\n"+
"\t\t<size>30</size>"+"\r\n"+
"\t</output>"+"\r\n"+
"\t<output>"+"\r\n"+
"\t\t<text>He made this!</text>"+"\r\n"+
"\t\t<x>20</x>"+"\r\n"+
"\t\t<y>-55</y>"+"\r\n"+
"\t\t<size>30</size>"+"\r\n"+
"\t</output>"+"\r\n"+
"\t<image>"+"\r\n"+
"\t\t<path>C:\\FULL_PATH_TO_YOUR_PICTURE_GOES_HERE.JPG</path>"+"\r\n"+
"\t\t<x>250</x>"+"\r\n"+
"\t\t<y>-100</y>"+"\r\n"+
"\t\t<width>350</width>"+"\r\n"+
"\t\t<height>350</height>"+"\r\n"+
"\t</image>"+"\r\n"+
"\t<speed>"+"\r\n"+
"\t\t<AngularSpeed>1</AngularSpeed>"+"\r\n"+
"\t\t<Interval>100</Interval>"+"\r\n"+
"\t</speed>"+"\r\n"+
"</NurchiScreenSaver.Settings>");
tw.Close();
}
}
}