글 작성자: 써니루루

.net Window form에서 컨트롤을 드래그 드롭하는 예제를 보면서 DragEnter와 DragDrop이벤트를 발췌해서 소개한다.


  /// <summary>
  /// The DragEnter event of the target control fires when the mouse enters
  /// a target control during a drag operation, and is used to determine if a drop
  /// will be allowed over this control.  This generally involves checking the type
  /// of data being dragged, the type of effects allowed (copy, move, etc.),
  /// and potentially the type and/or the specific instance of the source control that
  /// initiated the drag operation.
  ///
  /// This event will fire only if the AllowDrop property of the target control has
  /// been set to true.
  /// </summary>
  /// <param name="sender">The source of the event.</param>
  /// <param name="e">A DragEventArgs that contains the event data.</param>
  private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  {
   // Display some information about the DragDrop information in the
   // richTextBox1 control to show some of the information available.
   richTextBox1.Text = "Source Control: " + sourceControl.Name +
    "\r\nSource Control Type: " + sourceControl.GetType().Name +
    "\r\nAllowed Effect: " + e.AllowedEffect +
    "\r\nMouse Button: " + mouseButton.ToString() + "\r\n" +
    "\r\nAvailable Formats:\r\n";

   // Data may be available in more than one format, so loop through
   // all available formats and display them in richTextBox1.
   foreach (string availableFormat in e.Data.GetFormats(true))
   {
    richTextBox1.Text += "\t" + availableFormat + "\r\n";
   }

   // This control will use any dropped data to add items to the listbox.
   // Therefore, only data in a text format will be allowed.  Setting the
   // autoConvert parameter to true specifies that any data that can be
   // converted to a text format is also acceptable.
   if (e.Data.GetDataPresent(DataFormats.Text, true))
   {
    // Some controls in this sample allow both Copy and Move effects.
    // If a Move effect is allowed, this implementation assumes a Move
    // effect unless the CTRL key was pressed, in which case a Copy
    // effect is assumed.  This follows standard DragDrop conventions.
    if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move && (e.KeyState & ctrlKey) != ctrlKey)
    {
     // Show the standard Move icon.
     e.Effect = DragDropEffects.Move;
    }
    else
    {
     // Show the standard Copy icon.
     e.Effect = DragDropEffects.Copy;
    }
   }
  }

  /// <summary>
  /// The DragDrop event of the target control fires when a drop actually occurs over
  /// the target control.  This is where the data being dragged is actually processed.
  ///
  /// This event will fire only if the AllowDrop property of the target control has
  /// been set to true.
  /// </summary>
  /// <param name="sender">The source of the event.</param>
  /// <param name="e">A DragEventArgs that contains the event data.</param>
  private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  {

   // Store the data as a string so that it can be accessed from the
   // mnuCopy and mnuMove click events.
   sourceData = e.Data.GetData(DataFormats.Text, true).ToString();

   // If the right mouse button was used, provide a context menu to allow
   // the user to select a DragDrop effect.  The mouseButton is recorded in the
   // MouseDown event of the source control.
   if (mouseButton == MouseButtons.Right)
   {
    // Show a context menu, asking which operation to perform.
    // The ProcessData() call is then made in the click event
    // of the mnuCopy and mnuMove menu items.  Show only those
    // menu items that correspond to an allowed effect.
    mnuCopy.Visible = ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy);
    mnuMove.Visible = ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move);
    contextMenu1.Show(listBox1, new Point(20,20));
   }
   else
   {
    // Set the deleteSource member field based on the Effect.
    // The Effect is preset in the DragEnter event handler.
    deleteSource = (e.Effect == DragDropEffects.Move);

    // The processing of the data is done in a separate call, since
    // this is also called from the click event of the contextMenu1 items.
    ProcessData();

   }
  }