原文:
C#引用CefSharp并屏蔽鼠标右键和禁止拖动放置事件
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013564470/article/details/78339957
目录:
C#引用CefSharp
在C#的设计界面,添加panel控件用来放置CEFSharp浏览器。
CEFSharp浏览器dll添加引用
using CefSharp;
using CefSharp.WinForms;
CEFSharp浏览器初始化代码:
public Form1()
{
InitializeComponent();
InitBrowser();
}
public ChromiumWebBrowser browser;
public void InitBrowser()
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://my.csdn.net/u013564470");
browser.Dock = DockStyle.Fill;
panel1.Controls.Add(browser);
}
上述完成之后就可以运行了!
注意事项:平台选择X86,右侧解决方案中引用DLL文件,文件放在\项目名\bin\x86\Debug
C#添加代码屏蔽鼠标右键
在屏蔽之前先查看namespace CefSharp.WinForms内的代码
public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser, IWebBrowser, IDisposable
{
public ChromiumWebBrowser(string address);
public IDownloadHandler DownloadHandler { get; set; }
public ILoadHandler LoadHandler { get; set; }
public IDisplayHandler DisplayHandler { get; set; }
public IContextMenuHandler MenuHandler { get; set; }
public IRenderProcessMessageHandler RenderProcessMessageHandler { get; set; }
public IFindHandler FindHandler { get; set; }
public IRequestHandler RequestHandler { get; set; }
public IFocusHandler FocusHandler { get; set; }
public IResourceHandlerFactory ResourceHandlerFactory { get; set; }
public IGeolocationHandler GeolocationHandler { get; set; }
public bool CanGoForward { get; }
public bool CanGoBack { get; }
public bool IsBrowserInitialized { get; }
public override bool Focused { get; }
public IDragHandler DragHandler { get; set; }
public IKeyboardHandler KeyboardHandler { get; set; }
public ILifeSpanHandler LifeSpanHandler { get; set; }
public IDialogHandler DialogHandler { get; set; }
public string Address { get; }
public string TooltipText { get; }
public bool IsLoading { get; }
public RequestContext RequestContext { get; set; }
public BrowserSettings BrowserSettings { get; set; }
public bool IsActivating { get; set; }
public IJsDialogHandler JsDialogHandler { get; set; }
public event EventHandler<AddressChangedEventArgs> AddressChanged;
public event EventHandler<StatusMessageEventArgs> StatusMessage;
public event EventHandler<ConsoleMessageEventArgs> ConsoleMessage;
public event EventHandler<LoadingStateChangedEventArgs> LoadingStateChanged;
public event EventHandler<FrameLoadEndEventArgs> FrameLoadEnd;
public event EventHandler<FrameLoadStartEventArgs> FrameLoadStart;
public event EventHandler<LoadErrorEventArgs> LoadError;
public event EventHandler<IsBrowserInitializedChangedEventArgs> IsBrowserInitializedChanged;
public event EventHandler<TitleChangedEventArgs> TitleChanged;
public IBrowser GetBrowser();
public void Load(string url);
public void RegisterAsyncJsObject(string name, object objectToBind, bool camelCaseJavascriptNames = true);
public void RegisterJsObject(string name, object objectToBind, bool camelCaseJavascriptNames = true);
protected override void Dispose(bool disposing);
protected override void OnGotFocus(EventArgs e);
protected override void OnHandleCreated(EventArgs e);
protected override void OnSizeChanged(EventArgs e);
}
我们在其中的public IContextMenuHandler MenuHandler { get; set; }接口中更改鼠标右键事件。
方法为继承该类接口,并重写。实现方式如下:
public class MenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
model.Clear();
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
return false;
}
public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
定义完之后在CEFSharp代码初始化中引用。如下:
public ChromiumWebBrowser browser;
public void InitBrowser()
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://my.csdn.net/u013564470");
browser.MenuHandler = new MenuHandler();
browser.Dock = DockStyle.Fill;
panel1.Controls.Add(browser);
}
之后运行,会发现已经屏蔽鼠标右键。
C#添加代码屏蔽拖动放置事件
屏蔽拖动放置事件和屏蔽鼠标右键类似。我们在其中的public IDragHandler DragHandler { get; set; }接口中更改拖动放置事件。
方法为继承该类接口,并重写。实现方式如下:
public class DragHandler : IDragHandler
{
public bool OnDragEnter(IWebBrowser browserControl, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
return true;// false;//throw new NotImplementedException();
}
public void OnDraggableRegionsChanged(IWebBrowser browserControl, IBrowser browser, IList<DraggableRegion> regions)
{
//throw new NotImplementedException();
}
}
return true;为禁止。定义完之后在CEFSharp代码初始化中引用。如下:
public ChromiumWebBrowser browser;
public void InitBrowser()
{
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://my.csdn.net/u013564470");
browser.MenuHandler = new MenuHandler();
browser.DragHandler = new DragHandler();
browser.Dock = DockStyle.Fill;
panel1.Controls.Add(browser);
}
之后运行,会发现已经屏蔽鼠标右键和拖动放置事件。