C#引用CefSharp並遮蔽滑鼠右鍵和禁止拖動放置事件
阿新 • • 發佈:2019-02-08
目錄:
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);
}
之後執行,會發現已經遮蔽滑鼠右鍵和拖動放置事件。