更改WPF中的鼠标拖动光标

本文关键字:拖动 光标 鼠标 WPF 更改 | 更新日期: 2023-09-27 18:08:31

当将文档从文件系统拖动到表单时,我必须显示拖动的文件数。

我已经完成了下面的代码,但是我不能改变拖动光标。请告诉我最好的方法。

    private void tbDisplayFileContents_PreviewDragOver(object sender, DragEventArgs args)
    {
       if (IsSingleFile(args) != null)
        {
              tbDisplayFileContents_PreviewDrop(sender, args);
        }
        else
        {
          //  args.Effects = DragDropEffects.None;
        }
        Mouse.SetCursor(Cursors.Hand);
        Icon ico = new Icon(string.Concat("1365516094_10371.ico"));
        tbDisplayFileContents.Cursor = GenerateCursor.CreateCursor(ico, true, new System.Drawing.Color());
        args.Handled = true;
    }
    private void tbDisplayFileContents_PreviewDrop(object sender, DragEventArgs args)
    {
        args.Handled = true;
        string files = string.Empty;
        string[] fileName = IsSingleFile(args);
        if (fileName == null) return;
        isDrag = true;
        DoEvents();
        for (int i = 0; i < fileName.Length; i++)
        {
            if (i == 0)
            {
                files = string.Concat("1] ", fileName[i]);
            }
            else
            {
                int j = i + 1;
                files = string.Concat(files, Environment.NewLine, j, "] ", fileName[i]);
            }
        }
        lblfileName.Content = files;
    }

    private string[] IsSingleFile(DragEventArgs args)
    {
        if (args.Data.GetDataPresent(DataFormats.FileDrop, true))
        {
            string[] fileNames = args.Data.GetData(DataFormats.FileDrop, true) as string[];
            if (fileNames.Length != 0)
            {
                if (File.Exists(fileNames[0]))
                {
                    // At this point we know there is a single file.
                    return fileNames;
                }
            }
        }
        return null;
    }
    #endregion
    #region -------Events--------
    private void btnClear_Click(object sender, RoutedEventArgs e)
    {
        lblfileName.Content = string.Empty;
    }
    #endregion
    private void tbDisplayFileContents_PreviewDragEnter(object sender, DragEventArgs e)
    {
        e.Effects = DragDropEffects.None;
    }
    public static void DoEvents()
    {
        Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                              new Action(delegate
        {
            Icon ico = new Icon(string.Concat("1365516094_10371.ico"));
            Mouse.OverrideCursor = GenerateCursor.CreateCursor(ico, true, new System.Drawing.Color());
        }));
    }

我使用了如下的GiveFeedBack事件

<>之前private void tbDisplayFileContents_GiveFeedback(对象发送者,GiveFeedbackEventArgs e){if (e.Effects == dragdropeeffects . copy){e.UseDefaultCursors = false;//Mouse.SetCursor (Cursors.Hand);Icon ico = new Icon(string.Concat("1365516094_10371.ico"));//鼠标。Cursor = GenerateCursor。CreateCursor(ico, true, new System.Drawing.Color());Mouse.SetCursor (GenerateCursor。CreateCursor(ico, true, new System.Drawing.Color()));}其他的e.UseDefaultCursors = true;e.Handled = true;}之前

它可以用于表单到表单的拖动,但它不适用于从外部表单拖动的内容(文件),例如来自桌面的文件。

更改WPF中的鼠标拖动光标

我错过了代码中的GiveFeedback事件,它用于在拖放操作时修改鼠标光标。