1. 程式人生 > >第三篇 一個螢幕開滿玫瑰花的程式

第三篇 一個螢幕開滿玫瑰花的程式

思路:窗體全屏且透明,在窗體中隨機繪製圖片、

程式碼:

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();
            }
        }
    }
}