using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; // for GDI+ namespace lineEllipse { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } 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, 266); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.Text= Application.ProductName + " " + Application.ProductVersion; } //in order to paint something OnPaint method needs to be overridden protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) { base.OnPaint(pe); // call the superclass (NOTE: this must be here!) try { // --------------- prepare to draw in a specified area ---------------------- Rectangle drawingArea; // for drawing in Point pointTopLeft; // drawing area's top left limit Point pointBottomRight; // drawing area's bottom right limit drawingArea = this.ClientRectangle; pointTopLeft = new Point(drawingArea.X, drawingArea.Y); pointBottomRight = new System.Drawing.Point(drawingArea.Width, drawingArea.Height); // need a Brush object to fill in the background color Brush brushBackGround; brushBackGround = new System.Drawing.SolidBrush(System.Drawing.Color.White); // need a Graphics object to draw Graphics myGraphicsObject; myGraphicsObject = pe.Graphics; // initialise it // fill in the background color myGraphicsObject.FillRectangle(brushBackGround, drawingArea); myGraphicsObject.FillRectangle(brushBackGround, drawingArea); int lineWidth = 3; // ----------------- draw the ellipse ------------------------ Pen penEllipse; // for drawing the ellipse penEllipse = new System.Drawing.Pen(System.Drawing.Color.Black, lineWidth); PointF myPoint; // upper-left corner of the ellipse rectangle myPoint = new PointF(); myPoint.X = 8; myPoint.Y = 40; SizeF mySize; // width and height of the ellipse rectangle mySize = new SizeF(); mySize.Height = 160; mySize.Width = 180; RectangleF ellipseArea; // defines the ellipse area ellipseArea = new System.Drawing.RectangleF(myPoint, mySize); myGraphicsObject.DrawEllipse(penEllipse, ellipseArea); PointF myPoint1; // upper-left corner of the ellipse rectangle myPoint1 = new PointF(); myPoint1.X = myPoint.X + mySize.Height; myPoint1.Y = myPoint.Y + mySize.Width; LinearGradientBrush linGrBrush = new LinearGradientBrush( myPoint, myPoint1, Color.Red, Color.Blue); linGrBrush.GammaCorrection = true; // myGraphicsObject.FillEllipse(linGrBrush,ellipseArea); GraphicsPath path = new GraphicsPath(); path.AddEllipse(ellipseArea); PathGradientBrush pthGrBrush = new PathGradientBrush(path); pthGrBrush.CenterColor = Color.FromArgb(255, 0, 0, 255); // Set the color along the entire boundary // of the path to aqua. Color[] colors = {Color.FromArgb(255, 0, 255, 255)}; pthGrBrush.SurroundColors = colors; myGraphicsObject.FillEllipse(pthGrBrush,ellipseArea); // ----------------- draw lines ------------------------ Pen penLine = new System.Drawing.Pen(System.Drawing.Color.Red, lineWidth); Point pointLineEnd1 = new Point(); // one end of the line pointLineEnd1.X = 0; pointLineEnd1.Y = 0; Point pointLineEnd2 = new Point(); // other end of the line pointLineEnd2.X = 300; pointLineEnd2.Y = 300; // draw the first line myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); // scoot the end points over, change pen color, and draw another line pointLineEnd1.Y = pointLineEnd1.Y + 10; pointLineEnd2.Y = pointLineEnd2.Y + 30; penLine = new Pen(System.Drawing.Color.Orange, lineWidth); myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); // scoot the end points over, change pen color, and draw another line pointLineEnd1.Y = pointLineEnd1.Y + 10; pointLineEnd2.Y = pointLineEnd2.Y + 30; penLine = new Pen(System.Drawing.Color.Green, lineWidth); myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); // scoot the end points over, change pen color, and draw another line pointLineEnd1.Y = pointLineEnd1.Y + 10; pointLineEnd2.Y = pointLineEnd2.Y + 30; penLine = new Pen(System.Drawing.Color.Blue, lineWidth); myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); // scoot the end points over, change pen color, and draw another line pointLineEnd1.Y = pointLineEnd1.Y + 10; pointLineEnd2.Y = pointLineEnd2.Y + 30; penLine = new Pen(System.Drawing.Color.Yellow, lineWidth); myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); // scoot the end points over, change pen color, and draw another line pointLineEnd1.Y = pointLineEnd1.Y + 10; pointLineEnd2.Y = pointLineEnd2.Y + 30; penLine = new Pen(System.Drawing.Color.Purple, lineWidth); myGraphicsObject.DrawLine(penLine, pointLineEnd1, pointLineEnd2); } catch (Exception ex) { MessageBox.Show(ex.Message); } catch { MessageBox.Show("unknown exception"); } } // end OnPaint } // end class } // end namespace