home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / The Timer and Time / SevenSegmentClock / SevenSegmentDisplay.cs < prev   
Encoding:
Text File  |  2001-01-15  |  4.8 KB  |  126 lines

  1. //--------------------------------------------------
  2. // SevenSegmentDisplay.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. namespace Petzold.ProgrammingWindowsWithCSharp
  9. {
  10. class SevenSegmentDisplay
  11. {
  12.      Graphics grfx;
  13.  
  14.           // Indicates what segments are illuminated for all 10 digits
  15.  
  16.      static byte[,] bySegment = {{1, 1, 1, 0, 1, 1, 1},       // 0
  17.                                  {0, 0, 1, 0, 0, 1, 0},       // 1
  18.                                  {1, 0, 1, 1, 1, 0, 1},       // 2
  19.                                  {1, 0, 1, 1, 0, 1, 1},       // 3
  20.                                  {0, 1, 1, 1, 0, 1, 0},       // 4
  21.                                  {1, 1, 0, 1, 0, 1, 1},       // 5
  22.                                  {1, 1, 0, 1, 1, 1, 1},       // 6
  23.                                  {1, 0, 1, 0, 0, 1, 0},       // 7
  24.                                  {1, 1, 1, 1, 1, 1, 1},       // 8
  25.                                  {1, 1, 1, 1, 0, 1, 1}};      // 9
  26.  
  27.           // Points that define each of the seven segments
  28.  
  29.      readonly Point[][] apt = new Point[7][];
  30.  
  31.      public SevenSegmentDisplay(Graphics grfx)
  32.      {
  33.           this.grfx = grfx;
  34.  
  35.                // Initialize jagged Point array.
  36.  
  37.           apt[0] = new Point[] {new Point( 3,  2), new Point(39,  2),
  38.                                 new Point(31, 10), new Point(11, 10)};
  39.  
  40.           apt[1] = new Point[] {new Point( 2,  3), new Point(10, 11),
  41.                                 new Point(10, 31), new Point( 2, 35)};
  42.  
  43.           apt[2] = new Point[] {new Point(40,  3), new Point(40, 35),
  44.                                 new Point(32, 31), new Point(32, 11)};
  45.  
  46.           apt[3] = new Point[] {new Point( 3, 36), new Point(11, 32),
  47.                                 new Point(31, 32), new Point(39, 36),
  48.                                 new Point(31, 40), new Point(11, 40)};
  49.  
  50.           apt[4] = new Point[] {new Point( 2, 37), new Point(10, 41),
  51.                                 new Point(10, 61), new Point( 2, 69)};
  52.  
  53.           apt[5] = new Point[] {new Point(40, 37), new Point(40, 69),
  54.                                 new Point(32, 61), new Point(32, 41)};
  55.  
  56.           apt[6] = new Point[] {new Point(11, 62), new Point(31, 62),
  57.                                 new Point(39, 70), new Point( 3, 70)};
  58.      }
  59.      public SizeF MeasureString(string str, Font font)
  60.      {
  61.           SizeF sizef = new SizeF(0, grfx.DpiX * font.SizeInPoints / 72);
  62.  
  63.           for (int i = 0; i < str.Length; i++)
  64.           {
  65.                if (Char.IsDigit(str[i]))
  66.                     sizef.Width += 42 * grfx.DpiX * font.SizeInPoints
  67.                                                             / 72 / 72;
  68.                else if (str[i] == ':')
  69.                     sizef.Width += 12 * grfx.DpiX * font.SizeInPoints
  70.                                                             / 72 / 72;
  71.           }
  72.           return sizef;
  73.      }
  74.      public void DrawString(string str, Font font,
  75.                             Brush brush, float x, float y)
  76.      {
  77.           for (int i = 0; i < str.Length; i++)
  78.           {
  79.                if (Char.IsDigit(str[i]))
  80.                     x = Number(str[i] - '0', font, brush, x, y);
  81.  
  82.                else if (str[i] == ':')
  83.                     x = Colon(font, brush, x, y);
  84.           }
  85.      }
  86.      float Number(int num, Font font,
  87.                   Brush brush, float x, float y)
  88.      {
  89.           for (int i = 0; i < apt.Length; i++)
  90.                if (bySegment[num, i] == 1)
  91.                     Fill(apt[i], font, brush, x, y);
  92.  
  93.           return x + 42 * grfx.DpiX * font.SizeInPoints / 72 / 72;
  94.      }
  95.      float Colon(Font font, Brush brush, float x, float y)
  96.      {
  97.           Point[][] apt = new Point[2][];
  98.  
  99.           apt[0] = new Point[] {new Point( 2, 21), new Point( 6, 17),
  100.                                 new Point(10, 21), new Point( 6, 25)};
  101.  
  102.           apt[1] = new Point[] {new Point( 2, 51), new Point( 6, 47),
  103.                                 new Point(10, 51), new Point( 6, 55)};
  104.  
  105.           for (int i = 0; i < apt.Length; i++)
  106.                Fill(apt[i], font, brush, x, y);
  107.  
  108.           return x + 12 * grfx.DpiX * font.SizeInPoints / 72 / 72;
  109.      }
  110.      void Fill(Point[] apt, Font font, Brush brush, float x, float y)
  111.      {
  112.           PointF[] aptf = new PointF[apt.Length];
  113.  
  114.           for (int i = 0; i < apt.Length; i++)
  115.           {
  116.                aptf[i].X = x + apt[i].X * grfx.DpiX *
  117.                                           font.SizeInPoints / 72 / 72;
  118.                aptf[i].Y = y + apt[i].Y * grfx.DpiY *
  119.                                           font.SizeInPoints / 72 / 72;
  120.           }
  121.           grfx.FillPolygon(brush, aptf);
  122.      }
  123. }
  124. }
  125.  
  126.