程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#全屏隨機位置顯示圖片的小程序,

C#全屏隨機位置顯示圖片的小程序,

編輯:C#入門知識

C#全屏隨機位置顯示圖片的小程序,


想法:將屏幕截圖作為程序背景圖,在之上彈出提示窗口,選擇確定後進行定時圖片隨機位置顯示。(支持ESC鍵退出)

  • 需要添加的控件:Timer

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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved