Randomly change the background color of the control

When you do WPF in C #, you need to randomly adjust the background color of the control. The following is the relevant code to solve this problem:

DispatcherTimer timer = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
            //(2)
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timer.Tick += Timer_Tick;
            //(3)
            timer.Start();
        }

        Random ran = new Random();
        private void Timer_Tick(object sender, EventArgs e)
        {
            int a= ran.Next(0, 256);
            int r = ran.Next(0, 256);
            int g = ran.Next(0, 256);
            int b = ran.Next(0, 256);
            Color c = Color.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
            this.Background = new SolidColorBrush(c);
        }

  Random numbers are generated by clock changes, thereby changing the values ​​of r, g, and b to change the background color.

Guess you like

Origin www.cnblogs.com/lcy-4/p/12707508.html