WPF怎么实现文件拖放功能winform怎么实现拖拽功能

news/2024/7/8 3:57:03 标签: wpf, winform

WPF怎么实现文件拖放功能winform怎么实现文件拖拽功能,在管理员模式下wpf winform怎么实现文件的拖拽功能

WPF实现文件拖放功能,正常情况并没有什么问题,但是如果你的程序使用管理员身份启动,你就会发现文件拖放功能就会失效。同样winform使用管理员身份启动,你就会发现文件拖放功能就会失效

解决一,管理员不给拖拽,就吧管理员启动kill掉

方案一:让你的程序使用非管理员启动,程序中需要管理员身份的操作,一般为涉及到注册表操作或驱动操作,可以考虑将这部分操作放到一个服务里单独操作,可以理解为程序分成服务与应用程序两块,需要管理员身份操作的

功能部分放到服务里实现,界面相关的操作在应用程序里实现。

这种方案也能解决,并且问题解决的比较彻底,但是项目工程量比较大的情况下,工作量就比较大了,为一个文件拖放的功能,增加了较大的工作量,得不偿失。

方案二:将需要管理员权限启动的部分做成另一个单独的exe程序,在主程序中用调用,可参考C#默认以管理员身份运行程序_刘水镜的博客-CSDN博客

方案三:提供一个折中的办法,WPF经过我较长的上网搜索及研究,没有找到合适的办法解决这个问题,但是 WinForm 通过消息Hook却能实现,所以这个折中的办法就是WPF+WinForm来解决这个问题。

主要方法详解:

下面我们将主要讲解如何使用 WPF+WinForm 解决WPF程序使用管理员身份启动后不能拖放文件的问题。

第一部分:使用 WinForm 解决使用不能拖动的问题,关键代码如下

ElevatedDragDropManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows;

public class ElevatedDragDropManager : IMessageFilter
{
    #region "P/Invoke"
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint msg, ChangeWindowMessageFilterExAction action, ref CHANGEFILTERSTRUCT changeInfo);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ChangeWindowMessageFilter(uint msg, ChangeWindowMessageFilterFlags flags);

    [DllImport("shell32.dll")]
    private static extern void DragAcceptFiles(IntPtr hwnd, bool fAccept);

    [DllImport("shell32.dll")]
    private static extern uint DragQueryFile(IntPtr hDrop, uint iFile, [Out()]
StringBuilder lpszFile, uint cch);

    [DllImport("shell32.dll")]
    private static extern bool DragQueryPoint(IntPtr hDrop, ref POINT lppt);

    [DllImport("shell32.dll")]
    private static extern void DragFinish(IntPtr hDrop);

    [StructLayout(LayoutKind.Sequential)]
    private struct POINT
    {
        public int X;

        public int Y;
        public POINT(int newX, int newY)
        {
            X = newX;
            Y = newY;
        }

        public static implicit operator System.Drawing.Point(POINT p)
        {
            return new System.Drawing.Point(p.X, p.Y);
        }

        public static implicit operator POINT(System.Drawing.Point p)
        {
            return new POINT(p.X, p.Y);
        }
    }

    private enum MessageFilterInfo : uint
    {
        None,
        AlreadyAllowed,
        AlreadyDisAllowed,
        AllowedHigher
    }

    private enum ChangeWindowMessageFilterExAction : uint
    {
        Reset,
        Allow,
        Disallow
    }

    private enum ChangeWindowMessageFilterFlags : uint
    {
        Add = 1,
        Remove = 2
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct CHANGEFILTERSTRUCT
    {
        public uint cbSize;
        public MessageFilterInfo ExtStatus;
    }
    #endregion

    public static ElevatedDragDropManager Instance = new ElevatedDragDropManager();
    public event EventHandler<ElevatedDragDropArgs> ElevatedDragDrop;

    private const uint WM_DROPFILES = 0x233;
    private const uint WM_COPYDATA = 0x4a;

    private const uint WM_COPYGLOBALDATA = 0x49;
    private readonly bool IsVistaOrHigher = Environment.OSVersion.Version.Major >= 6;

    private readonly bool Is7OrHigher = (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major > 6;

    public void EnableDragDrop(IntPtr hWnd)
    {
        if (Is7OrHigher)
        {
            CHANGEFILTERSTRUCT changeStruct = new CHANGEFILTERSTRUCT();
            changeStruct.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT)));
            ChangeWindowMessageFilterEx(hWnd, WM_DROPFILES, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);
            ChangeWindowMessageFilterEx(hWnd, WM_COPYDATA, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);
            ChangeWindowMessageFilterEx(hWnd, WM_COPYGLOBALDATA, ChangeWindowMessageFilterExAction.Allow, ref changeStruct);
        }
        else if (IsVistaOrHigher)
        {
            ChangeWindowMessageFilter(WM_DROPFILES, ChangeWindowMessageFilterFlags.Add);
            ChangeWindowMessageFilter(WM_COPYDATA, ChangeWindowMessageFilterFlags.Add);
            ChangeWindowMessageFilter(WM_COPYGLOBALDATA, ChangeWindowMessageFilterFlags.Add);
        }

        DragAcceptFiles(hWnd, true);
    }

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_DROPFILES)
        {
            HandleDragDropMessage(m);
            return true;
        }
        return false;
    }

    private void HandleDragDropMessage(Message m)
    {
        dynamic sb = new StringBuilder(260);
        uint numFiles = DragQueryFile(m.WParam, 0xffffffffu, sb, 0);
        dynamic list = new List<string>();

        for (uint i = 0; i <= numFiles - 1; i++)
        {
            if (DragQueryFile(m.WParam, i, sb, Convert.ToUInt32(sb.Capacity) * 2) > 0)
            {
                list.Add(sb.ToString());
            }
        }

        POINT p = default(POINT);
        DragQueryPoint(m.WParam, ref p);
        DragFinish(m.WParam);

        dynamic args = new ElevatedDragDropArgs();
        args.HWnd = m.HWnd;
        args.Files = list;
        args.X = p.X;
        args.Y = p.Y;

        if (ElevatedDragDrop != null)
        {
            ElevatedDragDrop(this, args);
        }
    }
}

public class ElevatedDragDropArgs : EventArgs
{
    public IntPtr HWnd
    {
        get { return m_HWnd; }
        set { m_HWnd = value; }
    }
    private IntPtr m_HWnd;
    public List<string> Files
    {
        get { return m_Files; }
        set { m_Files = value; }
    }
    private List<string> m_Files;
    public int X
    {
        get { return m_X; }
        set { m_X = value; }
    }
    private int m_X;
    public int Y
    {
        get { return m_Y; }
        set { m_Y = value; }
    }

    private int m_Y;
    public ElevatedDragDropArgs()
    {
        Files = new List<string>();
    }
}

Form1.cs

注:需要将Form1窗口的AllowDrop属性设置为false,否则无法拖动文件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileDragDrop
{
    public partial class FileDragDrop : Form
    {
        public FileDragDrop()
        {
            InitializeComponent();
            //this.AllowDrop设置为false
            this.AllowDrop = false;
            ElevatedDragDropManager filter = new ElevatedDragDropManager();
            //开启拖放功能
            filter.EnableDragDrop(this.Handle);
            //添加消息过滤器
            Application.AddMessageFilter(filter);
            //设置拖放结束回调
            filter.ElevatedDragDrop += this.ElevatedDragDrop;
        }

        //拖放结束事件
        private void ElevatedDragDrop(System.Object sender, ElevatedDragDropArgs e)
        {
            try
            {
                if (e.HWnd == this.Handle)
                {
                    foreach (string file in e.Files)
                    {
                        //拖动文件
                        MessageBox.Show("ElevatedDragDrop File=" + (file) + "!");
                    }
                }
            }
            catch (Exception ex)
            {
                //异常信息
                MessageBox.Show("ElevatedDragDrop error=" + (ex.TargetSite?.Name) + "!");
            }
        }
    }
}

最终的效果:

 

WinForm项目代码链接:

GitHub - zhaobangyu/C-SHAP at WinForm

第二部分:WPF+WinForm的组合使用

效果图:

 ​WPF+WinForm项目代码链接:

https://github.com/zhaobangyu/C-SHAP/tree/WPF/FileDragDrop

补充

这个直接使用没问题。但如果关闭这个页面重新打开就会有问题。这个拖拽事件一直绑定的第一次创建时的handle。

原因是要在关闭的时候重新释放消息过滤器

 借鉴自https://www.cnblogs.com/whr2071/p/15922643.html

大体的报错范围是,在父窗体中进行释放,可以新建接收拖放的窗口,但窗口句柄与接收放置的句柄匹配不上,触发不了后续事件。在子窗体中进行释放,父窗体中的操作会直接报错“无法访问已释放的对象”。

最后,当我放弃第一个链接的方法去寻找其他解决办法时,看到了第二个链接。发现我没有释放消息过滤器,也就是

Application.RemoveMessageFilter(this);

当我把这个加到我的工程中,一切问题就解决了。

 WPF怎么实现文件拖放功能winform怎么实现文件拖拽功能,在管理员模式下wpf winform怎么实现文件的拖拽功能

 


http://www.niftyadmin.cn/n/4972836.html

相关文章

自定义date工具类 DateUtils.java

自定义date工具类 DateUtils.java 简介 Date日期类型的工具类。 api 日期格式化 format(Date date)&#xff1b;日期格式化 format(Date date, String pattern)&#xff1b;计算距离现在多久&#xff0c;非精确 getTimeBefore(Date date);计算距离现在多久&#xff0c;精确…

MTK6833_MT6833核心板_天玑700安卓5G核心板规格性能介绍

MTK6833安卓核心板采用台积电 7nm 制程的5G SoC&#xff0c;2*Cortex-A766*Cortex-A55架构&#xff0c;搭载Android12.0操作系统&#xff0c;主频最高达2.2GHz 。内置 5G 双载波聚合技术&#xff08;2CC&#xff09;及双 5G SIM 卡功能&#xff0c;实现优异的功耗表现及实时连网…

Matlab图像处理运算方法-点运算

点运算 点运算又称为对比度增强、对比度拉伸或灰度变换&#xff0c;是一种通过图像中的每一个像素值&#xff08;即像素点上的灰度值&#xff09;进行运算的图像处理方式。它将输入图像映射为输出图像&#xff0c;输出图像每个像素点的灰度值仅由对应的输入像素点的灰度值决定…

InCopy软件安装包分享(附安装教程)

目录 一、软件简介 二、软件下载 一、软件简介 InCopy是一款由Adobe公司开发的文字处理软件&#xff0c;它被广泛应用于出版行业和印刷行业&#xff0c;用于编辑和排版文字内容。InCopy与Adobe的另一款软件InDesign紧密集成&#xff0c;共同构成了专业的排版工作流程。 以下…

【MySQL系列】表的内连接和外连接学习

「前言」文章内容大致是对MySQL表的内连接和外连接。 「归属专栏」MySQL 「主页链接」个人主页 「笔者」枫叶先生(fy) 目录 一、内连接二、外连接2.1 左外连接2.2 右外连接 一、内连接 内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选&#xff0c;前面篇章学习的…

win11+vmware17+centos7.9环境搭建

温故知新 &#x1f4da;第一章 前言&#x1f4d7;背景&#x1f4d7;目标&#x1f4d7;总体方向 &#x1f4da;第二章 安装部署环境&#x1f4d7;安装VMware Workstation 17 Pro软件&#x1f4d7;安装CentOS-7虚拟机&#x1f4d5;镜像下载地址&#x1f4d5;创建虚拟机&#x1f4…

Python 阿里云盾滑块验证

&#xfeff;<table><tr><td bgcolororange>本文仅供学习交流使用&#xff0c;如侵立删&#xff01;</td></tr></table> 记一次阿里云盾滑块验证分析并通过 操作环境 win10 、 macPython3.9selenium、pyautogui 分析 最近在做中国庭审…

西北大学计算机考研844高分经验分享

西北大学计算机考研844经验分享 个人介绍 ​ 本人是西北大学22级软件工程研究生&#xff0c;考研专业课129分&#xff0c;过去一年里在各大辅导机构任职&#xff0c;辅导考研学生专业课844&#xff0c;辅导总时长达288小时&#xff0c;帮助多名学生专业课高分上岸。 前情回顾…