图片飞飞就是图片在屏幕上上下左右不停的动,就和屏保类似,下面是具体操作。
1、新建窗体应用程序
2、添加PictureBox控件,设定相关属性:
在父容器中停靠;
缩放模式为stretchImage(拉伸并填充整个窗体);
Image属性用来添加图片(需要图片资源),语法为:Image.FromFile(图片的完全路径)
例如:
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\0.jpg");
}
补充:Timer的使用
找到Timer组件,拖入Timer控件,设定属性:
启动Timer,Enable设为True;
Interval执行时间间隔(每隔多少毫秒执行一次);
添加运行代码,
例如:
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\0.jpg");
}
3、现在让图片每隔两秒执行一次:
结合PictureBox和Timer控件,Timer控件的Interval设为2000,实现图片切换:
int index = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\" + index + ".jpg");
}
private void timer1_Tick(object sender, EventArgs e)
{
index = index +1;
if (index > 4)
{
index = 0;
}
pictureBox1.Image = Image.FromFile(@"F:\练习\我的第一个应用程序\图片飞飞0808\bin\Debug\pp\" + index + ".jpg");
}
4、实现窗体可以移动
Point类型,使用Location属性可以为窗体设定位置,this.Location=new Point(x,y);
考虑窗体飞到外面去了,需要让它弹回来,得到屏幕的范围:Screen.GetWorkingArea(new Point(0,0));
返回一个Rectangle类型的变量,可以点出width和height得到屏幕的宽高;
int xSpeed = 2;
int ySpeed = 3;
private void timer2_Tick(object sender, EventArgs e)
{
Rectangle screenRectangle = Screen.GetWorkingArea(new Point(0, 0));
if (this.Location.X > screenRectangle.Width - this.Width)
{
xSpeed = -xSpeed;
}
if (this.Location.X < 0)
{
xSpeed = -xSpeed;
}
if (this.Location.Y > screenRectangle.Height - this.Height || this.Location.Y < 0)
{
ySpeed = -ySpeed;
}
this.Location = new Point(this.Location.X + xSpeed, this.Location.Y+ySpeed);
}
不想窗体有最大化、关闭等,和鼠标可拖动的功能,选中窗体,属性中FromBorderStyle设为None;
那么现在程序关不了,此时需要用到系统托盘:
添加一个系统托盘,拖入ContextMenuScript控件,添加菜单项和事件,Application.Exit()表示结束应用程序
拖入一个NotifyIncon控件,设定图标,并和菜单控件绑定;
在任务栏中去掉,选择From窗体,属性中ShowInTaskbar设为False;
去掉窗体边框---->设定窗体在前面---->不在任务栏显示---->调整时间 实现了优化;
补充:
this.close();//结束一个窗体
Application.Exit();//结束整个窗体
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://net.itheima.com/