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

 

 

效果图:要定时更新才会走动!!!

 

 

 

 

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值