Invalidate 重绘控件

Invalidate
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Threading;
 11 
 12 namespace tuxing
 13 {
 14     public partial class Form1 : Form
 15     {
 16         private Image picture;
 17         private Point pictureLocation;
 18 
 19         public Form1()
 20         {
 21             InitializeComponent();
 22             // Enable drag-and-drop operations and 
 23             // add handlers for DragEnter and DragDrop.
 24             this.AllowDrop = true;
 25             this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
 26             this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
 27         }
 28 
 29         private void Form1_DragDrop(object sender, DragEventArgs e)
 30         {
 31              //Handle FileDrop data.
 32             if (e.Data.GetDataPresent(DataFormats.FileDrop))
 33             {
 34                 // Assign the file names to a string array, in 
 35                 // case the user has selected multiple files.
 36                 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
 37                 //这里一定要是字符串数组。为什么?我不知道啊。。。求解
 38                 try
 39                 {
 40                     // Assign the first image to the picture variable.
 41                     this.picture = Image.FromFile(files[0]);
 42                     // Set the picture location equal to the drop point.
 43                     this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
 44                     //PointToClient()将指定屏幕点的位置计算成工作区坐标
 45                 }
 46                 catch (Exception ex)
 47                 {
 48                     MessageBox.Show(ex.Message);
 49                     return;
 50                 }
 51             }
 52             
 53             // Handle Bitmap data.
 54             if (e.Data.GetDataPresent(DataFormats.Bitmap))
 55             {
 56                 try
 57                 {
 58                     // Create an Image and assign it to the picture variable.
 59                     this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
 60                     //是不是这样,可以用GetData中数据中分别抽出地址,位图数据……
 61                     // Set the picture location equal to the drop point.
 62                     this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
 63                 }
 64                 catch (Exception ex)
 65                 {
 66                     MessageBox.Show(ex.Message);
 67                     return;
 68                 }
 69             }
 70             // Force the form to be redrawn with the image.
 71             this.Invalidate();
 72         }
 73 
 74         private void Form1_DragEnter(object sender, DragEventArgs e)
 75         {
 76             // If the data is a file or a bitmap, display the copy cursor.
 77             if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
 78                e.Data.GetDataPresent(DataFormats.FileDrop))
 79                 //GetDataPresent(),确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定格式
 80             {
 81                 e.Effect = DragDropEffects.Copy;
 82             }
 83             else
 84             {
 85                 e.Effect = DragDropEffects.None;
 86             }
 87         }
 88 
 89         protected override void OnPaint(PaintEventArgs e)
 90         {
 91             // If there is an image and it has a location, 
 92             // paint it when the Form is repainted.
 93             base.OnPaint(e);
 94             if (this.picture != null && this.pictureLocation != Point.Empty)
 95             {
 96                 e.Graphics.DrawImage(this.picture, this.pictureLocation);
 97             }
 98         }
 99     }
100 }

 

posted on 2013-04-27 14:31  laymond  阅读(233)  评论(0编辑  收藏  举报

导航