拖放到表单上(但不是表单上的 WebBrowser 控件)

发布于 2024-10-08 04:04:46 字数 480 浏览 0 评论 0原文

我正在编写一个 Windows 窗体应用程序 (C#),它具有 WebBrowser 控件(以及其他一些按钮和文本框)。我希望能够将文件拖放到表单上的任何位置。问题是默认情况下 Web 浏览器将尝试渲染放入其控制中的任何文件;我不希望这样,因为我必须首先对文件执行一些预处理。 WebBrowser 控件提供了一个名为AllowWebBrowserDrop 的属性,我将其设置为 false 以禁用此行为。但是,结果是我无法在 WebBrowser 控件上放置任何内容(“不允许”反馈)。 WebBrowser 控件占据了大部分空间,因此如果您必须将文件放在空闲空间中的某个地方,那就有点蹩脚了。如何在不让 WebBrowser 控件尝试呈现文件的情况下将文件拖放到窗体上的任何位置?

我想我应该补充一点,我的表单上的AllowDrop 设置为true,并且有DragEnter 和DragDrop 的处理程序。我已将 WebBrowser 上的 AllowWebBrowserDrop 设置为 false。其他一切都有默认设置。

I'm writing a Windows Forms app (C#) that has a WebBrowser control (among a few other buttons and text boxes). I want to be able to drop a file anywhere on the form. The problem is that by default the WebBrowser will attempt to render any file dropped into its control; I don't want this as I have to perform some pre-processing on the file first. The WebBrowser control provides a property named AllowWebBrowserDrop, which I set to false to disable this behavior. However, the result is that I cannot drop anything on the WebBrowser control ("not allowed" feedback). The WebBrowser control takes up the majority of the space, so it would be kind of lame if you had to drop the file in the slack space somewhere. How can I enable dropping a file anywhere on my form without having the WebBrowser control try to render it?

I guess I should add that I have AllowDrop on my form set to true and have handlers for DragEnter and DragDrop. I have AllowWebBrowserDrop on my WebBrowser set to false. Everything else has the default settings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-10-15 04:04:46

根据您使用 WebBrowser 执行的操作,您可以处理 Navigating 事件,该事件在浏览器导航之前触发。然后通过检查 URL 来确定是否要处理丢弃。例如:

private void browser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (e.Url.IsFile)
    {
        // Prevent navigation
        e.Cancel = true;

        // Fire your other OnDrop code
    }
}

要使其正常工作,您需要将 AllowWebBrowserDrop 设置为 true

如果这适合您的业务案例,那就太好了;否则,您可能会陷入直接处理窗口消息的困境,如上所述,这并不有趣。

Depending on what you're doing with the WebBrowser, you can handle the Navigating event, which fires before the browser navigates. Then determine if you want to handle the drop by checking the URL. For example:

private void browser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    if (e.Url.IsFile)
    {
        // Prevent navigation
        e.Cancel = true;

        // Fire your other OnDrop code
    }
}

For this to work, you would want to leave AllowWebBrowserDrop set to true.

If this works for your business case, great; otherwise you're probably stuck handling window messages directly, as mentioned, which isn't much fun.

摇划花蜜的午后 2024-10-15 04:04:46

您能否添加一个具有比其上方的 WebBrowser 更高 z 顺序的透明背景色 Panel 并让该 Panel 处理放置?

Could you add a transparent background colored Panel with a higher z-order than the WebBrowser above it and let the Panel handle the drop?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文