当前位置:文章写作网 >日记 >日记 >自己做屏保2

自己做屏保2

2007-11-06 11:18 作者:tshfang 阅读量:5862 推荐9次 | 我要投稿

下面是简单的一个字母屏保.文字只是在屏幕中走动,没有特效。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace screen

{

public partial class Form1 : Form

{

private int iDistance=200;

private int speed =10;

private int ixStart;

private int iyStart;

private int ScreenNumber;

public Form1(int scrn)

{

InitializeComponent();

ScreenNumber = scrn;

}

private void Form1_Load(object sender, EventArgs e)

{

this.Name = "ScreenSaver";

//窗体运行后无边界

this.FormBorderStyle = FormBorderStyle.None;

//程序运行后不显示在任务栏上

this.ShowInTaskbar = false;

//窗体运行后,最大化,充满整个屏幕

this.WindowState = FormWindowState.Maximized;

this.Bounds = Screen.AllScreens[ScreenNumber].Bounds;

//隐藏光标

Cursor.Hide();

//最前显示

this.TopMost = true;

}

//字幕屏幕保护处理逻辑

private void timer1_Tick(object sender, EventArgs e)

{

//得到计算机屏幕的工作区域

Rectangle ssWorkArea = Screen.GetWorkingArea(this);

label1.Location = new Point(ssWorkArea.Width - iDistance,

label1.Location.Y);

//显示标签

label1.Visible = true;

// 增加2个象素点,你可以通过修改speed的值来改变标签的移动速度

iDistance += speed;

// 如果标签已经走出屏幕,则把标签的位置重定位到屏幕的右边

if (label1.Location.X <= -(label1.Width))

{

//Reset the distance to 0.

iDistance = 0;

//判断标签的位置是否在顶部,如果在,则重定位到中部

if (label1.Location.Y == 0)

label1.Location = new Point(label1.Location.X, (ssWorkArea.Height / 2));

//判断标签的位置是否在中部,如果在,则重定位到底部

else if (label1.Location.Y == ssWorkArea.Height / 2)

label1.Location = new Point(label1.Location.X, ssWorkArea.Height - label1.Height);

//重定位到顶部

else

label1.Location = new Point(label1.Location.X, 0);

}

}

//处理鼠标事件

private void Form1_MouseDown(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();

timer1.Enabled = false;

Application.Exit();

};

}

//处理键盘事件

private void Form1_KeyDown(object sender, KeyEventArgs e)

{

Cursor.Show();

timer1.Enabled = false;

Application.Exit();

}

//获取鼠标的位置

private void Form1_MouseHover(object sender, EventArgs e)

{

ixStart = Cursor.Position.X;

iyStart = Cursor.Position.Y;

}

}

}

可以在timer事件中加以修改,比如每次更换文字的颜色、显示内容、文字旋转等等.由您去发挥了.

下篇文章讲介绍如何设置屏保参数。

其他人在看啥

    《自己做屏保2》的评论 (共 0 条)

    • Guest::piaoliang