C# winform 實現圖片輪播

做爲一個C#winform剛剛入門的我  以爲這多是初學者都會遇到的  我本身也以爲很神奇this

 

首先 窗體裏有一個Button按鈕,和一個pictureBox 圖片框線程

 

定義一個線程方法orm

 


        /// <summary>
        /// 改變圖片
        /// </summary>
        /// <param name="img">圖片</param>
        /// <param name="millisecondsTimeOut">切換圖片間隔時間</param>
        private void ChangeImage(Image img, int millisecondsTimeOut)
        {
            this.Invoke(new Action(() =>
            {
                pictureBox1.Image = img;
            })
                );
            Thread.Sleep(millisecondsTimeOut);
        }事件

 

 

在button1 添加單擊事件以下代碼圖片

  Thread th;
        private void button1_Click(object sender, EventArgs e)
        {
            //timer1.Enabled = true;
            th = new Thread
                (
                    delegate() 
                    {
                        // 3就是要循環輪數了
                        for (int i = 0; i < 3; i++)
                        {           
                           //調用方法io

                            ChangeImage(WindowsFormsApplication2.Properties.Resources.img__5_, 1000);
                            ChangeImage(WindowsFormsApplication2.Properties.Resources.img__6_, 1000);
                            ChangeImage(WindowsFormsApplication2.Properties.Resources.img__7_, 1000);
                        }
                    }
                );
            th.IsBackground = true;
            th.Start();
        }入門

 

就能夠完成了,form

雖然對於大神們來講不值得一提,object

但對於像咱們這些初學者 在作一些簡單項目的時候 仍是比較有用,循環

你們能夠經過這個 去延伸  用處不單單是圖片輪播

相關文章
相關標籤/搜索