Csharp/CSharp Tutorial/2D/Draw String

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A rectangle with some text

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class RectangleWithText : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public RectangleWithText()
  {
    InitializeComponent();
    SetStyle(ControlStyles.ResizeRedraw, true);
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.RectangleWithText_Paint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new RectangleWithText());
  }
  private void RectangleWithText_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Rectangle r = new Rectangle(150, 10, 130, 60);
    g.DrawRectangle(Pens.Blue, r);
    g.DrawString("www.nfex.ru", new Font("Arial", 12), Brushes.Black, r);
  
  }
}

Draw string along a circle

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class DrawStringAlignACircle : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public DrawStringAlignACircle()
  {
    InitializeComponent();
    BackColor = SystemColors.Window;
    ForeColor = SystemColors.WindowText;
  }
  protected override void OnPaint ( PaintEventArgs e )
  {
    Graphics g = e.Graphics;
    SetScale(g);
    DrawFace(g);
    base.OnPaint(e);
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new DrawStringAlignACircle());
  }
  private void SetScale(Graphics g)
  {
    g.TranslateTransform(Width/2, Height/2);
    float inches = Math.Min(Width / g.DpiX, Height / g.DpiX);
    g.ScaleTransform(inches * g.DpiX / 2000, inches * g.DpiY / 2000);
  }
  private void DrawFace(Graphics g)
  {
    Brush brush = new SolidBrush(ForeColor);
    Font font = new Font("Arial", 40);
  
    float x, y;
    const int numHours = 12;
    const int deg = 360 / numHours;
    const int FaceRadius = 450;
    
    for (int i = 1; i <= numHours; i++)
    {
      x = GetCos(i*deg + 90) * FaceRadius;
      y = GetSin(i*deg + 90) * FaceRadius;
      StringFormat format = new StringFormat();
      format.Alignment = StringAlignment.Center;
      format.LineAlignment = StringAlignment.Center;
      g.DrawString( i.ToString(), font, brush, -x, -y,format);
    
    }
    brush.Dispose();
    font.Dispose();
  }  
  private static float GetSin(float degAngle)
  {
    return (float) Math.Sin(Math.PI * degAngle / 180f);
  }
  private static float GetCos(float degAngle)
  {
    return (float) Math.Cos(Math.PI * degAngle / 180f);
  }
}

Draw string along a circle 2

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class DrawStringAlignACircle : System.Windows.Forms.Form
{
  public DrawStringAlignACircle()
  {
    InitializeComponent();
    BackColor = SystemColors.Window;
    ForeColor = SystemColors.WindowText;
  }
  protected override void OnPaint ( PaintEventArgs e )
  {
    Graphics g = e.Graphics;
    SetScale(g);
    DrawFace(g);
    base.OnPaint(e);
  }
  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new DrawStringAlignACircle());
  }
  private void SetScale(Graphics g)
  {
    g.TranslateTransform(Width/2, Height/2);
    float inches = Math.Min(Width / g.DpiX, Height / g.DpiX);
    g.ScaleTransform(inches * g.DpiX / 2000, inches * g.DpiY / 2000);
  }
  private void DrawFace(Graphics g)
  {
    Brush brush = new SolidBrush(ForeColor);
    Font font = new Font("Arial", 40);
  
    float x, y;
    const int numHours = 12;
    const int deg = 360 / numHours;
    const int FaceRadius = 450;
    
    for (int i = 1; i <= numHours; i++)
    {
      SizeF stringSize = 
        g.MeasureString(i.ToString(),font);
        
      x = GetCos(i*deg + 90) * FaceRadius;
      x += stringSize.Width / 2;
      y = GetSin(i*deg + 90) * FaceRadius;
      y += stringSize.Height / 2;
      g.DrawString(i.ToString(), font, brush, -x, -y);
    
    }
    brush.Dispose();
    font.Dispose();
  }  
  private static float GetSin(float degAngle)
  {
    return (float) Math.Sin(Math.PI * degAngle / 180f);
  }
  private static float GetCos(float degAngle)
  {
    return (float) Math.Cos(Math.PI * degAngle / 180f);
  }
}

Draw string with HatchStyle

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class MainForm : Form {
    public MainForm() {
        CenterToScreen();
        InitializeComponent();
    }
    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        int yOffSet = 10;
        Array obj = Enum.GetValues(typeof(HatchStyle));
        for (int x = 0; x < 5; x++) {
            HatchStyle temp = (HatchStyle)obj.GetValue(x);
            HatchBrush theBrush = new HatchBrush(temp, Color.White, Color.Black);
            g.DrawString(temp.ToString(), new Font("Times New Roman", 10),
                         Brushes.Black, 0, yOffSet);
            g.FillEllipse(theBrush, 150, yOffSet, 200, 25);
            yOffSet += 40;
        }
    }
}