C#中Invalidate() 方法

來源:互聯網
上載者:User
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;
   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.