Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método Control.OnPaint (PaintEventArgs)

 

Aciona o evento Paint.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms.dll)

protected virtual void OnPaint(
	PaintEventArgs e
)

Parâmetros

e
Type: System.Windows.Forms.PaintEventArgs

Um PaintEventArgs que contém os dados do evento.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The M:System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs) method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Observações para os Herdeiros:

When overriding M:System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs) in a derived class, be sure to call the base class's M:System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs) method so that registered delegates receive the event.

The following code example enables the user to drag an image or image file onto the form, and have it be displayed at the point on which it is dropped. The M:System.Windows.Forms.Control.OnPaint(System.Windows.Forms.PaintEventArgs) method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting. The E:System.Windows.Forms.Control.DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. The E:System.Windows.Forms.Control.DragDrop event-handling method displays the image on the form, if an T:System.Drawing.Image can be created from the data. Because the P:System.Windows.Forms.DragEventArgs.X and P:System.Windows.Forms.DragEventArgs.Y values are screen coordinates, the example uses the M:System.Windows.Forms.Control.PointToClient(System.Drawing.Point) method to convert them to client coordinates.

private Image picture;
private Point pictureLocation;

public Form1()
{
   // Enable drag-and-drop operations and 
   // add handlers for DragEnter and DragDrop.
   this.AllowDrop = true;
   this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
   this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
}

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
   // Handle FileDrop data.
   if(e.Data.GetDataPresent(DataFormats.FileDrop) )
   {
      // Assign the file names to a string array, in 
      // case the user has selected multiple files.
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      try
      {
         // Assign the first image to the picture variable.
         this.picture = Image.FromFile(files[0]);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }

   // Handle Bitmap data.
   if(e.Data.GetDataPresent(DataFormats.Bitmap) )
   {
      try
      {
         // Create an Image and assign it to the picture variable.
         this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }
   // Force the form to be redrawn with the image.
   this.Invalidate();
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is a file or a bitmap, display the copy cursor.
   if (e.Data.GetDataPresent(DataFormats.Bitmap) || 
      e.Data.GetDataPresent(DataFormats.FileDrop) ) 
   {
      e.Effect = DragDropEffects.Copy;
   }
   else
   {
      e.Effect = DragDropEffects.None;
   }
}
// This example creates a PictureBox control on the form and draws to it.
// This example assumes that the Form_Load event handler method is
// connected to the Load event of the form.

private PictureBox pictureBox1 = new PictureBox();
private void Form1_Load(object sender, System.EventArgs e)
{
    // Dock the PictureBox to the form and set its background to white.
    pictureBox1.Dock = DockStyle.Fill;
    pictureBox1.BackColor = Color.White;
    // Connect the Paint event of the PictureBox to the event handler method.
    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

    // Add the PictureBox control to the Form.
    this.Controls.Add(pictureBox1);
}

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Create a local version of the graphics object for the PictureBox.
    Graphics g = e.Graphics;

    // Draw a string on the PictureBox.
    g.DrawString("This is a diagonal line drawn on the control",
        new Font("Arial",10), System.Drawing.Brushes.Blue, new Point(30,30));
    // Draw a line in the PictureBox.
    g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, pictureBox1.Top,
        pictureBox1.Right, pictureBox1.Bottom);
}

.NET Framework
Disponível desde 1.1
Retornar ao início
Mostrar: