为什么 DragDrop 在 VS2010 下不起作用? [英] Why is DragDrop not working under VS2010?

查看:14
本文介绍了为什么 DragDrop 在 VS2010 下不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 UserControl 的 winforms 应用程序.用户控件的工作是收集用户从 Windows 资源管理器中放置的文件,打开文件,确定类型并相应地处理它.

I have a winforms app that uses a UserControl. The user control's job is to collect a file that the user drops on it from Windows Explorer, Open the file, determine the type and handle it accordingly.

此控件在 Visual Studio 2008 Pro 下完美运行.我升级到VS 2010 Pro,现在不行了.是否有我应该注意的标志或属性发生了变化??

This control worked PERFECTLY under Visual Studio 2008 Pro. I upgraded to VS 2010 Pro, and now, it doesn't work. Is there a flag or a property that has changed that I should be aware of??

我做了一个快速演示来测试.这个演示在 2008 年下完美运行,但在 2010 年下根本无法运行.

I made a quick demo to test. This demo works perfectly under 2008, but doesn't work at all under 2010.

设置:创建一个新的 winform 项目.添加用户控件.在用户控件的代码部分中设置以下代码.(编译以使用户控件出现在工具箱中)将用户控件添加到表单中.运行程序,然后将任何文件从窗口拖到窗体上.如果有效,用户控制区域应该改变颜色.

The setup: Create a new winform project. Add a user control. Set the following code in the user control's code section. (compile to get the user control to appear in the toolbox) Add the user control to the form. Run the program, and drag ANY file from windows onto the form. If it works, the user control area should change colors.

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        this.AllowDrop = true;
        this.DragDrop += new DragEventHandler(UserControl1_DragDrop);
        this.DragEnter += new DragEventHandler(UserControl1_DragEnter);
        this.DragLeave += new EventHandler(UserControl1_DragLeave);
    }

    void UserControl1_DragLeave(object sender, EventArgs e)
    {
        this.BackColor = Color.FromName("Control");
    }

    void UserControl1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
            this.BackColor = Color.Blue;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    void UserControl1_DragDrop(object sender, DragEventArgs e)
    {
        this.BackColor = Color.Yellow;
    }
}

我愿意接受你们可能想到的任何解释或修复!

I'm open to any explanation or fix that you guys may think up!

更新:

我使用下面列出的评论进行了测试.仍然不起作用.但是,我注意到它只会在开发环境中失败.当我转到 bin 目录并手动启动程序时,它工作正常.当我在开发环境中时它不起作用,这使得调试有点困难.仍在寻找全局修复.

I tested using the comments listed below. STILL doesn't work. However, I have noted that it only fails while in the development environment. When I go to the bin directory and launch the program manually, it works fine. It just doesn't work when I am in the development environment, which makes debugging a bit difficult. Still looking for the big-picture fix.

推荐答案

这里一个可能的故障原因是 UIPI,UAC 的用户界面组件.您不能从非提升的进程拖放到提升的进程拥有的窗口.当您从打开兼容性"选项卡中的以管理员身份运行此程序"选项的快捷方式启动 Visual Studio 时,您将触发此操作.唯一的解决方法是关闭该选项.或者如您所见,直接从 .exe 文件运行它.

A likely failure cause here is UIPI, the user interface component of UAC. You cannot drag from a non-elevated process and drop to a window owned by an elevated process. You'll trigger this when you started Visual Studio from a shortcut that has the "Run this program as an administrator" option in the Compatibility tab turned on. The only workaround is to turn that option off. Or to run it directly from the .exe file, as you discovered.

这篇关于为什么 DragDrop 在 VS2010 下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆