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 else
18 {
19 this.Close();
20 }
21 }
22
23 private void Form1_KeyDown(object sender, KeyEventArgs e)
24 {
25 if (e.KeyData == 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 else
55 {
56 timer1.Enabled = false;
57 MessageBox.Show("沒有圖片,感謝使用");
58 this.Close();
59 }
60 }
?
轉載于:https://www.cnblogs.com/Idus/p/5886817.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C#全屏随机位置显示图片的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。