国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

使用C#編寫LED樣式時鐘控件

2024-07-21 02:29:38
字體:
來源:轉載
供稿:網友
 

運行效果:http://blog.csdn.net/images/blog_csdn_net/johnsuna/clockcontrol.jpg 右下角圖片

//--------------------------(如轉載,請保留版權信息)-------------------------//
//   sevensegmentclockstyle.cs 朱繼山 a3news(at)hotmail.com  --//
// ----------------------------- http://www.brawdraw.com ----------------------//
// -------------------- 未經書面許可,請勿用于商業用途 ---------------------//

using system;

namespace brawdraw.com.photoframe.net.publicfunctions.clock
{
 /// <summary>
 /// clock's 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);
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭水| 丰顺县| 买车| 龙井市| 清流县| 太白县| 商洛市| 达尔| 吐鲁番市| 邢台市| 丰顺县| 武定县| 盘山县| 中宁县| 青浦区| 六盘水市| 资兴市| 遂昌县| 中山市| 麻江县| 陈巴尔虎旗| 瓦房店市| 平远县| 郴州市| 遂川县| 额尔古纳市| 昭觉县| 从化市| 阆中市| 德安县| 斗六市| 德钦县| 明水县| 积石山| 河西区| 大庆市| 霍州市| 怀来县| 通化县| 康平县| 宣化县|