C#引用CefSharp
在C#的设计界面,添加panel控件用来放置CEFSharp浏览器。
CEFSharp浏览器dll添加引用
using CefSharp;
using CefSharp.WinForms;
1
2
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,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,CefMenuCommand commandID,CefEventFlags eventFlags)
{
return false;
}
public voID OnContextMenudismissed(IWebbrowser browserControl,iframe frame)
{
}
public bool runcontextMenu(IWebbrowser browserControl,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,IDragData dragData,DragOperationsMask mask)
{
return true;// false;//throw new NotImplementedException();
}
public voID OnDraggableRegionsChanged(IWebbrowser browserControl,IList 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);
}
之后运行,会发现已经屏蔽鼠标右键和拖动放置事件。
总结以上是内存溢出为你收集整理的ChromiumWebBrowser禁止鼠标右键和拖动全部内容,希望文章能够帮你解决ChromiumWebBrowser禁止鼠标右键和拖动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)