C# 畫矩形圖(更新循環流動)

fff基於WINCE 6.0 .Net CompactFramework開發一個流體控件,主要目的就是讓程序自動生成矩形圖,定時幾十毫秒更新一次就可看到圖片要循環流動,在工業控制方面可動態顯示某個設備在運行中。

如果是.NET GDI+ 不需要這麼複雜,只需引用 Bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);就可實現右至左,上到下,下到上的流動效果!!

主要代碼如下:

 

        //流動速度
        private int speed = 10;
        //矩形塊大小
        private int blockSize = 20;
        //矩形塊間的縫隙大小
        private int chinkSize = 5;
        //步伐大小
        private int stepSize = 1;
        //背景顏色
        private Color backColor = Color.White;
        //前景顏色
        private Color foreColor = Color.Blue;
        //記錄偏移位置
        private int offset = 0;
        #region 左到右流動
        /// <summary>
        /// 左到右流動-完成2019-09-17
        /// </summary>
        public void LeftGoToRight() {
            Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
            Graphics ghp = Graphics.FromImage(map);
            ghp.DrawImage(map, 0, 0);
            ghp.Clear(this.backColor);
            int count = this.Width / (chinkSize + blockSize);
            //餘數
            if ((this.Width % (chinkSize + blockSize)) > 0) {
                count++;
            }
            //壓入第一個
            if (offset > 0) {
                if ((offset - chinkSize) > 0) {
                    if (offset <= (chinkSize + blockSize)) {
                        Rectangle first = new Rectangle(0, 0, offset - chinkSize, this.Height);
                        SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
                        ghp.FillRectangle(firstBlueBrush, first);
                        firstBlueBrush.Dispose();
                    } else {
                        offset = stepSize;
                    }
                }
            }
            for (int index = 0; index < count; index++) {
                int mv = index * (chinkSize + blockSize);
                Rectangle r = new Rectangle(offset + mv, 0, blockSize, this.Height);
                SolidBrush blueBrush = new SolidBrush(this.foreColor);
                ghp.FillRectangle(blueBrush, r);
                blueBrush.Dispose();
            }
            offset += stepSize;
            ghp.Dispose();
            //如果是.Net CompactFramework 下面方函數無法使用 
            //.NET 可用下面的方法翻轉或垂直鏡像
            //map.RotateFlip(RotateFlipType.Rotate90FlipX);
            this.pictureBox_main.Image = map;
        }
        #endregion
        #region 右到左流動
        /// <summary>
        /// 右到左流動
        /// </summary>
        public void RightGoToLeft() {
            Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
            Graphics ghp = Graphics.FromImage(map);
            ghp.DrawImage(map, 0, 0);
            ghp.Clear(this.backColor);
            int count = this.Width / (chinkSize + blockSize);
            //餘數
            if ((this.Width % (chinkSize + blockSize)) > 0) {
                count++;
            }
            //壓入第一個
            if (offset > 0) {
                if ((offset - chinkSize) > 0) {
                    if (offset <= (chinkSize + blockSize)) {
                        Rectangle first = new Rectangle((this.Width + chinkSize) - offset, 0, offset - chinkSize, this.Height);
                        SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
                        ghp.FillRectangle(firstBlueBrush, first);
                        firstBlueBrush.Dispose();
                    } else {
                        offset = stepSize;
                    }
                }
            }

            for (int index = 0; index < count; index++) {
                int mv = index * (chinkSize + blockSize);
                int x = mv == 0 ? this.Width - blockSize : this.Width - mv - blockSize;
                Rectangle r = new Rectangle(x - offset, 0, blockSize, this.Height);
                SolidBrush blueBrush = new SolidBrush(this.foreColor);
                ghp.FillRectangle(blueBrush, r);
                blueBrush.Dispose();
            }
            offset += stepSize;
            ghp.Dispose();
            this.pictureBox_main.Image = map;
        }
        #endregion
        #region 從頂部向下暴布流下
        /// <summary>
        /// 從頂部向下暴布流下
        /// </summary>
        public void TopGoToBottom() {
            Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
            Graphics ghp = Graphics.FromImage(map);
            ghp.DrawImage(map, 0, 0);
            ghp.Clear(this.backColor);
            int count = this.Height / (chinkSize + blockSize);
            //餘數
            if ((this.Height % (chinkSize + blockSize)) > 0) {
                count++;
            }
            //壓入第一個
            if (offset > 0) {
                if ((offset - chinkSize) > 0) {
                    if (offset <= (chinkSize + blockSize)) {
                        Rectangle first = new Rectangle(0, 0, this.Width, offset - chinkSize);
                        SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
                        ghp.FillRectangle(firstBlueBrush, first);
                        firstBlueBrush.Dispose();
                    } else {
                        offset = stepSize;
                    }
                }
            }
            for (int index = 0; index < count; index++) {
                int mv = index * (chinkSize + blockSize);
                Rectangle r = new Rectangle(0, offset + mv, this.Width, blockSize);
                SolidBrush blueBrush = new SolidBrush(this.foreColor);
                ghp.FillRectangle(blueBrush, r);
                blueBrush.Dispose();
            }
            offset += stepSize;
            ghp.Dispose();
            this.pictureBox_main.Image = map;
        }
        #endregion
         #region 從底部向上流動
        /// <summary>
        /// 從底部向上流動
        /// </summary>
        public void BottomGoToTop() {
            Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
            Graphics ghp = Graphics.FromImage(map);
            ghp.DrawImage(map, 0, 0);
            ghp.Clear(this.backColor);
            int count = this.Height / (chinkSize + blockSize);
            //餘數
            if ((this.Height % (chinkSize + blockSize)) > 0) {
                count++;
            }
            //壓入第一個
            if (offset > 0) {
                if ((offset - chinkSize) > 0) {
                    if (offset <= (chinkSize + blockSize)) {
                        Rectangle first = new Rectangle(0, (this.Height + chinkSize) - offset, this.Width, offset - chinkSize);
                        SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
                        ghp.FillRectangle(firstBlueBrush, first);
                        firstBlueBrush.Dispose();
                    } else {
                        offset = stepSize;
                    }
                }
            }
            for (int index = 0; index < count; index++) {
                int mv = index * (chinkSize + blockSize);
                int x = mv == 0 ? this.Height - blockSize : this.Height - mv - blockSize;
                Rectangle r = new Rectangle(0, x - offset, this.Width, blockSize);
                SolidBrush blueBrush = new SolidBrush(this.foreColor);
                ghp.FillRectangle(blueBrush, r);
                blueBrush.Dispose();
            }
            offset += stepSize;
            ghp.Dispose();
            this.pictureBox_main.Image = map;
        }
        #endregion

 

 

效果圖:要定時更新纔會走動!!!

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章