home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Tapping Into the Keyboard / TypeAway / TypeAway.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  3.8 KB  |  127 lines

  1. //---------------------------------------
  2. // TypeAway.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Text;
  7. using System.Windows.Forms;
  8. using Petzold.ProgrammingWindowsWithCSharp;
  9.  
  10. class TypeAway: Form
  11. {
  12.      public static void Main()
  13.      {
  14.           Application.Run(new TypeAway());
  15.      }
  16.  
  17.      protected Caret  caret;
  18.      protected string strText = "";
  19.      protected int    iInsert = 0;
  20.  
  21.      public TypeAway()
  22.      {
  23.           Text = "Type Away";
  24.           BackColor = SystemColors.Window;
  25.           ForeColor = SystemColors.WindowText;
  26.           
  27.           FontHeight = 24;
  28.  
  29.           caret = new Caret(this);
  30.           caret.Size = new Size(2, Font.Height);
  31.           caret.Position = new Point(0, 0);
  32.      }
  33.      protected override void OnKeyPress(KeyPressEventArgs kpea)
  34.      {
  35.           caret.Hide();
  36.           Graphics grfx = CreateGraphics();
  37.           grfx.FillRectangle(new SolidBrush(BackColor), 
  38.                              new RectangleF(Point.Empty, 
  39.                              grfx.MeasureString(strText, Font,
  40.                              Point.Empty, StringFormat.GenericTypographic)));
  41.  
  42.           switch(kpea.KeyChar)
  43.           {
  44.           case '\b':
  45.                if (iInsert > 0)
  46.                {
  47.                     strText = strText.Substring(0, iInsert - 1) +
  48.                                                 strText.Substring(iInsert);
  49.                     iInsert -= 1;
  50.                }
  51.                break;
  52.  
  53.           case '\r':
  54.           case '\n':
  55.                break;
  56.  
  57.           default:
  58.                if (iInsert == strText.Length)
  59.                     strText += kpea.KeyChar;
  60.                else
  61.                     strText = strText.Substring(0, iInsert) +
  62.                               kpea.KeyChar +
  63.                               strText.Substring(iInsert);
  64.                iInsert++;
  65.                break;
  66.           }
  67.           grfx.TextRenderingHint = TextRenderingHint.AntiAlias;
  68.           grfx.DrawString(strText, Font, new SolidBrush(ForeColor), 
  69.                           0, 0, StringFormat.GenericTypographic);
  70.           grfx.Dispose();
  71.  
  72.           PositionCaret();
  73.           caret.Show();
  74.      }
  75.      protected override void OnKeyDown(KeyEventArgs kea)
  76.      {
  77.           switch (kea.KeyData)
  78.           {
  79.           case Keys.Left:
  80.                if (iInsert > 0)
  81.                     iInsert--;
  82.                break;
  83.  
  84.           case Keys.Right:
  85.                if (iInsert < strText.Length)
  86.                     iInsert++;
  87.                break;
  88.  
  89.           case Keys.Home:
  90.                iInsert = 0;
  91.                break;
  92.  
  93.           case Keys.End:
  94.                iInsert = strText.Length;
  95.                break;
  96.  
  97.           case Keys.Delete:
  98.                if (iInsert < strText.Length)
  99.                {
  100.                     iInsert++;
  101.                     OnKeyPress(new KeyPressEventArgs('\b'));
  102.                }
  103.                break;
  104.  
  105.           default:
  106.                return;
  107.           }
  108.           PositionCaret();
  109.      }
  110.      protected void PositionCaret()
  111.      {
  112.           Graphics grfx = CreateGraphics();
  113.           string str = strText.Substring(0, iInsert);
  114.           StringFormat strfmt = StringFormat.GenericTypographic;
  115.           strfmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
  116.           SizeF sizef = grfx.MeasureString(str, Font, Point.Empty, strfmt);
  117.           caret.Position = new Point((int)sizef.Width, 0);
  118.           grfx.Dispose();
  119.      }
  120.      protected override void OnPaint(PaintEventArgs pea)
  121.      {
  122.           Graphics grfx = pea.Graphics;
  123.           grfx.TextRenderingHint = TextRenderingHint.AntiAlias;
  124.           grfx.DrawString(strText, Font, new SolidBrush(ForeColor), 
  125.                           0, 0, StringFormat.GenericTypographic);
  126.      }
  127. }