/**************************************************************************************************
* 作者wenwenhua
*
/* Program : A Simple Screen Saver
* File Name : ScreenSaver.cs
* Author : Tran Khanh Hien
* Date : 06/20/2001
* email : [email protected]
*/
****************************************************************************************************/
//应用程序主逻辑 screansaver 类
//其他的类文件简单,在这就不丢人了,把破的东西SHOW 出来是需要勇气的!
using System;
using System.Windows.Forms;
namespace SAVER
{
///
/// screansaver 的摘要说明。
///
public class screansaver
{
public screansaver() { }
///
/// 应用程序的主入口点。
///
//[STAThread]
static void Main()
{
Form1 myform;
if( MessageBox.Show("是否设置应用程序?","notice",MessageBoxButtons.OKCancel) ==DialogResult.OK )
{
passset ps=new passset();
ps.ShowDialog();
myform = new Form1(ps.filepathtip.Text,(int)ps.numTimespan.Value,ps.usepwd.Checked);
ps.Dispose();
}
else
{
myform=new Form1();
}
if(myform.ShowDialog()==DialogResult.Cancel)
{
myform.Dispose();
Application.Exit();
}
}
}
}
//form1 的源码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace SAVER
{
///
/// Form1 的摘要说明。
///
public class Form1: System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private Timer timer;
public int TimeSpan=4000 ;
private string bgimagepath =Directory.GetCurrentDirectory()+@"/images";
public string BgImageFilePath
{
get
{
return this.bgimagepath;
}
set
{
if( value!= "" &&Directory.Exists(value))
this.bgimagepath=value;
}
}
public string ScreanPix;
string[] strPath;
public bool usepwd=true;
Random ra;
Image[] bt;
Enterpwd ps;
public Form1(string bgimage,int timespan,bool usepwd)
{
InitializeComponent();
this.BgImageFilePath = bgimage;
ra=new Random(0);
this.timer.Interval = timespan;
this.usepwd=usepwd;
this.DialogResult=DialogResult.None;
this.SaveFileToMem();
}
private void SaveFileToMem()
{
Cursor.Hide();
strPath=Directory.GetFiles(this.BgImageFilePath);
bt=new Bitmap[strPath.Length];
//将指定的目录下的图片载入,存入内存中,(笨方法)
for(int i=0;i
bt[i]=Image.FromFile(strPath[i]);
}
}
public Form1()
{
InitializeComponent();
ra=new Random(0);
this.SaveFileToMem();
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
//code added by me!!!
for(int i=0;i < strPath.Length;i++)
{
bt[i].Dispose();
}
timer.Dispose();
ra=null;
}
base.Dispose( disposing );
}// 清理所有正在使用的资源。
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.timer = new System.Windows.Forms.Timer(this.components);
//
// timer
//
this.timer.Enabled = true;
this.timer.Interval = 4000;
this.timer.Tick += new System.EventHandler(this.timer_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
//this.ClientSize = new System.Drawing.Size(1016, 741);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Opacity = 0.9;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
}
#endregion
protected void Form1_KeyDown ( object sender , KeyEventArgs e )
{
this.CheckPermission();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
this.CheckPermission();
}
private void CheckPermission()
{
if(this.usepwd== false )
{
this.DialogResult=DialogResult.Cancel;
}
else
{
ps=new Enterpwd();
ps.ShowDialog();
if(ps.IsLegalUser == true )
{
this.DialogResult=DialogResult.Cancel;
ps.Dispose();
}
else{}
}
}
double p=0.5;
int i=0;
private void timer_Tick(object sender, EventArgs e)
{
i++;
if( ps!=null && i==3)
{
ps.Dispose();
i=0;
}
if(p>=0.5)
{
p+=0.1;
}if(p>=1.0)
{
p-=0.5;
}
this.Opacity=p;
this.BackgroundImage=bt[ra.Next(strPath.Length)];
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.CheckPermission();
}
}
}
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace SAVER
{
///
/// PwdOperate 是用于密码保护的类。
///
public class PwdOperate
{
public static bool Hadsetted()
{
return ReadUserInfo()!="";
}
public PwdOperate(){}
private static string PwdFilePath = Directory.GetCurrentDirectory()+@"/pwdfile.txt";
public static bool SetAndModifyUserPwd(string username,string pwd)
{
if(File.Exists( PwdFilePath ))
{
//将文件内容删除,再写.
using ( File.Open( PwdFilePath,FileMode.Truncate ) ){}
}
return SetUserPwd( username,pwd );
}
private static bool SetUserPwd(string username,string pwd)
{
string str = string.Concat(username,"|");
str = string.Concat( str,MD5Encrypt(pwd) );
return WriteUserInfo( str );
}
public static bool CheckUserPwd(string user,string pwd)
{
string str = string.Concat( user,"|", MD5Encrypt( pwd ));
string str2 = ReadUserInfo();
return str.Equals( str2 );
}
private static string ReadUserInfo()
{
string str="";
if( File.Exists( PwdFilePath ) )
{
// create reader & open file
TextReader tr = new StreamReader( PwdFilePath );
// read a line of text
str=tr.ReadLine();
// close the stream
tr.Close();
} return str;
/* using (Stream fs = File.OpenRead( PwdFilePath ) )
{
//byte[] b = new byte[1024];
//UTF8Encoding temp = new UTF8Encoding(true);
//while (fs.Read(b,0,b.Length) > 0)
while(fs.readl)
{
str=temp.GetString(b);
}
}
*/
}
private static bool WriteUserInfo(string strToWrite)
{
if(File.Exists( PwdFilePath ))
{
byte[] info = new UTF8Encoding(true).GetBytes( strToWrite );
using (FileStream fs = File.OpenWrite( PwdFilePath ) )
{
fs.Write(info,0,info.Length);
fs.Flush();
}
return true;
}
return false;
}
///
/// mdb加密
///
///
///
private static string MD5Encrypt ( string data)
{
MD5 md5 = new MD5CryptoServiceProvider();
string encryptedPwd=BitConverter.ToString( md5.ComputeHash(UTF8Encoding.Default.GetBytes(data)) );
md5.Clear();
return encryptedPwd;
}
}
}