想法:将屏幕截图作为程序背景图,在之上弹出提示窗口,选择确定后进行定时图片随机位置显示。(支持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 = ;
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(, , , , 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(, bounds.Right - img.Width);
int picYPoint = rd.Next(, bounds.Height - img.Height);
Point ulCorner = new Point(picXPoint, picYPoint);
g.DrawImageUnscaled(img, ulCorner);
}
else
{
timer1.Enabled = false;
MessageBox.Show("没有图片,感谢使用");
this.Close();
}
}