我们在Winform支持网页通常增加WebBrowser控件实现,相当于内嵌浏览器浏览网页使用,

而此WebBrowser默认情况是文件拖入功能是不支持的,

如何才能支持呢。在这里介绍如何实现方法

一.直接上源码吧,

下载后直接用,其实不要了解太深入,会用就行了啦(用之前,网页需有加入JS drop功能)

下载地址   http://pcbren.cn/ShareFiles/WebbrowserDemoDrop.zip

二.实现拖拽是重构WebBrowser浏览器Drag事件,让浏览器支持拖拽功能,以下为部份代码:

    public partial class ExtendedWebBrowser : System.Windows.Forms.WebBrowser, MsHtmHstInterop.IDocHostUIHandler, MsHtmHstInterop.IDropTarget, IEInterface.IWebBrowser2
{ public event Events.DragEnterEventHander ExDragEnter;
private Events.DragEnterEventArgs dragEnterEventArgs;
public event Events.DragOverEventHander ExDragOver;
private Events.DragOverEventArgs dragOverEventArgs;
public event Events.DragLeaveEventHander ExDragLeave;
public event Events.DragDropEventHander ExDragDrop;
private Events.DragDropEventArgs dragDropEventArgs;
private bool registerAsDropTarget = true;
private bool registerAsBrowser = true;
private bool slient = true;
MsHtmHstInterop.IDropTarget pDropTarget;//保存原有的拖放对象
private bool isDragToInputBox = false;//判断是否是在向输入框中拖放 public ExtendedWebBrowser()
{
InitializeComponent();
this.AllowWebBrowserDrop = true;
Navigate("about:blank");
MsHtmHstInterop.ICustomDoc idoc = this.Document.DomDocument as MsHtmHstInterop.ICustomDoc;
idoc.SetUIHandler(this as MsHtmHstInterop.IDocHostUIHandler);
dragEnterEventArgs = new Events.DragEnterEventArgs();
dragOverEventArgs = new Events.DragOverEventArgs();
dragDropEventArgs = new Events.DragDropEventArgs();
} #region 获取新窗口Url
AxHost.ConnectionPointCookie cookie;
WebBrowserExtendedEvents events; //This method will be called to give you a chance to create your own event sink
protected override void CreateSink()
{
//MAKE SURE TO CALL THE BASE or the normal events won't fire
base.CreateSink();
events = new WebBrowserExtendedEvents(this);
cookie = new AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(LuckyPang.ControlLibrary.IEInterface.DWebBrowserEvents2));
} protected override void DetachSink()
{
if (null != cookie)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
} //This new event will fire when the page is navigating
public event EventHandler<WebBrowserExtendedNavigatingEventArgs> ExtendedNavigating; protected void OnExtendedNavigating(string url, string frame, out bool cancel)
{
EventHandler<WebBrowserExtendedNavigatingEventArgs> h = ExtendedNavigating;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
} public event EventHandler<WebBrowserExtendedNewWindowEventArgs> ExtendedNewWindow; protected void OnExtendedNewWindow(string url,out bool cancel)
{
EventHandler<WebBrowserExtendedNewWindowEventArgs> h = ExtendedNewWindow;
WebBrowserExtendedNewWindowEventArgs args = new WebBrowserExtendedNewWindowEventArgs(url);
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
} public event EventHandler<Events.WebBrowserExtendedWindowClosingEventArgs> ExtendedWindowClosing;
protected void OnExtendWindowClosing(out bool cancel)
{
EventHandler<Events.WebBrowserExtendedWindowClosingEventArgs> h = ExtendedWindowClosing;
Events.WebBrowserExtendedWindowClosingEventArgs args = new Events.WebBrowserExtendedWindowClosingEventArgs();
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
} //This class will capture events from the WebBrowser
class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, LuckyPang.ControlLibrary.IEInterface.DWebBrowserEvents2
{
ExtendedWebBrowser _Browser;
private bool m_bPop;
public WebBrowserExtendedEvents(ExtendedWebBrowser browser)
{ _Browser = browser; } //Implement whichever events you wish
public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
_Browser.OnExtendedNavigating((string)URL, (string)targetFrameName, out cancel);
} public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
_Browser.OnExtendedNewWindow(bstrUrl, out Cancel);
} #region DWebBrowserEvents2 成员 public void StatusTextChange(string Text)
{
throw new NotImplementedException();
} public void ProgressChange(int Progress, int ProgressMax)
{
throw new NotImplementedException();
} public void CommandStateChange(int Command, bool Enable)
{
throw new NotImplementedException();
} public void DownloadBegin()
{
m_bPop = false;
} public void DownloadComplete()
{
m_bPop = true;
} public void TitleChange(string Text)
{
throw new NotImplementedException();
} public void PropertyChange(string szProperty)
{
throw new NotImplementedException();
} public void NewWindow2(ref object ppDisp, ref bool Cancel)
{
Cancel = m_bPop;
} public void NavigateComplete2(object pDisp, ref object URL)
{
throw new NotImplementedException();
} public void DocumentComplete(object pDisp, ref object URL)
{
throw new NotImplementedException();
} public void OnQuit()
{
throw new NotImplementedException();
} public void OnVisible(bool Visible)
{
throw new NotImplementedException();
} public void OnToolBar(bool ToolBar)
{
throw new NotImplementedException();
} public void OnMenuBar(bool MenuBar)
{
throw new NotImplementedException();
} public void OnStatusBar(bool StatusBar)
{
throw new NotImplementedException();
} public void OnFullScreen(bool FullScreen)
{
throw new NotImplementedException();
} public void OnTheaterMode(bool TheaterMode)
{
throw new NotImplementedException();
} public void WindowSetResizable(bool Resizable)
{
throw new NotImplementedException();
} public void WindowSetLeft(int Left)
{
throw new NotImplementedException();
} public void WindowSetTop(int Top)
{
throw new NotImplementedException();
} public void WindowSetWidth(int Width)
{
throw new NotImplementedException();
} public void WindowSetHeight(int Height)
{
throw new NotImplementedException();
} public void WindowClosing(bool IsChildWindow, ref bool Cancel)
{
_Browser.OnExtendWindowClosing(out Cancel);
} public void ClientToHostWindow(ref int CX, ref int CY)
{
throw new NotImplementedException();
} public void SetSecureLockIcon(int SecureLockIcon)
{
throw new NotImplementedException();
} public void FileDownload(ref bool Cancel)
{
throw new NotImplementedException();
} public void NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
throw new NotImplementedException();
} public void PrintTemplateInstantiation(object pDisp)
{
throw new NotImplementedException();
} public void PrintTemplateTeardown(object pDisp)
{
throw new NotImplementedException();
} public void UpdatePageStatus(object pDisp, ref object nPage, ref object fDone)
{
throw new NotImplementedException();
} public void PrivacyImpactedStateChange(bool bImpacted)
{
throw new NotImplementedException();
} #endregion
}
#endregion #region IDocHostUIHandler 成员 void MsHtmHstInterop.IDocHostUIHandler.EnableModeless(int fEnable)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.FilterDataObject(MsHtmHstInterop.IDataObject pDO, out MsHtmHstInterop.IDataObject ppDORet)
{
ppDORet = pDO;
} void MsHtmHstInterop.IDocHostUIHandler.GetDropTarget(MsHtmHstInterop.IDropTarget pDropTarget, out MsHtmHstInterop.IDropTarget ppDropTarget)
{
this.pDropTarget = pDropTarget;//保存默认的对象
ppDropTarget = this as MsHtmHstInterop.IDropTarget ;//把当前对角注册为拖放对象
} void MsHtmHstInterop.IDocHostUIHandler.GetExternal(out object ppDispatch)
{
if (this.ObjectForScripting != null)
{
ppDispatch = this.ObjectForScripting;
}
else
{
ppDispatch = null;
}
} void MsHtmHstInterop.IDocHostUIHandler.GetHostInfo(ref MsHtmHstInterop._DOCHOSTUIINFO pInfo)
{
pInfo.dwFlags = 0x00040000;
} void MsHtmHstInterop.IDocHostUIHandler.GetOptionKeyPath(out string pchKey, uint dw)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.HideUI()
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.OnDocWindowActivate(int fActivate)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.OnFrameWindowActivate(int fActivate)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.ResizeBorder(ref MsHtmHstInterop.tagRECT prcBorder, MsHtmHstInterop.IOleInPlaceUIWindow pUIWindow, int fRameWindow)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.ShowContextMenu(uint dwID, ref MsHtmHstInterop.tagPOINT ppt, object pcmdtReserved, object pdispReserved)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.ShowUI(uint dwID, MsHtmHstInterop.IOleInPlaceActiveObject pActiveObject, MsHtmHstInterop.IOleCommandTarget pCommandTarget, MsHtmHstInterop.IOleInPlaceFrame pFrame, MsHtmHstInterop.IOleInPlaceUIWindow pDoc)
{
if (pActiveObject != null)
{
pActiveObject.GetWindow((IntPtr ));
}
} void MsHtmHstInterop.IDocHostUIHandler.TranslateAccelerator(ref MsHtmHstInterop.tagMSG lpmsg, ref Guid pguidCmdGroup, uint nCmdID)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.TranslateUrl(uint dwTranslate, ref ushort pchURLIn, IntPtr ppchURLOut)
{
throw new NotImplementedException();
} void MsHtmHstInterop.IDocHostUIHandler.UpdateUI()
{
throw new NotImplementedException();
} #endregion #region IDropTarget 成员 public new void DragEnter(MsHtmHstInterop.IDataObject pDataObj, uint grfKeyState, MsHtmHstInterop._POINTL pt, ref uint pdwEffect)
{
pDropTarget.DragEnter(pDataObj, grfKeyState, pt, ref pdwEffect);//调用默认方法
//获取拖动的参数
if (ExDragEnter != null)
{
DataObject dobj = null;
if (pDataObj != null)
{
dobj = new DataObject(pDataObj);//拖动数据
Point p = new Point(pt.x, pt.y);//鼠标在容器上的位置
dragEnterEventArgs.SetParameters(dobj, grfKeyState, p, pdwEffect);
ExDragEnter(this, dragEnterEventArgs);//此处为自定义事件
if (dragEnterEventArgs.handled)//自定义事件的返回值
{
pdwEffect = dragEnterEventArgs.pdwEffect;
}
}
}
} public new void DragLeave()
{
pDropTarget.DragLeave();//调用默认的方法
if (ExDragLeave != null)
{
ExDragLeave(this);//自定义事件
}
} public new void DragOver(uint grfKeyState, MsHtmHstInterop._POINTL pt, ref uint pdwEffect)
{
uint temp = pdwEffect;//保存原有的拖放效果,当调用默认的
//操作时此参数会改变,并根据此来判
//断是否向输入框中拖放 pDropTarget.DragOver(grfKeyState, pt, ref pdwEffect);//调用默认方法
if (pdwEffect > )//如果值变为了零,那么是向输入框中拖放了
{
isDragToInputBox = true;//标记设为true
temp = ;
}
else
{
isDragToInputBox = false;//否则标记设为false
pdwEffect = temp;
}
if (ExDragOver != null)//自定义事件处理
{
Point p = new Point(pt.x, pt.y);
dragOverEventArgs.SetParameters(grfKeyState, p, temp);
if (dragOverEventArgs.handled)
{
pdwEffect = dragOverEventArgs.pdwEffect;
}
}
} public void Drop(MsHtmHstInterop.IDataObject pDataObj, uint grfKeyState, MsHtmHstInterop._POINTL pt, ref uint pdwEffect)
{
if (ExDragDrop != null)//自定义事件处理
{
if (pDataObj != null && !isDragToInputBox )//如果没有向输入框中拖放则触发此事件
//否则按默认处理
{
DataObject dobj = new DataObject(pDataObj);
Point p = new Point(pt.x, pt.y);
dragDropEventArgs.SetParameters(dobj, grfKeyState, p, pdwEffect);
ExDragDrop(this, dragDropEventArgs);
if (dragDropEventArgs.handled)
{
pdwEffect = dragDropEventArgs.pdwEffect;
}
}
}
pDropTarget.Drop(pDataObj, grfKeyState, pt, ref pdwEffect);//调用默认方法
} #endregion #region IWebBrowser2 成员 public bool RegisterAsDropTarget
{
get
{
return registerAsDropTarget;
}
set
{
registerAsDropTarget = value;
}
}
public bool Silent
{
get
{
return slient;
}
set
{
slient = value;
}
} public bool RegisterAsBrowser
{
get
{
return registerAsBrowser;
}
set
{
registerAsBrowser = value;
}
}
#endregion
}

三.拖拽界面展示:

PCB Winform中的WebBrowser扩展拖放(拖拽)功能 实现方法的更多相关文章

  1. JQuery UI的拖拽功能实现方法小结

    JQuery UI提供的API极大简化了拖拽功能的开发.只需要分别在拖拽源(source)和目标(target)上调用draggable和droppable两个函数即可. 拖拽原理 首先要明确几个概念 ...

  2. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  3. WinForm中嵌入WebBrowser,并且支持C#和JS方法的相互调用

    纯粹WinForm界面不够友好,实现数据复杂度高的处理有些力不从心,所以看了看api以后决定用html来做. 我的wlw的代码插件不是很好用,大家凑合看吧 类前说明引用和权限 1: [Permissi ...

  4. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

  5. 给Winform中的TabControl添加更现代的拖拽功能

    上周接到一个开发任务,大致是允许APP中的Tab拖动以成为一个独立Tab,脱离之前的TabControl,就是现在Web拖动标签页创建新窗口的功能,现在浏览器必备的功能,应该很简单,然而我司采用的Do ...

  6. winform中文本框添加拖拽功能

    对一个文本框添加拖拽功能: private void txtFolder_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataP ...

  7. html5中的拖拽功能

    拖拽元素支持的事件 ondrag 应用于拖拽元素,整个拖拽过程都会调用 ondragstart 应用于拖拽元素,当拖拽开始时调用 ondragleave 应用于拖拽元素,当鼠标离开拖拽元素是调用 on ...

  8. Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结

    Atitit.D&D drag&drop拖拽功能c#.net java swing的对比与实现总结 1. 实现一个D&D操作一般包括三个步骤: 1 2. .net黑头的拖曳机制 ...

  9. 使用jQuery Draggable和Droppable实现拖拽功能

    上篇博客中已经介绍了web开发中基本拖放原理,现在给出需要完成的功能.最后运行的效果如下图所示: 主要功能需求说明: 1.左侧的元素结构最后会通过Ajax call服务器的数据来生成,能支持多级元素. ...

随机推荐

  1. Docker 安装并定制 Nginx 服务器

    安装并定制 Nginx 1.查阅对应的官方文档,首先下载镜像文件: [spider@izwz9d74k4cznxtxjeeur9z local]$ sudo docker pull nginx [su ...

  2. CF 429B B.Working out (四角dp)

    题意: 两个人一个从左上角一个从左下角分别开始走分别走向右下角和右上角,(矩阵每个格子有数)问到达终点后可以得到的最大数是多少,并且条件是他们两个相遇的时候那个点的数不能算 思路: 首先这道题如果暴力 ...

  3. cmd杀死进程

    打开cmd 1.查看所有进程占用的端口 输入:netstat –ano(查看所有进程,查找相应占用端口的程序的pid) 直接查看占用指定端口的程序的pid 输入:netstat -ano|findst ...

  4. 【BZOJ 1013】球形空间产生器sphere(高斯消元)

    球形空间产生器sphere HYSBZ - 1013 (高斯消元) 原题地址 题意 给出n维的球上的n个点,问原球体球心. 提示 n维球体上两点距离公式\(dist = \sqrt{ (a1-b1)^ ...

  5. Python网络编程—socket(二)

    http://www.cnblogs.com/phennry/p/5645369.html 接着上篇博客我们继续介绍socket网络编程,今天主要介绍的内容:IO多路复用.多线程.补充知识点. 一.I ...

  6. Spring 获取当前activeProfile

    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()) ...

  7. java 访问对象私有变量

    Captcha captcha = getCaptcha(captchaId); // 通过反射获取验证码值 Class<?> classType = captcha.getClass() ...

  8. codeforces gym 100357 K (表达式 模拟)

    题目大意 将一个含有+,-,^,()的表达式按照运算顺序转换成树状的形式. 解题分析 用递归的方式来处理表达式,首先直接去掉两边的括号(如果不止一对全部去光),然后找出不在括号内且优先级最低的符号.如 ...

  9. [bzoj3289]Mato的文件管理_莫队_树状数组

    Mato的文件管理 bzoj-3289 题目大意:给定一个n个数的序列.m次询问:一段区间中的逆序对个数. 注释:$1\le n\,mle 5\cdot 10^4$. 想法: 开始想这个题的大佬们,给 ...

  10. YAML/Properties配置文件与Spring Boot(转)

    多年来,Java开发人员依赖于属性文件或xml文件来指定应用程序配置.在企业应用程序中,人们可以为每个环境(如开发,分段和生产)创建单独的文件,以定义相应环境的属性.但是,通过Spring引导,我们可 ...