Csharp/CSharp Tutorial/2D/Ellipse

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

Draw an ellipse

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class DrawAnEllipse : System.Windows.Forms.Form
{
  public DrawAnEllipse()
  {
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(400, 400);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawAnEllipse_Paint);
  }
  static void Main() 
  {
    Application.Run(new DrawAnEllipse());
  }
  private void DrawAnEllipse_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    // 
    Pen p2 = new Pen(Color.Gray, 7);
    g.DrawEllipse(p2, 200, 200, 150, 190);
  }
}

DrawEllipse: Traffic Lights

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    private string strColor;
    public static void Main() {
        Application.Run(new Form1());
    }
    private void Form1_Click(object sender, EventArgs e) {
        switch (strColor) {
            case "red":
                strColor = "yellow";
                break;
            case "yellow":
                strColor = "green";
                break;
            default:
                strColor = "red";
                break;
        }
        this.Invalidate(new Rectangle(10, 10, 50, 150));
    }
    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White, this.ClientRectangle);
        g.FillRectangle(Brushes.Black, 10, 10, 50, 150);
        g.DrawEllipse(Pens.White, 15, 15, 40, 40);
        g.DrawEllipse(Pens.White, 15, 60, 40, 40);
        g.DrawEllipse(Pens.White, 15, 105, 40, 40);
        switch (strColor) {
            case "red":
                g.FillEllipse(Brushes.Red, 15, 15, 40, 40);
                break;
            case "yellow":
                g.FillEllipse(Brushes.Yellow, 15, 60, 40, 40);
                break;
            case "green":
                g.FillEllipse(Brushes.Green, 15, 105, 40, 40);
                break;
            default:
                g.FillEllipse(Brushes.Red, 15, 15, 40, 40);
                break;
        }
    }
}