home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Clip Drag and Drop / ImageDrop / ImageDrop.cs next >
Encoding:
Text File  |  2001-01-15  |  2.6 KB  |  85 lines

  1. //----------------------------------------
  2. // ImageDrop.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ImageDrop: ImageClip
  11. {
  12.      bool bIsTarget;
  13.  
  14.      public new static void Main()
  15.      {
  16.          Application.Run(new ImageDrop());
  17.      }
  18.      public ImageDrop()
  19.      {
  20.           Text = strProgName = "Image Drop";
  21.  
  22.           AllowDrop = true;
  23.      }
  24.      protected override void OnDragOver(DragEventArgs dea)
  25.      {
  26.           if (dea.Data.GetDataPresent(DataFormats.FileDrop) ||
  27.               dea.Data.GetDataPresent(typeof(Metafile))     ||
  28.               dea.Data.GetDataPresent(typeof(Bitmap)))
  29.           {
  30.                if ((dea.AllowedEffect & DragDropEffects.Move) != 0)
  31.                     dea.Effect = DragDropEffects.Move;
  32.  
  33.                if (((dea.AllowedEffect & DragDropEffects.Copy) != 0) &&
  34.                    ((dea.KeyState & 0x08) != 0))    // Ctrl key
  35.                     dea.Effect = DragDropEffects.Copy;
  36.           }
  37.      }
  38.      protected override void OnDragDrop(DragEventArgs dea)
  39.      {
  40.           if (dea.Data.GetDataPresent(DataFormats.FileDrop))
  41.           {
  42.                string[] astr = (string[]) 
  43.                                    dea.Data.GetData(DataFormats.FileDrop);
  44.                try
  45.                {
  46.                     image = Image.FromFile(astr[0]);
  47.                }
  48.                catch (Exception exc)
  49.                {
  50.                     MessageBox.Show(exc.Message, Text);
  51.                     return;
  52.                }
  53.                strFileName = astr[0];
  54.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  55.                Invalidate();
  56.           }
  57.           else 
  58.           {
  59.                if (dea.Data.GetDataPresent(typeof(Metafile)))
  60.                     image = (Image) dea.Data.GetData(typeof(Metafile));
  61.  
  62.                else if (dea.Data.GetDataPresent(typeof(Bitmap)))
  63.                     image = (Image) dea.Data.GetData(typeof(Bitmap));
  64.  
  65.                bIsTarget = true;
  66.                strFileName = "DragAndDrop";
  67.                Text = strProgName + " - " + strFileName;
  68.                Invalidate();
  69.           }
  70.      }
  71.      protected override void OnMouseDown(MouseEventArgs mea)
  72.      {
  73.           if (image != null)
  74.           {
  75.                bIsTarget = false;
  76.  
  77.                DragDropEffects dde = DoDragDrop(image, 
  78.                               DragDropEffects.Copy | DragDropEffects.Move);
  79.  
  80.                if (dde == DragDropEffects.Move && !bIsTarget)
  81.                     image = null;
  82.           }
  83.      }
  84. }
  85.