您的位置:首页 > 移动开发 > 微信开发

C#全屏随机位置显示图片的小程序

2016-09-19 21:40 323 查看
想法:将屏幕截图作为程序背景图,在之上弹出提示窗口,选择确定后进行定时图片随机位置显示。(支持ESC键退出)

需要添加的控件:Timer



需要修改的Form1属性为下图红色区域:







资源文件的添加:添加->新建项->资源文件



ESC键退出程序:

在Form1.Designer.cs中增加

this.KeyDown += Form1_KeyDown;

代码如下:

Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));

public Form1()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackgroundImage = GetNoCursor();
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 500;
if (MessageBox.Show("消息", "标题", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
timer1.Enabled = true;
}
else
{
this.Close();
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
{
timer1.Enabled = false;
MessageBox.Show("消息", "标题", MessageBoxButtons.OK);
this.Close();
}
}

private Bitmap GetNoCursor()
{
Bitmap Source = new Bitmap(bounds.Width, bounds.Height);    //根据屏幕大小创建Bitmap对象
Graphics g = Graphics.FromImage(Source);
g.CopyFromScreen(0, 0, 0, 0, Source.Size);  //获取没有鼠标的屏幕截图
g.Dispose();    //释放资源
return Source;
}

private void timer1_Tick(object sender, EventArgs e)
{
Image img = Resource1.Image1;//获取用于显示的资源文件
if (img != null)
{
Graphics g = this.CreateGraphics();
Random rd = new Random();
int picXPoint = rd.Next(0, bounds.Right - img.Width);
int picYPoint = rd.Next(0, bounds.Height - img.Height);
Point ulCorner = new Point(picXPoint, picYPoint);
g.DrawImageUnscaled(img, ulCorner);
}
else
{
timer1.Enabled = false;
MessageBox.Show("没有图片,感谢使用");
this.Close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: