首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> 心得技巧
使用C#编写LED样式时钟控件-.NET教程,C#语言
作者:网友供稿 点击:137
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
//--------------------------(如转载,请保留版权信息)-------------------------//
//   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);
  }
 }
}


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java 调用存储过程-JSP教程,Java技巧及代码
·JSP中文乱码问题的解决.-JSP教程,Java技巧及代码
·Java的网络编程:用Java实现Web服务器-JSP教程,Java技巧及代码
·asp.net连接Access数据库-.NET教程,Asp.Net开发
·VC++.NET 2005 几个比较难缠的问题及其解决方法-.NET教程,.NET Framework
·DropDownList 控件 DataTextField 和 DataValueField 分开绑定-ASP教程,数据库相关
·.Net PetShop 4.0的分布式数据库设计-.NET教程,.NET Framework
·用Asp.net实现新闻分页-.NET教程,Asp.Net开发
·在.NET 2.0 中发送Email-.NET教程,E-mail专题
·逆变桥功率开关管门极关断箝位电路
最新文章
·经典收藏:网页页面常用的特殊符号_心得技巧教程
·用html和css写出漂亮正规的blog_心得技巧教程
·用photoshop优化美眉照片的眼部特写_photoshop教程
·网站页面的均衡规划与选择_心得技巧教程
·关闭远程差分压缩给windows vista拷贝提速_windows vista
·谈google广告的单价与点击-smart_网赚技巧
·用photoshop消除照片中的杂色条纹_photoshop教程
·一个网友观点:两年以来使用div排版的经验_心得技巧教程
·开始使用google adsense_google推广
·google官方讲解noindex、nofollow、noarchive及nosnippet等meta标签用法_google推广
相关主题
  • 使用C#编写扩展存储过程-.NET教程,C#语言
  • 使用C#编写的一个定时关机程序-.NET教程,C#语言
  • 使用C#编写一个计时器-.NET教程,C#语言
  • 使用C#编写Ice应用程序-.NET教程,C#语言
  • 使用C#编程将websphere MQ 5.3 windows客户端消息发送到linux服务器端-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭