VS.NET如何在panel上用graphics绘图

2010-08-28 10:49:52来源:西部e网作者:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PanelGraphics
{
 /// <summary>
 /// Form1 的摘要描述。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Panel panel1;
  /// <summary>
  /// 設計工具所需的變數。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows Form 設計工具支援的必要項
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
   //
  }

  /// <summary>
  /// 清除任何使用中的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form 設計工具產生的程式碼
  /// <summary>
  /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
  /// 這個方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.panel1 = new System.Windows.Forms.Panel();
   this.SuspendLayout();
   //
   // panel1
   //
   this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.panel1.Location = new System.Drawing.Point(0, 0);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(292, 266);
   this.panel1.TabIndex = 0;
   this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Controls.Add(this.panel1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   // Create a Graphics object for the Control.
   Graphics g = panel1.CreateGraphics();
   // Create pen.
   Pen blackPen = new Pen(Color.Black, 3);
   // Create location and size of ellipse.
   float x = 0.0F;
   float y = 0.0F;
   float width = 150.0F;
   float height = 150.0F;
   // Draw ellipse to screen.
   g.DrawEllipse(blackPen, x, y, width, height);
   // Clean up the Graphics object.
   g.Dispose();
  }
 }
}

关键词:VS.NET

赞助商链接: