コントロールの背景色をランダムに変更する

C#でWPFを実行する場合、コントロールの背景色をランダムに調整する必要があります。以下は、この問題を解決するための関連コードです。

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); 
            色c = Color.FromArgb((byte)a、(byte)r、(byte)g、(byte)b);
            this.Background = new SolidColorBrush(c); 
        }

  乱数はクロックの変更によって生成されるため、r、g、bの値を変更して背景色を変更します。

おすすめ

転載: www.cnblogs.com/lcy-4/p/12707508.html