//-------------------------------------------------
// TenCentimeterRuler.cs ?2001 by Charles Petzold
//-------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class TenCentimeterRuler: PrintableForm
{
     public new static void Main()
     {
          Application.Run(new TenCentimeterRuler());
     }
     public TenCentimeterRuler()
     {
          Text = "Ten-Centimeter Ruler";
     }
     protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Pen       pen   = new Pen(clr);
          Brush     brush = new SolidBrush(clr);
          const int xOffset = 10;
          const int yOffset = 10;

          grfx.DrawPolygon(pen, new PointF[] 
               { 
                    MMConv(grfx, new PointF(xOffset,       yOffset)),
                    MMConv(grfx, new PointF(xOffset + 100, yOffset)),
                    MMConv(grfx, new PointF(xOffset + 100, yOffset + 10)),
                    MMConv(grfx, new PointF(xOffset,       yOffset + 10))
               });

          StringFormat strfmt = new StringFormat();
          strfmt.Alignment = StringAlignment.Center;

          for (int i = 1; i < 100; i++)
          {
               if (i % 10 == 0)         // Centimeter markings
               {
                    grfx.DrawLine(pen, 
                         MMConv(grfx, new PointF(xOffset + i, yOffset)),
                         MMConv(grfx, new PointF(xOffset + i, yOffset + 5)));

                    grfx.DrawString((i/10).ToString(), Font, brush, 
                         MMConv(grfx, new PointF(xOffset + i, yOffset + 5)), 
                         strfmt);
               }
               else if (i % 5 == 0)     // Half-centimeter markings
               {
                    grfx.DrawLine(pen, 
                         MMConv(grfx, new PointF(xOffset + i, yOffset)),
                         MMConv(grfx, new PointF(xOffset + i, yOffset + 3)));
               }
               else                     // Millimeter markings
               {
                    grfx.DrawLine(pen, 
                      MMConv(grfx, new PointF(xOffset + i, yOffset)),
                      MMConv(grfx, new PointF(xOffset + i, yOffset + 2.5f)));
               }
          }
     }
     PointF MMConv(Graphics grfx, PointF pointf)
     {
	     pointf.X *= grfx.DpiX / 25.4f;
	     pointf.Y *= grfx.DpiY / 25.4f;

	     return pointf;
     }
}

C#在Form上面画一把尺子

相关文章:

  • 2021-05-24
  • 2021-09-04
  • 2022-03-10
  • 2021-10-13
  • 2022-12-23
  • 2022-01-12
  • 2022-02-25
  • 2022-01-23
猜你喜欢
  • 2022-01-20
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
  • 2021-08-03
相关资源
相似解决方案