第三篇 一个屏幕开满玫瑰花的程序

来源:互联网 发布:老子 知乎 编辑:程序博客网 时间:2024/04/28 05:02

思路:窗体全屏且透明,在窗体中随机绘制图片、

代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Rose.net3.Properties;using System.Windows.Forms.VisualStyles;namespace Rose.net3{    public partial class Form1 : Form    {        private static readonly int WIDTH = Screen.PrimaryScreen.WorkingArea.Width;        private static readonly int HEIGHT = Screen.PrimaryScreen.WorkingArea.Height;        private static int i = 0;        Timer timer = new Timer();        private Random random = new Random();        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            this.Width = WIDTH;            this.Height = HEIGHT;            this.TransparencyKey = this.BackColor;            this.StartPosition = FormStartPosition.CenterScreen;            this.Location=new Point(1,1);                      timer.Tick += new EventHandler(timer_Tick);            timer.Interval = 100;            timer.Start();        }        void timer_Tick(object sender, EventArgs e)        {            i++;            int x = random.Next(-10, WIDTH - 100);            int y = random.Next(-10, HEIGHT - 100);            Graphics gr;            gr = CreateGraphics();            gr.DrawImage(Resources.Rose,new Point(x,y));            if (i == 100)            {                timer.Stop();            }        }               private void Form1_KeyDown(object sender, KeyEventArgs e)        {            if (e.KeyCode== Keys.Escape)            {                this.Close();            }        }    }}