首页 > 代码库 > C#全屏随机位置显示图片的小程序

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

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

  • 需要添加的控件:Timer

技术分享

 

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

 技术分享

技术分享

技术分享

 

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

技术分享

  • ESC键退出程序:

在Form1.Designer.cs中增加

this.KeyDown += Form1_KeyDown;

  • 代码如下:
     1         Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); 2  3         public Form1() 4         { 5             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 6             this.BackgroundImage = GetNoCursor(); 7             InitializeComponent(); 8         } 9 10         private void Form1_Load(object sender, EventArgs e)11         {12             timer1.Interval = 500;13             if (MessageBox.Show("消息", "标题", MessageBoxButtons.YesNo) == DialogResult.Yes)14             {15                 timer1.Enabled = true;16             }17             else18             {19                 this.Close();20             }21         }22 23         private void Form1_KeyDown(object sender, KeyEventArgs e)24         {25             if (e.KeyData =http://www.mamicode.com/= Keys.Escape)26             {27                 timer1.Enabled = false;28                 MessageBox.Show("消息", "标题", MessageBoxButtons.OK);29                 this.Close();30             }31         }32 33         private Bitmap GetNoCursor()34         {35             Bitmap Source = new Bitmap(bounds.Width, bounds.Height);    //根据屏幕大小创建Bitmap对象36             Graphics g = Graphics.FromImage(Source);37             g.CopyFromScreen(0, 0, 0, 0, Source.Size);  //获取没有鼠标的屏幕截图38             g.Dispose();    //释放资源39             return Source;40         }41 42         private void timer1_Tick(object sender, EventArgs e)43         {44             Image img = Resource1.Image1;//获取用于显示的资源文件45             if (img != null)46             {47                 Graphics g = this.CreateGraphics();48                 Random rd = new Random();49                 int picXPoint = rd.Next(0, bounds.Right - img.Width);50                 int picYPoint = rd.Next(0, bounds.Height - img.Height);51                 Point ulCorner = new Point(picXPoint, picYPoint);52                 g.DrawImageUnscaled(img, ulCorner);53             }54             else55             {56                 timer1.Enabled = false;57                 MessageBox.Show("没有图片,感谢使用");58                 this.Close();59             }60         }

     

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