在ListView控件中启用拖放事件,如何判断拖放的是不是文件?
怎么获取此文件的属性信息?

解决方案 »

  1.   

    可以用来判断托放的数据类型 GetDataPresent  System.Windows.Forms.DragEventArgs.Data
    如:
    private void ListDragTarget_DragOver(object sender, System.Windows.Forms.DragEventArgs e) 
    {    // Determine whether string data exists in the drop data. If not, then
        // the drop effect reflects that the drop cannot occur.
        if (!e.Data.GetDataPresent(typeof(System.String))) {        e.Effect = DragDropEffects.None;
            DropLocationLabel.Text = "None - no string data.";
            return;
        }    // Set the effect based upon the KeyState.
        if ((e.KeyState & (8+32)) == (8+32) && 
            (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) {
            // KeyState 8 + 32 = CTL + ALT        // Link drag-and-drop effect.
            e.Effect = DragDropEffects.Link;    } else if ((e.KeyState & 32) == 32 && 
            (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) {        // ALT KeyState for link.
            e.Effect = DragDropEffects.Link;    } else if ((e.KeyState & 4) == 4 && 
            (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) {        // SHIFT KeyState for move.
            e.Effect = DragDropEffects.Move;    } else if ((e.KeyState & 8) == 8 && 
            (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) {        // CTL KeyState for copy.
            e.Effect = DragDropEffects.Copy;    } else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)  {        // By default, the drop action should be move, if allowed.
            e.Effect = DragDropEffects.Move;    } else
            e.Effect = DragDropEffects.None;
            
        // Get the index of the item the mouse is below.     // The mouse locations are relative to the screen, so they must be 
        // converted to client coordinates.    indexOfItemUnderMouseToDrop = 
            ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y)));    // Updates the label text.
        if (indexOfItemUnderMouseToDrop != ListBox.NoMatches){        DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
        } else
            DropLocationLabel.Text = "Drops at the end.";}
      

  2.   

    private void FromInfor_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
    string filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
    FileInfor fileInfo = new FileInfor(filePath);
    ...
    } private void FromInfor_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
    if (e.Data.GetDataPresent(DataFormats.FileDrop)){
    e.Effect = DragDropEffects.Link;
    }
    else {
     e.Effect = DragDropEffects.None;
    } }
      

  3.   

    我怎么在ListView中可以得到要拖放的对象?
    ListBox 有IndexFromPoint 方法,可在ListView 中没这样的方法,
    请高人赐教!!