ChromiumWebBrowser禁止鼠标右键和拖动
在屏蔽之前先查看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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
}
我们在其中的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);
}
1
2
3
4
5
6
7
8
9
之后运行,会发现已经屏蔽鼠标右键。
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 regions)
{
//throw new NotImplementedException();
}
}
return true;为禁止。定义完之后在CEFSharp代码初始化中引用。如下:
public ChromiumWebBrowser browser;
public void InitBrowser(http://www.my516.com)
{
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);
}
之后运行,会发现已经屏蔽鼠标右键和拖动放置事件。