Sie u Quant Rong

download Sie u Quant Rong

of 6

Transcript of Sie u Quant Rong

  • 8/12/2019 Sie u Quant Rong

    1/6

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.ComponentModel;using System.Drawing.Drawing2D;namespace painttest{ public partial class Form1 : Form { public Form1() { InitializeComponent(); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true); }

    public class shape {

    public int PenWidth; public Color Color; public Rectangle Rectangle; public virtual void Draw(Graphics graphics){} } //------------------------------------------------- class Ellipse:shape { public override void Draw(Graphics graphics) { using (Pen pen = new Pen(Color, PenWidth)) graphics.DrawEllipse(pen, Rectangle);

    } } //------------------------------------------------- class MyRectangle:shape { public override void Draw(Graphics graphics) { using (Pen pen = new Pen(Color, PenWidth)) graphics.DrawRectangle(pen, Rectangle); } } //------------------------------------------------------------- List allshape = new List();

    //----------------------------------------------------- class EllipseConstruction { public Point Origin; public shape Ellipse; } EllipseConstruction ellipseConstruction; //------------------------------------------------------------- //class RectangleConstruction //{

  • 8/12/2019 Sie u Quant Rong

    2/6

    // public Point Origin; // public MyRectangle myRectangle; //} //RectangleConstruction rectangleConstruction;

    //---------------------------------------------------- private Random Rand = new Random(); static readonly Color[] TableColors = { Color.Black, Color.White, Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Magenta, Color.Cyan, };

    protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Capture = true;

    if (radioEllipse.Checked) { ellipseConstruction = new EllipseConstruction { Origin = e.Location, Ellipse = new Ellipse { Color = TableColors[Rand.Next(TableColors.Length)], PenWidth = Rand.Next(1, 6) } }; } if (radioRectangle.Checked) { ellipseConstruction = new EllipseConstruction {

    Origin = e.Location, Ellipse = new MyRectangle { Color = TableColors[Rand.Next(TableColors.Length)], PenWidth = Rand.Next(1, 6) } }; } } base.OnMouseDown(e); } protected override void OnMouseMove(MouseEventArgs e) { if (Capture) {

    UpdateEllipseUnderConstruction(e.Location);

    } base.OnMouseMove(e); } private void UpdateEllipseUnderConstruction(Point point) { Point origin = ellipseConstruction.Origin;

    int xRadius = Math.Abs(origin.X - point.X);

  • 8/12/2019 Sie u Quant Rong

    3/6

    int yRadius = Math.Abs(origin.Y - point.Y);

    // Make X and Y radii the same for a true circle unless the Shift key is held down if ((ModifierKeys & Keys.Shift) == 0) ellipseConstruction.Ellipse.Rectangle = new Rectangle(origin.X - xRadius, origin.Y - yRadius, xRadius * 2, yRadius * 2); else { xRadius = yRadius = Math.Max(xRadius, yRadius);

    ellipseConstruction.Ellipse.Rectangle = new Rectangle(origin.X - xRadius, origin.Y - yRadius, xRadius * 2, yRadius * 2); }

    Invalidate(); // Notify operating system that we need to be repainted. } protected override void OnMouseUp(MouseEventArgs e) { if (Capture && e.Button == MouseButtons.Left) { Capture = false; {

    UpdateEllipseUnderConstruction(e.Location); if (ellipseConstruction.Ellipse.Rectangle.Width > 0 && ellipseConstruction.Ellipse.Rectangle.Height > 0) allshape.Add(ellipseConstruction.Ellipse); } ellipseConstruction = null; } base.OnMouseUp(e); } protected override void OnKeyDown(KeyEventArgs e) { if (Capture && e.KeyData == Keys.Escape) {

    Capture = false;ellipseConstruction = null; // Remove construction ellipse Invalidate(); // Notify operating system that we need to be repainted. } base.OnKeyDown(e); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.Clear(Color.SlateGray); foreach (shape ellipse in allshape) ellipse.Draw(e.Graphics); if (ellipseConstruction != null)

    ellipseConstruction.Ellipse.Draw(e.Graphics); base.OnPaint(e); }

    }}//using System;//using System.Collections.Generic;//using System.Drawing;//using System.Windows.Forms;

  • 8/12/2019 Sie u Quant Rong

    4/6

    //// Definition for our ellipse object.//class Ellipse//{// public int PenWidth;// public Color Color;// public Rectangle Rectangle;

    // // Paint ourselves with the specified Graphics object// public void Draw(Graphics graphics)// {// using (Pen pen = new Pen(Color, PenWidth))// graphics.DrawEllipse(pen, Rectangle);// }// class Form1 : Form// {// //[STAThread]// //static void Main()// //{// // Application.EnableVisualStyles();// // Application.SetCompatibleTextRenderingDefault(false);// // Application.Run(new Form1());// //}

    // public Form1()// {// // Remove "flickering" from the repainting.// SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);// }

    // // Store all created ellipses so they can be rendered during the Paint event.// List ellipses = new List();

    // // Definition of an Ellipse under construction

    // class EllipseConstruction// {// public Point Origin;// public Ellipse Ellipse;// }

    // // Storage for ellipse under construction.// EllipseConstruction ellipseConstruction;

    // // Random number generator Ellipse creation.// private Random Rand = new Random();

    // // These are the possible ellipse colors

    // static readonly Color[] EllipseColors =// {// Color.Black,// Color.White,// Color.Red,// Color.Green,// Color.Blue,// Color.Yellow,// Color.Magenta,// Color.Cyan,

  • 8/12/2019 Sie u Quant Rong

    5/6

    // };

    // protected override void OnMouseDown(MouseEventArgs e)// {// if (e.Button == MouseButtons.Left)// {// // Capture mouse until button up.// Capture = true;

    // // Create a new Ellipse object and record the [X, Y] origin of this click// ellipseConstruction = new EllipseConstruction// {// Origin = e.Location,// Ellipse = new Ellipse { Color = EllipseColors[Rand.Next(EllipseColors.Length)], PenWidth = Rand.Next(1, 6) }// };// }

    // base.OnMouseDown(e);// }

    // protected override void OnMouseMove(MouseEventArgs e)// {

    // // If the mouse is captured, the user is creating a new Ellipse so we update its rectangle with the mouse coordinates// if (Capture)// UpdateEllipseUnderConstruction(e.Location);

    // base.OnMouseMove(e);// }// private void UpdateEllipseUnderConstruction(Point point)// {// // Calculate new ellipse rectangle based on ellipseConstruction.Origin and point.

    // Point origin = ellipseConstruction.Origin;

    // int xRadius = Math.Abs(origin.X - point.X);// int yRadius = Math.Abs(origin.Y - point.Y);

    // // Make X and Y radii the same for a true circle unless the Shiftkey is held down// if ((ModifierKeys & Keys.Shift) == 0)// xRadius = yRadius = Math.Max(xRadius, yRadius);

    // ellipseConstruction.Ellipse.Rectangle = new Rectangle(origin.X - xRadius, origin.Y - yRadius, xRadius * 2, yRadius * 2);

    // Invalidate(); // Notify operating system that we need to be repain

    ted.// }

    // protected override void OnMouseUp(MouseEventArgs e)// {// if (Capture && e.Button == MouseButtons.Left)// {// // If the mouse is captured and it's the Left button being released, the user is// // done creating a new Ellipse.

  • 8/12/2019 Sie u Quant Rong

    6/6