home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Edit List and Spin / Transform / Transform.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  4.9 KB  |  137 lines

  1. //----------------------------------------
  2. // Transform.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Imaging;      // For bitmap 
  8. using System.Windows.Forms;
  9.  
  10. class Transform: Form
  11. {
  12.      Matrix matrix = new Matrix();
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new Transform());
  17.      }
  18.      public Transform()
  19.      {
  20.           Text = "Transform";
  21.           ResizeRedraw = true;
  22.           BackColor = Color.White;
  23.           Size += Size;
  24.  
  25.                // Create modal dialog box.
  26.  
  27.           MatrixElements dlg = new MatrixElements();
  28.           dlg.Owner    = this;
  29.           dlg.Matrix   = matrix;
  30.           dlg.Changed += new EventHandler(MatrixDialogOnChanged);
  31.           dlg.Show();
  32.      }
  33.      protected override void OnPaint(PaintEventArgs pea)
  34.      {
  35.           Graphics grfx = pea.Graphics;
  36.  
  37.           DrawAxes(grfx);
  38.           grfx.Transform = matrix;
  39.           DrawHouse(grfx);
  40.      }
  41.       void DrawAxes(Graphics grfx)
  42.      {
  43.           Brush        brush  = Brushes.Black;
  44.           Pen          pen    = Pens.Black;
  45.           StringFormat strfmt = new StringFormat();
  46.  
  47.                // Horizontal axis
  48.  
  49.           strfmt.Alignment = StringAlignment.Center;
  50.  
  51.           for (int i = 1; i <= 10; i++)
  52.           {
  53.                grfx.DrawLine(pen, 100 * i, 0, 100 * i, 10);
  54.                grfx.DrawString((i * 100).ToString(), Font, brush, 
  55.                                100 * i, 10, strfmt);
  56.                grfx.DrawLine(pen, 100 * i, 10 + Font.Height, 
  57.                                   100 * i, ClientSize.Height);
  58.           }
  59.  
  60.                // Vertical axis
  61.  
  62.           strfmt.Alignment     = StringAlignment.Near;
  63.           strfmt.LineAlignment = StringAlignment.Center;
  64.  
  65.           for (int i = 1; i <= 10; i++)
  66.           {
  67.                grfx.DrawLine(pen, 0, 100 * i, 10, 100 * i);
  68.                grfx.DrawString((i * 100).ToString(), Font, brush, 
  69.                                10, 100 * i, strfmt);
  70.                float cxText = grfx.MeasureString(
  71.                                         (i * 100).ToString(), Font).Width;
  72.                grfx.DrawLine(pen, 10 + cxText,      100 * i, 
  73.                                   ClientSize.Width, 100 * i);
  74.           }
  75.      }
  76.      void DrawHouse(Graphics grfx)
  77.      {
  78.           Rectangle   rectFacade  =   new Rectangle( 0, 40, 100,  60);
  79.           Rectangle   rectDoor    =   new Rectangle(10, 50,  25,  50);
  80.           Rectangle[] rectWindows = { new Rectangle(50, 50,  10,  10),
  81.                                       new Rectangle(60, 50,  10,  10),
  82.                                       new Rectangle(70, 50,  10,  10),
  83.                                       new Rectangle(50, 60,  10,  10),
  84.                                       new Rectangle(60, 60,  10,  10),
  85.                                       new Rectangle(70, 60,  10,  10),
  86.                                       new Rectangle(15, 60,   5,   7),
  87.                                       new Rectangle(20, 60,   5,   7),
  88.                                       new Rectangle(25, 60,   5,   7) };
  89.           Rectangle   rectChimney =   new Rectangle(80,  5,  10,  35);
  90.           Point[]     ptRoof      = { new Point( 50,  0), 
  91.                                       new Point(  0, 40), 
  92.                                       new Point(100, 40) };
  93.  
  94.                // Create bitmap and brush for chimney.
  95.  
  96.           Bitmap bitmap = new Bitmap(8, 6); 
  97.  
  98.           byte[] bits = { 0, 0, 0, 0, 0, 0, 0, 0,
  99.                           1, 1, 1, 0, 1, 1, 1, 0, 
  100.                           1, 1, 1, 0, 1, 1, 1, 0, 
  101.                           0, 0, 0, 0, 0, 0, 0, 0, 
  102.                           1, 0, 1, 1, 1, 0, 1, 1,
  103.                           1, 0, 1, 1, 1, 0, 1, 1,
  104.  
  105.                           0, 0, 0, 0, 0, 0, 0, 0,
  106.                           0, 0, 1, 1, 1, 1, 1, 1 };
  107.  
  108.           for (int i = 0; i < 48; i++)
  109.                bitmap.SetPixel(i % 8, i / 8, 
  110.                          bits[i] == 1 ? Color.DarkGray: Color.LightGray);
  111.  
  112.           Brush brush = new TextureBrush(bitmap);
  113.  
  114.                // Draw entire house.
  115.  
  116.           grfx.FillRectangle(Brushes.LightGray, rectFacade);
  117.           grfx.DrawRectangle(Pens.Black,        rectFacade);
  118.  
  119.           grfx.FillRectangle(Brushes.DarkGray, rectDoor);
  120.           grfx.DrawRectangle(Pens.Black,       rectDoor);
  121.  
  122.           grfx.FillRectangles(Brushes.White, rectWindows);
  123.           grfx.DrawRectangles(Pens.Black,    rectWindows);
  124.  
  125.           grfx.FillRectangle(brush,      rectChimney);
  126.           grfx.DrawRectangle(Pens.Black, rectChimney);
  127.  
  128.           grfx.FillPolygon(Brushes.DarkGray, ptRoof);
  129.           grfx.DrawPolygon(Pens.Black,       ptRoof);
  130.      }
  131.      void MatrixDialogOnChanged(object obj, EventArgs ea)
  132.      {
  133.           MatrixElements dlg = (MatrixElements) obj;
  134.           matrix = dlg.Matrix;
  135.           Invalidate();
  136.      }
  137. }