Csharp/CSharp Tutorial/GUI Windows Forms/Form Paint

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

FormPaint Event

using System;        
using System.Drawing;
using System.Windows.Forms;
public class FormPaintEvent : Form
{
  public FormPaintEvent()
  {
    Size = new Size(300,200);
    Paint += new PaintEventHandler(PaintHandler);
  }
  static void Main() 
  {
    Application.Run(new FormPaintEvent());
  }
  private void PaintHandler(object sender, PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    g.DrawString("www.nfex.ru", Font, Brushes.Black, 50, 75);
  }
}

Override Paint method for Form

using System;
using System.Drawing;
using System.Windows.Forms;
public class FormPaintOverride : Form
{
  public FormPaintOverride()
  {
        Text = "Paint Demonstration";
    Size = new Size(300,200);
  }
  static void Main() 
  {
    Application.Run(new FormPaintOverride());
  }
  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    Graphics g = e.Graphics;
    g.DrawString("www.nfex.ru", Font, Brushes.Black, 50, 75);
  }
}

Paint Invalidate Demonstration

using System;
using System.Drawing;
using System.Windows.Forms;
public class PaintInvalidate : Form
{
  private Button btn;
  public PaintInvalidate()
  {
    Size = new Size(300,200);
    btn = new Button();
    btn.Parent = this;
    btn.Location = new Point(25,25);
    btn.Text = "Update";
    btn.Click += new System.EventHandler(btn_Click);
  }
  static void Main() 
  {
    Application.Run(new PaintInvalidate());
  }
  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    Graphics g = e.Graphics;
    String str = "\nThe time is " + DateTime.Now.ToLongTimeString();
    g.DrawString(str, Font, Brushes.Black, 50, 75);
  }
  private void btn_Click(object sender, EventArgs e)
  {
    Invalidate();
  }
}