我是靠谱客的博主 想人陪热狗,最近开发中收集的这篇文章主要介绍矩形和区域,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.       使用Rectangle结构体的方法

 

       private void MethodsMenu_Click(object sender,

      System.EventArgs e)

    {

      // Create a Graphics object

      Graphics g = this.CreateGraphics();

      // Create a Point & Size

      PointF pt = new PointF( 30.8f , 20.7f );

      SizeF sz = new SizeF( 60.0f , 40.0f );

      // Create two rectangles

      RectangleF rect1 = new RectangleF(pt, sz);

      RectangleF rect2 =

        new RectangleF( 40.2f , 40.6f , 100.5f , 100.0f );

      // Ceiling a rectangle

      Rectangle rect3 = Rectangle.Ceiling(rect1);

      // Truncate a rectangle

      Rectangle rect4 = Rectangle.Truncate(rect1);

      // Round a rectangle

      Rectangle rect5 = Rectangle.Round(rect2);

      // Draw rectangles

      g.DrawRectangle(Pens.Black, rect3);

      g.DrawRectangle(Pens.Red, rect5);  

      // Intersect a rectangle  交集

      Rectangle isectRect =

        Rectangle.Intersect(rect3, rect5);

      // Fill rectangle

      g.FillRectangle(

        new SolidBrush(Color.Blue), isectRect);

      // Inflate a recntangle

      Size inflateSize = new Size(0, 40);

      isectRect.Inflate(inflateSize);

      // Draw rectangle

      g.DrawRectangle(Pens.Blue, isectRect);

      // Empty rectangle and set its properties

      rect4 = Rectangle.Empty;

      rect4.Location = new Point(50, 50);

      rect4.X = 30;

      rect4.Y = 40;

      // Union rectangles 并集

      Rectangle unionRect =

        Rectangle.Union(rect4, rect5);

      // Draw rectangle

      g.DrawRectangle(Pens.Green, unionRect);

      // Displose

      g.Dispose();

    }

 

2.       Contains方法用于确定一个矩形或一个点是否在当前矩形的内部。如果在,Contains方法返回true,否则false

 

确定单击鼠标的位置是否在一个矩形内部。我们将检查以确定单击的是否是鼠标左键,然后创建一个矩形,并生产一条消息(如何单击了鼠标左键)。

private void Form1_MouseDown(object sender,

              System.Windows.Forms.MouseEventArgs e)

         {

              if(e.Button == MouseButtons.Left)

              {

                   if (bigRect.Contains( new Point(e.X, e.Y)) )

                       MessageBox.Show("Clicked inside rectangle");

                   else

                       MessageBox.Show("Clicked outside rectangle");     

              }

         }

//MouseEventArgs枚举提供了可用于确定鼠标单击位置的成员,其中包括LeftMiddleNoneRightXbutton1Xbutton2

 

3.      Region

 区域是指一个封闭图形或窗体的内部。一个窗体有两个区,即一个非工作区,一个工作区。非工作区不允许绘图,它包括标题栏以及水平滚动条和垂直滚动条。

 Region类没有属性,在构造了一个区域之后,应用程序可以使用Graphics类的FillRegion方法来填充此区域。

Region方法:

u        Complement方法:将区域更新为一个矩形的不与此区域相交的部分

         private void menuItem7_Click(object sender, System.EventArgs e)

         {

              // Create a Graphics

              Graphics g = this.CreateGraphics();

              // Create two Rectangles

              Rectangle rect1 = new Rectangle(20, 20, 60, 80);

              Rectangle rect2 = new Rectangle(50, 30, 60, 80);

              // Create two Regions

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              // Draw rectangles

              g.DrawRectangle(Pens.Green, rect1);

              g.DrawRectangle(Pens.Black, rect2);

              // Complement can take a Rectangle, RectangleF,

              // Region or a GraphicsPath as an argument

              rgn1.Complement(rgn2);

              // rgn1.Complement(rect2);          

              g.FillRegion(Brushes.Blue, rgn1);   

              // Dispose

              g.Dispose();

         }

u        IsEmpty IsInfinite方法:IsEmpty是否区域为空。IsInfinite是否区域具有无限内部。

         private void menuItem8_Click(object sender, System.EventArgs e)

         {       

              // Create a Graphics object

              Graphics g = this.CreateGraphics();

              g.Clear(this.BackColor);

              // Create Rectangles and Regions

              Rectangle rect1 =

                   new Rectangle(20, 20, 60, 80);           

              Rectangle rect2 =

                   new Rectangle(50, 30, 60, 80);

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              // If Region is not empty, empty it

              if (! rgn1.IsEmpty(g))

                   rgn1.MakeEmpty();

              // If Region is not infinite, infinite

              // it

              if (! rgn2.IsInfinite(g))

                   rgn2.MakeInfinite();

              // Get bounds of the infinite region

              RectangleF rect = rgn2.GetBounds(g);

              // Display

              MessageBox.Show(rect.ToString());

              // Fill the region         

              g.FillRegion(Brushes.Red, rgn2);

              // Dispose

              g.Dispose();

 

         }

u        Exclude方法:将此区域更新为区域内部不与一个矩形相交的部分。

         private void menuItem9_Click(object sender, System.EventArgs e)

         {

              // Create Graphics object

              Graphics g = this.CreateGraphics();

              g.Clear(this.BackColor);

              // Create Rectangles

              Rectangle rect1 = new Rectangle(20, 20, 60, 80);            

              Rectangle rect2 = new Rectangle(50, 30, 60, 80);

              // Create Regions

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              // Draw rectangles

              g.DrawRectangle(Pens.Green, rect1);

              g.DrawRectangle(Pens.Black, rect2);

              // Exclued the region

              rgn1.Exclude(rgn2);   

              // Fill region after exclude    

              g.FillRegion(Brushes.Blue, rgn1);   

              // Dispose

              g.Dispose();

         }

u        Union方法:将此区域更新为它与给定图形路径的幷集。

         private void menuItem10_Click(object sender, System.EventArgs e)

         {

        

              Graphics g = this.CreateGraphics();

              g.Clear(this.BackColor);

 

              Rectangle rect1 = new Rectangle(20, 20, 60, 80);            

              Rectangle rect2 = new Rectangle(50, 30, 60, 80);

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              g.DrawRectangle(Pens.Green, rect1);

              g.DrawRectangle(Pens.Black, rect2);

              // Complement can take a Rectangle, RectangleF,

              // Region or a GraphicsPath as an argument

              rgn1.Union(rgn2); 

              // rgn1.Complement(rect2);          

              g.FillRegion(Brushes.Blue, rgn1);

    

              // Dispose

              g.Dispose();

         }

u        Xor方法:将此区域更新为它与给定图形路径的幷集再减去交集。

         private void menuItem11_Click(object sender, System.EventArgs e)

         {

              // Create Graphics object

              Graphics g = this.CreateGraphics();

              g.Clear(this.BackColor);

              // Create Rectangles

              Rectangle rect1 = new Rectangle(20, 20, 60, 80);

              Rectangle rect2 = new Rectangle(50, 30, 60, 80);

              // Create Regions

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              // Draw Rectangles

              g.DrawRectangle(Pens.Green, rect1);

              g.DrawRectangle(Pens.Black, rect2);

              // Xoring two regions

              rgn1.Xor(rgn2);   

              // Fill the region after Xoring

              g.FillRegion(Brushes.Blue, rgn1);   

              // Dispose

              g.Dispose();

        

         }

u        Intersect方法:将此区域更新为它与给定图形路径的交集。

         private void menuItem12_Click(object sender, System.EventArgs e)

         {

              Graphics g = this.CreateGraphics();

              g.Clear(this.BackColor);

 

              Rectangle rect1 = new Rectangle(20, 20, 60, 80);            

              Rectangle rect2 = new Rectangle(50, 30, 60, 80);

              Region rgn1 = new Region(rect1);

              Region rgn2 = new Region(rect2);

              g.DrawRectangle(Pens.Green, rect1);

              g.DrawRectangle(Pens.Black, rect2);

              // Complement can take a Rectangle, RectangleF,

              // Region or a GraphicsPath as an argument

              rgn1.Intersect(rgn2); 

              // rgn1.Complement(rect2);          

              g.FillRegion(Brushes.Blue, rgn1);

    

              // Dispose

              g.Dispose();

         }

 

最后

以上就是想人陪热狗为你收集整理的矩形和区域的全部内容,希望文章能够帮你解决矩形和区域所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部