Control.Invalidate 方法
使控制項的特定地區無效並向控制項發送繪製訊息。
重載列表
使控制項的特定地區無效並向控制項發送繪製訊息。
受 .NET Framework 精簡版的支援。
[C#] public void Invalidate();
[C++] public: void Invalidate();
使控制項的特定地區無效並向控制項發送繪製訊息。還可以使分配給該控制項的子控制項無效。
[C#] public void Invalidate(bool);
使控制項的指定地區無效(將其添加到控制項的更新地區,下次繪製操作時將重新繪製更新地區),並向控制項發送繪製訊息。
受 .NET Framework 精簡版的支援。
[C#] public void Invalidate(Rectangle);
使控制項的指定地區無效(將其添加到控制項的更新地區,下次繪製操作時將重新繪製更新地區),並向控制項發送繪製訊息。
[C#] public void Invalidate(Region);
使控制項的指定地區無效(將其添加到控制項的更新地區,下次繪製操作時將重新繪製更新地區),並向控制項發送繪製訊息。還可以使分配給該控制項的子控制項無效。
[C#] public void Invalidate(Rectangle, bool);
使控制項的指定地區無效(將其添加到控制項的更新地區,下次繪製操作時將重新繪製更新地區),並向控制項發送繪製訊息。還可以使分配給該控制項的子控制項無效。
[C#] public void Invalidate(Region, bool);
樣本
[Visual Basic, C#, C++] 下面的樣本使使用者能夠將映像或影像檔拖到表單上,並使它在放置點顯示。每次繪製表單時,都重寫 OnPaint 方法以重新繪製映像;否則映像將保持到下一次重新繪製。DragEnter 事件處理方法決定拖到表單中的資料的類型,並提供適當的反饋。如果 Image 可以從該資料中建立,則 DragDrop 事件處理方法就會在該表單上顯示此映像。因為 DragEventArgs.X 和 DragEventArgs.Y 值為螢幕座標,所以樣本使用 PointToClient 方法將它們轉換成工作區座標。
[C#]
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;
}
}