欢迎光临
我们一直在努力

使用C#编写LED样式时钟控件-.NET教程,C#语言

建站超值云服务器,限时71元/月

//————————–(如转载,请保留版权信息)————————-//
//   sevensegmentclockstyle.cs 朱继山 a3news(at)hotmail.com  –//
// —————————– http://www.brawdraw.com ———————-//
// ——————– 未经书面许可,请勿用于商业用途 ———————//

using system;

namespace brawdraw.com.photoframe.net.publicfunctions.clock
{
 /// <summary>
 /// clocks style.时钟的样式定义
 /// </summary>
 public enum sevensegmentclockstyle
 {
  dateonly, // 只显示日期
  timeonly, // 只显示时间
  dateandtime //显示日期和时间
 }
}

//————————–(如转载,请保留版权信息)————————-//
//    sevensegmentclock.cs  朱继山 a3news(at)hotmail.com  ——-//
// —————————– http://www.brawdraw.com ———————-//
// ——————– 未经书面许可,请勿用于商业用途 ———————//
using system;
using system.drawing;
using system.drawing.drawing2d;
using system.globalization;
using system.windows.forms;
using brawdraw.com.photoframe.net.publicfunctions;
using system.componentmodel;

namespace brawdraw.com.photoframe.net.publicfunctions.clock
{
//这是控件的关键代码
 public class sevensegmentclock : usercontrol
 {
  datetime _datetime;
//默认使用同时绘制日期和时间
  sevensegmentclockstyle _clockstyle = sevensegmentclockstyle.dateandtime;
  color _clockcolor = color.black;
//是否绘制阴影(即残影),以摸拟真似的led时钟
  bool _isdrawshadow = true;

  timer _timer    = null;
//是否自动更新时间
  bool _istimerenable = false;
  graphics g = null;
  bitmap m_bitmap = null;

  public bool isdrawshadow
  {
   get { return this._isdrawshadow; }
   set
   {
    this._isdrawshadow = value;
    this.invalidate();
   }
  }

  [browsable(false)]
  public system.windows.forms.timer timer
  {
   get { return this._timer; }
   set
   {
    this._timer = value;
    if(_timer != null)
    {
     _timer.tick    += new eventhandler(timerontick);
    }
   }
  }

  public bool istimerenable
  {
   get { return this._istimerenable; }
   set
   {
    if(value == true)
    {
     if(this._timer == null)
     {
      _timer = new timer();
      _timer.tick    += new eventhandler(timerontick);
      _timer.interval = 1000;
      _timer.enabled  = true;
     }
    }
    else
    {
     if(this._timer != null)
     {
      _timer.enabled  = false;
     }
    }
    this._istimerenable = value;
   }
  }

  public void start()
  {
   this.istimerenable = true;
   this.refresh();
  }

  public void stop()
  {
   this.istimerenable = false;
  }

  public system.datetime datetime
  {
   get { return this._datetime; }
   set { this._datetime = value; }
  }

//led文字的颜色
  public system.drawing.color clockcolor
  {
   get { return this._clockcolor; }
   set
   {
    this._clockcolor = value;
    this.invalidate();
   }
  }

  public sevensegmentclockstyle sevensegmentclockstyle
  {
   get { return this._clockstyle; }
   set
   {
    this._clockstyle = value;
    this.invalidate();
   }
  }

  public sevensegmentclock()
  {
   text = “seven-segment clock”;
//使用双缓冲,支持透明绘制
   setstyle(controlstyles.userpaint | controlstyles.doublebuffer | controlstyles.allpaintinginwmpaint
    | controlstyles.resizeredraw | controlstyles.supportstransparentbackcolor, true);
   this.updatestyles();
   init();
   _datetime = datetime.now;
  }

//初始化
  private void init()
  {
   m_bitmap = new bitmap(this.width, this.height);

   g = graphics.fromimage(m_bitmap);
   g.compositingquality = compositingquality.highquality;
   g.textrenderinghint = system.drawing.text.textrenderinghint.antialiasgridfit;
   
   //g.interpolationmode = interpolationmode.highqualitybicubic;
   g.smoothingmode = smoothingmode.highquality;
   //g.textrenderinghint = system.drawing.text.textrenderinghint.antialiasgridfit;
  }

  void timerontick(object obj, eventargs ea)
  {
   datetime dtnow = datetime.now;
   dtnow = new datetime(dtnow.year, dtnow.month, dtnow.day, dtnow.hour, dtnow.minute, dtnow.second);
   if (dtnow != _datetime)
   {
    _datetime = dtnow;
    invalidate();
   }
  }

  protected override void onpaint(painteventargs e)
  {
   m_bitmap = drawclock();
   graphics gg = e.graphics;
   gg.compositingquality = compositingquality.highquality;
   gg.drawimageunscaled(m_bitmap, 0, 0);
   //g.dispose();
  }

  public bitmap drawclock()
  {
   return this.drawclock(this.clientrectangle);
  }

  private void sevensegmentclock_resize(object sender, system.eventargs e)
  {
   init();
   this.refresh();
  }

  private void initializecomponent()
  {
   //
   // sevensegmentclock
   //
   this.name = “sevensegmentclock”;
   this.size = new system.drawing.size(448, 64);
   this.resize += new system.eventhandler(this.sevensegmentclock_resize);
  }

  int _clockstringwidth;
  int _clockstringheight;
  public int clockstringwidth
  {
   get
   {
    return _clockstringwidth;
   }
  }
  
  public int clockstringheight
  {
   get
   {
    return _clockstringheight;
   }
  }

//绘制时钟
  public bitmap drawclock(rectangle destrect)
  {
   m_bitmap = new bitmap(destrect.width, destrect.height);
   //m_bitmap = new bitmap(destrect.x + this.width, destrect.y + this.height);
   graphics grfx = graphics.fromimage(m_bitmap);
//设置绘图面板的绘制质量等
   grfx.compositingquality = compositingquality.highquality;
   grfx.textrenderinghint = system.drawing.text.textrenderinghint.antialiasgridfit;
   grfx.smoothingmode = smoothingmode.highquality;

   sevensegmentdisplay ssd = new sevensegmentdisplay(grfx);
   ssd.isdrawshadow = this._isdrawshadow;
   graphicsstate gs = grfx.save();
   grfx.translatetransform(destrect.x, destrect.y);
   string strtime = string.empty;
   if(this._clockstyle == sevensegmentclockstyle.timeonly)
   {
    strtime = _datetime.tostring(“t”, datetimeformatinfo.invariantinfo);
   }
   else if(this._clockstyle == sevensegmentclockstyle.dateonly)
   {
//设置日期格式
    strtime = _datetime.tostring(“yyyy-mm-dd”, datetimeformatinfo.invariantinfo);
   }
   else
   {
    strtime = _datetime.tostring(“yyyy-mm-dd”, datetimeformatinfo.invariantinfo) + ” ” + _datetime.tostring(“t”, datetimeformatinfo.invariantinfo);
   }

   sizef  sizef   = ssd.measurestring(strtime, font);
   float  fscale  = math.min(destrect.width  / sizef.width, destrect.height / sizef.height);
   font   font    = new font(font.fontfamily, fscale * font.sizeinpoints);

   sizef = ssd.measurestring(strtime, font);
   _clockstringwidth = (int)sizef.width;
   _clockstringheight = (int)sizef.height;

   ssd.drawstring(strtime, font, new solidbrush(this._clockcolor),
    (destrect.width  – sizef.width) / 2,
    (destrect.height – sizef.height) / 2);
   grfx.restore(gs);
   return m_bitmap;
  }
 }
}

//————————–(如转载,请保留版权信息)————————-//
//   sevensegmentdisplay.cs 2001 by charles petzold                        //
//————————改编:朱继山 a3news(at)hotmail.com  ———–//
using system;
using system.drawing;
using system.windows.forms;

namespace brawdraw.com.photoframe.net.publicfunctions.clock
{
//字符绘制的算法
 class sevensegmentdisplay
 {
  graphics grfx;
  brush _brush = brushes.black;
  bool _isdrawshadow = true;
  color _shadowcolor = color.fromargb(60, color.white);
  brush _shadowbrush = null;
  // indicates what segments are illuminated for all 10 digits

  static byte[,] bysegment = {
         {1, 1, 1, 0, 1, 1, 1},       // 0
         {0, 0, 1, 0, 0, 1, 0},       // 1
         {1, 0, 1, 1, 1, 0, 1},       // 2
         {1, 0, 1, 1, 0, 1, 1},       // 3
         {0, 1, 1, 1, 0, 1, 0},       // 4
         {1, 1, 0, 1, 0, 1, 1},       // 5
         {1, 1, 0, 1, 1, 1, 1},       // 6
         {1, 0, 1, 0, 0, 1, 0},       // 7
         {1, 1, 1, 1, 1, 1, 1},       // 8
         {1, 1, 1, 1, 0, 1, 1}        // 9
           };
  // points that define each of the seven segments
  readonly point[][] apt = new point[7][];

  public bool isdrawshadow
  {
   get { return this._isdrawshadow; }
   set { this._isdrawshadow = value; }
  }

  public sevensegmentdisplay(graphics grfx)
  {
   this.grfx = grfx;
   // initialize jagged point array.
   apt[0] = new point[] {
          new point( 3,  2), new point(39,  2),
          new point(31, 10), new point(11, 10)
         };

   apt[1] = new point[] {
          new point( 2,  3), new point(10, 11),
          new point(10, 31), new point( 2, 35)
         };

   apt[2] = new point[] {
          new point(40,  3), new point(40, 35),
          new point(32, 31), new point(32, 11)
         };

   apt[3] = new point[] {
          new point( 3, 36), new point(11, 32),
          new point(31, 32), new point(39, 36),
          new point(31, 40), new point(11, 40)
         };

   apt[4] = new point[] {
          new point( 2, 37), new point(10, 41),
          new point(10, 61), new point( 2, 69)
         };

   apt[5] = new point[] {
          new point(40, 37), new point(40, 69),
          new point(32, 61), new point(32, 41)
         };

   apt[6] = new point[] {
          new point(11, 62), new point(31, 62),
          new point(39, 70), new point( 3, 70)
         };
  }

  public sizef measurestring(string str, font font)
  {
   sizef sizef = new sizef(0, grfx.dpix * font.sizeinpoints / 72);

   for (int i = 0; i < str.length; i++)
   {
    if (char.isdigit(str[i]))
    {
     sizef.width += 42 * grfx.dpix * font.sizeinpoints / 72 / 72;
    }
    else if (str[i] == -)
    {
     sizef.width += 42 * grfx.dpix * font.sizeinpoints / 72 / 72;
    }
    else if (str[i] == 🙂
    {
     sizef.width += 20 * grfx.dpix * font.sizeinpoints / 72 / 72;
    }
    else if (str[i] == )
    {
     sizef.width += 36 * grfx.dpix * font.sizeinpoints / 72 / 72;
    }
   }
   return sizef;
  }

  public void drawstring(string str, font font, brush brush, float x, float y)
  {
   this._brush = brush;
   this._shadowbrush = new solidbrush(color.fromargb(40, ((solidbrush)this._brush).color));

   for (int i = 0; i < str.length; i++)
   {
    if (char.isdigit(str[i]))
    {
     x = number(str[i] – 0, font, brush, x, y);
    }
    else if (str[i] == -)
    {
     x = minussign(font, brush, x, y);
    }
    else if (str[i] == 🙂
    {
     x = colon(font, brush, x, y);
    }
    else if (str[i] == )
    {
     x = drawspace(font, brush, x, y);
    }
   }
  }

  private float number(int num, font font, brush brush, float x, float y)
  {
   for (int i = 0; i < apt.length; i++)
   {
    if(_isdrawshadow)
    {
     fill(apt[i], font, _shadowbrush, x, y);
    }
    if (bysegment[num, i] == 1)
    {
     fill(apt[i], font, brush, x, y);
    }
   }
   return x + 42 * grfx.dpix * font.sizeinpoints / 72 / 72;
  }

  private float minussign(font font, brush brush, float x, float y)
  {
   fill(apt[3], font, brush, x, y);
   return x + 42 * grfx.dpix * font.sizeinpoints / 72 / 72;
  }

  private float drawspace(font font, brush brush, float x, float y)
  {
   return x + 36 * grfx.dpix * font.sizeinpoints / 72 / 72;
  }

  private float colon(font font, brush brush, float x, float y)
  {
   point[][] apt = new point[2][];

   apt[0] = new point[] {
          new point( 4, 12), new point( 16, 12),
          new point(16, 24), new point( 4, 24)
         };

   apt[1] = new point[] {
          new point( 4, 50), new point( 16, 50),
          new point(16, 62), new point( 4, 62)
         };

   for (int i = 0; i < apt.length; i++)
   {
    fill(apt[i], font, brush, x, y);
   }

   return x + 20 * grfx.dpix * font.sizeinpoints / 72 / 72;
  }

  private void fill(point[] apt, font font, brush brush, float x, float y)
  {
   pointf[] aptf = new pointf[apt.length];

   for (int i = 0; i < apt.length; i++)
   {
    aptf[i].x = x + apt[i].x * grfx.dpix * font.sizeinpoints / 72 / 72;
    aptf[i].y = y + apt[i].y * grfx.dpiy * font.sizeinpoints / 72 / 72;
   }

   grfx.fillpolygon(brush, aptf);
  }
 }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用C#编写LED样式时钟控件-.NET教程,C#语言
分享到: 更多 (0)