1。Drawing to the Screen

Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basicsbool drawEllipse = false;
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  drawEllipse 
= !drawEllipse;
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  Graphics g 
= this.CreateGraphics();
{
{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics      
// Draw the ellipse
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
      g.FillEllipse(Brushes.DarkBlue, this.ClientRectangle);
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics    }

{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics      
// Erase the previously drawn ellipse
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
      g.FillEllipse(SystemBrushes.Control, this.ClientRectangle);
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics    }

Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  }

{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics    g.Dispose();
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  }

Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics}

    注意:加 try-catch 块的原因是:“The Graphic class's implementation of IDisposable Dispose can release the underlying graphics object that it's maintaining.”,Graphics 对象是必须要被释放的。        此处也可以用 C# 特有的 using 块(结束后会自动调用 Dispose):
{
{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics          if( drawEllipse ) 
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics             g.FillEllipse(Brushes.DarkBlue, 
this.ClientRectangle);
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics         
else 
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics             g.FillEllipse(SystemBrushes.Control, 
this.ClientRectangle);
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics      }
 // g.Dispose called automatically here
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
}
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics

        注意:如果想在窗口 resize 时候让椭圆自动重画。需要在 Form 类的constructor中加入 this.SetStyle(ControlStyles.ResizeRedraw, true) ;    同时在 button click事件中加入 this.Refresh();(相当于 this.Invalidate(true) 加上 this.Update();但是 resize 时候自动重画全部窗口的效率不高,比较费时。

2。Saving and Restoring Graphics Settings

 使用 System.Drawing. Drawing2D namespace 中的 GraphicsState 对象,再加上 Graphics 类的 Save() 和 Restore() 方法
{
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  // 保存旧的
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
  GraphicsState oldState = g.Save();
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  
// 改变一下平滑模式
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
  g.SmoothingMode = SmoothingMode.AntiAlias;
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  
// 开始画Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics  
// 恢复旧的
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
  g.Restore(oldState);
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics}

Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics
Windows Forms Programming In C# 读书笔记 - 第四章 Drawing Basics

相关文章:

  • 2022-12-23
  • 2021-12-01
  • 2021-09-27
  • 2022-12-23
  • 2021-12-26
  • 2021-10-12
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2021-06-12
  • 2021-11-16
  • 2021-06-22
  • 2021-08-19
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案