12 changed files with 220 additions and 50 deletions
@ -0,0 +1,139 @@ |
|||||
|
using CefSharp; |
||||
|
using CefSharp.Wpf; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
using System.Windows; |
||||
|
using System.Windows.Controls; |
||||
|
using System.Windows.Input; |
||||
|
|
||||
|
namespace 齐越慧眼.cefhelper |
||||
|
{ |
||||
|
public class CefMenuItem : IContextMenuHandler |
||||
|
{ |
||||
|
|
||||
|
void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame) |
||||
|
{ |
||||
|
//隐藏菜单栏
|
||||
|
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl; |
||||
|
|
||||
|
chromiumWebBrowser.Dispatcher.Invoke(() => |
||||
|
{ |
||||
|
chromiumWebBrowser.ContextMenu = null; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) |
||||
|
{ |
||||
|
|
||||
|
//绘制了一遍菜单栏 所以初始化的时候不必绘制菜单栏,再此处绘制即可
|
||||
|
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl; |
||||
|
|
||||
|
chromiumWebBrowser.Dispatcher.Invoke(() => |
||||
|
{ |
||||
|
var menu = new ContextMenu |
||||
|
{ |
||||
|
IsOpen = true |
||||
|
}; |
||||
|
|
||||
|
RoutedEventHandler handler = null; |
||||
|
|
||||
|
handler = (s, e) => |
||||
|
{ |
||||
|
menu.Closed -= handler; |
||||
|
|
||||
|
//If the callback has been disposed then it's already been executed
|
||||
|
//so don't call Cancel
|
||||
|
if (!callback.IsDisposed) |
||||
|
{ |
||||
|
callback.Cancel(); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
menu.Closed += handler; |
||||
|
|
||||
|
menu.Items.Add(new MenuItem |
||||
|
{ |
||||
|
Header = "刷新", |
||||
|
Command = new CustomCommand(() => { |
||||
|
Application.Current.Dispatcher.Invoke(() => |
||||
|
{ |
||||
|
chromiumWebBrowser.Reload(); |
||||
|
}); |
||||
|
}) |
||||
|
}); |
||||
|
chromiumWebBrowser.ContextMenu = menu; |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 关闭窗体
|
||||
|
/// </summary>
|
||||
|
private void CloseWindow() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private static IEnumerable<Tuple<string, CefMenuCommand>> GetMenuItems(IMenuModel model) |
||||
|
{ |
||||
|
var list = new List<Tuple<string, CefMenuCommand>>(); |
||||
|
for (var i = 0; i < model.Count; i++) |
||||
|
{ |
||||
|
var header = model.GetLabelAt(i); |
||||
|
var commandId = model.GetCommandIdAt(i); |
||||
|
list.Add(new Tuple<string, CefMenuCommand>(header, commandId)); |
||||
|
} |
||||
|
|
||||
|
return list; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class CustomCommand : ICommand |
||||
|
{ |
||||
|
Action _TargetExecuteMethod; |
||||
|
Func<bool> _TargetCanExecuteMethod; |
||||
|
public event EventHandler CanExecuteChanged = delegate { }; |
||||
|
|
||||
|
public CustomCommand(Action executeMethod) |
||||
|
{ |
||||
|
_TargetExecuteMethod = executeMethod; |
||||
|
} |
||||
|
|
||||
|
bool ICommand.CanExecute(object parameter) |
||||
|
{ |
||||
|
if (_TargetCanExecuteMethod != null) |
||||
|
{ |
||||
|
return _TargetCanExecuteMethod(); |
||||
|
} |
||||
|
if (_TargetExecuteMethod != null) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public void RaiseCanExecuteChanged() |
||||
|
{ |
||||
|
CanExecuteChanged(this, EventArgs.Empty); |
||||
|
} |
||||
|
|
||||
|
void ICommand.Execute(object parameter) |
||||
|
{ |
||||
|
_TargetExecuteMethod?.Invoke(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
After Width: | Height: | Size: 4.2 KiB |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue