Csharp/CSharp Tutorial/2D/Region
Содержание
Create an irregular Region add a circle to the region
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Region reg = new Region(); reg.MakeEmpty(); GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(10, 10, 50, 50); reg.Union(gp); gp.Reset(); gp.AddLine(40, 40, 70, 10); gp.AddLine(70, 10, 100, 40); gp.CloseFigure(); reg.Union(gp); reg.Union(new Rectangle(40, 50, 60, 60)); g.SetClip(reg, CombineMode.Replace); g.FillRectangle(Brushes.Green, this.ClientRectangle); gp.Dispose(); reg.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}</source>
Create a Region object, cut a rectangular hole in it, and fill it
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Region r = new Region(new Rectangle(30, 30, 30, 60)); r.Exclude(new Rectangle(40, 40, 10, 10)); g.FillRegion(Brushes.Orange, r); Console.WriteLine("This Region: "); Console.WriteLine(r.IsInfinite(g) ? " - is infinite" : " - is finite"); Console.WriteLine(r.IsEmpty(g) ? " - is empty" : " - is non-empty"); PointF pf = new PointF(35.0f, 30.0f); Console.WriteLine((r.IsVisible(pf) ? " - includes" : " - excludes") + " the point (35.0, 50.0)"); Rectangle rect = new Rectangle(25, 65, 15, 15); g.DrawRectangle(Pens.Black, rect); Console.WriteLine((r.IsVisible(rect) ? " - is visible" : " - is invisible") + " in the rectangle shown"); r.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}</source>
Create a Region whose boundary is the above GraphicsPath
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); GraphicsPath gp = new GraphicsPath(); gp.AddLine(10, 10, 10, 50); gp.AddLine(10, 50, 50, 50); gp.AddLine(50, 50, 50, 10); gp.StartFigure(); gp.AddLine(60, 10, 60, 50); gp.AddLine(60, 50, 100, 50); gp.AddLine(100, 50, 100, 10); gp.CloseFigure(); Rectangle r = new Rectangle(110, 10, 40, 40); gp.AddEllipse(r); Region reg = new Region(gp); g.FillRegion(Brushes.Green, reg); reg.Dispose(); gp.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}</source>
Region.IsVisible
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1; GraphicsPath gP; string mes = "Move to the big I!"; FontFamily fF = new FontFamily("Times new roman"); public Form1() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); this.label1.Location = new System.Drawing.Point(88, 16); this.label1.Size = new System.Drawing.Size(160, 23); this.label1.Text = "label1"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 109); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label1 }); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.ResumeLayout(false); Graphics g = this.CreateGraphics(); label1.Text = mes; string s = "I"; int fSt = (int)FontStyle.Regular; Point xy = new Point(50, 10); StringFormat sFr = StringFormat.GenericDefault; gP = new GraphicsPath(); gP.AddString(s, fF, fSt, 50, xy, sFr); } [STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Graphics g = this.CreateGraphics(); g.DrawPath(Pens.Black, gP); // draw the path to the surface g.Dispose(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { Region reg = new Region(gP); if (reg.IsVisible(new Point(e.X, e.Y))) mes = "in"; else mes = "Move to the big I!"; }
}</source>