Control.OnPaint(PaintEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 Paint 事件。
protected:
virtual void OnPaint(System::Windows::Forms::PaintEventArgs ^ e);
protected virtual void OnPaint (System.Windows.Forms.PaintEventArgs e);
abstract member OnPaint : System.Windows.Forms.PaintEventArgs -> unit
override this.OnPaint : System.Windows.Forms.PaintEventArgs -> unit
Protected Overridable Sub OnPaint (e As PaintEventArgs)
參數
PaintEventArgs,其中包含事件資料。
範例
下列程式碼範例可讓使用者將影像或影像檔拖曳到表單上,並將其顯示在卸載的點。 系統會 OnPaint 覆寫 方法,在每次繪製表單時重新繪製影像;否則,影像只會持續到下一次重新繪製為止。 DragEnter事件處理方法會決定要拖曳到表單中的資料類型,並提供適當的意見反應。 如果 Image 可以從資料建立 ,事件 DragDrop 處理方法會顯示表單上的影像。 DragEventArgs.X由於 和 DragEventArgs.Y 值是螢幕座標,所以此範例會使用 PointToClient 方法來將它們轉換成用戶端座標。
private:
Image^ picture;
Point pictureLocation;
public:
Form1()
{
// Enable drag-and-drop operations and
// add handlers for DragEnter and DragDrop.
this->AllowDrop = true;
this->DragDrop += gcnew DragEventHandler( this, &Form1::Form1_DragDrop );
this->DragEnter += gcnew DragEventHandler( this, &Form1::Form1_DragEnter );
}
protected:
virtual void OnPaint( PaintEventArgs^ e ) override
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
Form::OnPaint( e );
if ( this->picture != nullptr && 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.
array<String^>^files = (array<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( 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 = dynamic_cast<Image^>(e->Data->GetData( DataFormats::Bitmap ));
// Set the picture location equal to the drop point.
this->pictureLocation = this->PointToClient( Point(e->X,e->Y) );
}
catch ( Exception^ ex )
{
MessageBox::Show( ex->Message );
return;
}
}
// Force the form to be redrawn with the image.
this->Invalidate();
}
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;
}
}
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;
}
}
Private picture As Image
Private pictureLocation As Point
Public Sub New()
' Enable drag-and-drop operations.
Me.AllowDrop = True
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
' If there is an image and it has a location,
' paint it when the Form is repainted.
If (Me.picture IsNot Nothing) And _
Not (Me.pictureLocation.Equals(Point.Empty)) Then
e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
End If
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, _
ByVal e As DragEventArgs) Handles MyBase.DragDrop
' Handle FileDrop data.
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
' Assign the file names to a string array, in
' case the user has selected multiple files.
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Try
' Assign the first image to the 'picture' variable.
Me.picture = Image.FromFile(files(0))
' Set the picture location equal to the drop point.
Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
' Handle Bitmap data.
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
Try
' Create an Image and assign it to the picture variable.
Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
' Set the picture location equal to the drop point.
Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
' Force the form to be redrawn with the image.
Me.Invalidate()
End Sub
Private Sub Form1_DragEnter(ByVal sender As Object, _
ByVal e As DragEventArgs) Handles MyBase.DragEnter
' If the data is a file or a bitmap, display the copy cursor.
If e.Data.GetDataPresent(DataFormats.Bitmap) _
Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
// 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;
void Form1_Load( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
pictureBox1 = gcnew PictureBox;
// 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 += gcnew System::Windows::Forms::PaintEventHandler( this, &Form1::pictureBox1_Paint );
// Add the PictureBox control to the Form.
this->Controls->Add( pictureBox1 );
}
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",
gcnew System::Drawing::Font( "Arial",10 ), System::Drawing::Brushes::Blue, Point(30,30) );
// Draw a line in the PictureBox.
g->DrawLine( System::Drawing::Pens::Red, pictureBox1->Left, pictureBox1->Top,
pictureBox1->Right, pictureBox1->Bottom );
}
// 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();
// Cache font instead of recreating font objects each time we paint.
private Font fnt = new Font("Arial",10);
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",
fnt, 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);
}
' 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 pictureBox1 As New PictureBox()
Private fnt as New Font("Arial", 10)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' 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.
AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint
' Add the PictureBox control to the Form.
Me.Controls.Add(pictureBox1)
End Sub
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
' Create a local version of the graphics object for the PictureBox.
Dim g As Graphics = e.Graphics
' Draw a string on the PictureBox.
g.DrawString("This is a diagonal line drawn on the control", _
fnt, Brushes.Red, New PointF(30.0F, 30.0F))
' Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
End Sub
備註
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件。
方法 OnPaint 也可讓衍生類別處理事件,而不附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnPaint(PaintEventArgs) 時,請確定呼叫基底類別的 OnPaint(PaintEventArgs) 方法,使已註冊的委派能接收到事件。