用C# 编写的一个简单屏保程序

发布时间:2009年06月04日      浏览次数:1112 次
代码如下,请高手帮我在里面添加音乐!!!随便添加一个好听的音乐就好了....谢谢了
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
public class ScreenSaver : Form
{
private System.ComponentModel.IContainer components ;
private Timer timerSaver ;
private Label lblMarquee ;
private int speed = 12 ;
private string strMarqueeText = "用C#制造的屏幕保护 " ;
private Font fontMarquee = new Font ( "Arial " , 20 , FontStyle.Bold ) ;
private Color colorMarquee = Color.BlueViolet ;
private int iDistance ;
private int ixStart = 0 ;
private int iyStart = 0 ;
public ScreenSaver ( )
{
InitializeComponent ( ) ;
lblMarquee.Font=fontMarquee ;
lblMarquee.ForeColor=colorMarquee ;
Cursor.Hide ( ) ;
}
/// 清理所有正在使用的资源。
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
components = new System.ComponentModel.Container ( ) ;
timerSaver = new Timer ( components ) ;
lblMarquee = new Label ( ) ;
SuspendLayout ( ) ;
timerSaver.Enabled = true ;
timerSaver.Interval = 1 ;
timerSaver.Tick += new System.EventHandler ( timerSaver_Tick ) ;
lblMarquee.ForeColor = Color.White ;
lblMarquee.Location = new Point ( 113 , 0 ) ;
lblMarquee.Name = "lblMarquee " ;
lblMarquee.Size = new Size ( 263 , 256 ) ;
lblMarquee.TabIndex = 0 ;
lblMarquee.Visible = false ;
AutoScaleBaseSize = new Size ( 6 , 14 ) ;
BackColor = Color.Black ;
ClientSize = new Size ( 384 , 347 ) ;
ControlBox = false ;
this.Controls.Add ( lblMarquee) ;
this.KeyPreview = true ;
this.MaximizeBox = false ;
this.MinimizeBox = false ;
this.Name = "ScreenSaver " ;
//窗体运行后无边界
this.FormBorderStyle = FormBorderStyle.None ;
//程序运行后不显示在任务栏上
this.ShowInTaskbar = false ;
//窗体运行后,最大化,充满整个屏幕
this.WindowState = FormWindowState.Maximized ;
this.StartPosition = FormStartPosition.Manual ;
this.KeyDown += new KeyEventHandler ( Form1_KeyDown ) ;
this.MouseDown += new MouseEventHandler ( Form1_MouseDown ) ;
this.MouseMove += new MouseEventHandler ( Form1_MouseMove ) ;
ResumeLayout ( false ) ;
}
protected void timerSaver_Tick ( object sender , System.EventArgs e )
{
int randomum1 ;
Random r1 = new Random();
randomum1 = (int)(600*r1.NextDouble());
lblMarquee.Text = strMarqueeText ;
lblMarquee.Height = lblMarquee.Font.Height ;
lblMarquee.Width = 350 ;
//得到计算机屏幕的工作区域
Rectangle ssWorkArea = Screen.GetWorkingArea ( this ) ;
lblMarquee.Location = new Point ( ssWorkArea.Width - iDistance ,
lblMarquee.Location.Y ) ;
randomum1 = (int)(ssWorkArea.Width*r1.NextDouble());
//显示标签
lblMarquee.Visible = true ;
// 增加2个象素点,你可以通过修改speed的值来改变标签的移动速度
iDistance += speed ;
// 如果标签已经走出屏幕,则把标签的位置重定位到屏幕的右边
if ( lblMarquee.Location.X <= -( lblMarquee.Width ) )
{
iDistance = 0 ;
//初始位置随机产生 从右面出来
lblMarquee.Location = new Point ( lblMarquee.Location.X , randomum1 ) ;
}
}
protected void Form1_MouseDown ( object sender , MouseEventArgs e )
{
Cursor .Show ( ) ;
timerSaver.Enabled = false ;
Application .Exit ( ) ;
}
protected void Form1_MouseMove ( object sender , MouseEventArgs e )
{
// 把鼠标刚刚开始移动的位置给记录下来
if ( ixStart == 0 && iyStart == 0 )
{
ixStart = e.X ;
iyStart = e.Y ;
return ;
}
//判断自屏幕保护程序运行后,鼠标的位置是否变动
else if ( e.X != ixStart || e.Y != iyStart )
{
Cursor .Show ( ) ;
timerSaver.Enabled = false ;
Application .Exit ( ) ;
};
}
protected void Form1_KeyDown ( object sender , KeyEventArgs e )
{
Cursor .Show ( ) ;
timerSaver.Enabled = false ;
Application .Exit ( ) ;
}
public static void Main ( ) //string [ ] args
{
Application.Run ( new ScreenSaver ( ) ) ;
}
}
发表于:2007-05-22 09:48:453楼 得分:0
using System.Runtime.InteropServices;
public static uint SND_ASYNC = 0x0001; // play asynchronously
public static uint SND_FILENAME = 0x00020000; // name is file name
[DllImport( "winmm.dll ")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);
private void button1_Click(object sender, EventArgs e)
{
mciSendString(@ "close temp_alias ", null, 0, 0);
mciSendString(@ "open " "E:\音乐\周杰伦-东风破.mp3 " " alias temp_alias ",
null, 0, 0);
mciSendString( "play temp_alias ", null, 0, 0); // 重复播放用 "play temp_alias repeat "
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!