下午好呀! 本人想讨教大家一个.NET问题,关于红路灯倒计时的数字图案显示问题

.Net技术 码拜 9年前 (2016-03-09) 987次浏览
下午好呀! 本人想讨教大家一个.NET问题,关于红路灯倒计时的数字图案显示问题
怎么样使用c#窗体程序实现图片上红路灯倒计时这个数字的图案(不是数字而是数字的图案)呀!
解决方案

60

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace WindowsFormsApplication3
{
    public class LEDScreen : Control
    {
        private int _DotWidth = 10;
        [Description("点阵的大小")]
        public int DotWidth {
            get { return _DotWidth; }
            set {
                if (value < 0) throw new ArgumentException("点宽度必须大于0");
                _DotWidth = value;
                this.Width = 0;
                this.Invalidate();
            }
        }
        private Size _ScreenSize = new Size(10, 10);
        [Description("屏幕分辨率")]
        public Size ScreenSize {
            get { return _ScreenSize; }
            set {
                if (value.Width < 0 || value.Height < 0)
                    throw new ArgumentException("无效的屏幕尺寸");
                _ScreenSize = value;
                _Swith = new bool[value.Width, value.Height];
                this.Width = 0;     //触发 SetBoundsCore 函数
                this.Invalidate();
            }
        }
        private Color _LigthColor = Color.Red;
        [Description("亮灯的颜色")]
        public Color LigthColor {
            get { return _LigthColor; }
            set { 
                _LigthColor = value;
                this.Invalidate();
            }
        }
        private Color _GrayColor = Color.Gray;
        [Description("灭灯的颜色")]
        public Color GrayColor {
            get { return _GrayColor; }
            set { 
                _GrayColor = value;
                this.Invalidate();
            }
        }
        private bool[,] _Swith = new bool[10, 10];
        public bool[,] Swith {
            get { return _Swith; }
        }
        public LEDScreen() {
            this.BackColor = Color.Black;
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
        protected override void OnPaint(PaintEventArgs e) {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            using (SolidBrush sb = new SolidBrush(this._GrayColor)) {
                for (int x = 0; x < this._ScreenSize.Width; x++) {
                    for (int y = 0; y < this._ScreenSize.Height; y++) {
                        sb.Color = this._Swith[x, y] ? this._LigthColor : this._GrayColor;
                        g.FillEllipse(sb, x * this._DotWidth, y * this._DotWidth, this._DotWidth, this._DotWidth);
                    }
                }
            }
            base.OnPaint(e);
        }
        //代码控制控件的大小 
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
            width = this._ScreenSize.Width * this._DotWidth + 1;
            height = this._ScreenSize.Height * this._DotWidth + 1;
            base.SetBoundsCore(x, y, width, height, specified);
        }
    }
}

下午好呀! 本人想讨教大家一个.NET问题,关于红路灯倒计时的数字图案显示问题
闲来无事做 打个酱油 做了一下你这个东西 本人参考把实现方式太多了


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明下午好呀! 本人想讨教大家一个.NET问题,关于红路灯倒计时的数字图案显示问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)