-
Notifications
You must be signed in to change notification settings - Fork 7
Z Context Menu Handler.cs
Mohammed Osama edited this page May 11, 2018
·
1 revision
Custom Context Menu Handler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CefSharp;
using System.Windows.Forms;
using CefSharp.WinForms;
using ChromiumApplication.ExampleMain.Example.Request;
using ChromiumApplication;
namespace ChromiumApplication.ExampleMain.Example.Request {
public class AMyCustomMenuHandler : IContextMenuHandler {
private string copyFrameUrl;
private const int SaveImageGet = 26500;
private const int ShowDevTools = 50;
private const int CloseDevTools = 51;
private const int SaveImageAs = 26503;
private const int SaveAsPdf = 26504;
private const int SaveLinkAs = 26505;
private const int CopyLinkAddress = 26506;
private const int OpenLinkInNewTab = 26507;
private string lastSelText = "";
Form1 myForm;
public AMyCustomMenuHandler(Form1 form)
{
myForm = form;
}
public AMyCustomMenuHandler()
{
}
public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
// Remove any existent option using the Clear method of the model
// // Edit
// model.Clear();
model.Clear();
// save text
lastSelText = parameters.SelectionText;
// Console.WriteLine("Context menu opened !");
Console.Write("Context menu opened !");
// You can add a separator in case that there are more items on the list
if (model.Count > 0)
{
model.AddSeparator();
}
//Add new custom menu items
if (parameters.FrameUrl != null)
{
copyFrameUrl = parameters.FrameUrl;
}
// Removing existing menu item
// bool removed = model.Remove(CefMenuCommand.ViewSource); // Remove "View Source" option
if (parameters.LinkUrl != "")
{
// model.AddItem((CefMenuCommand)SaveLinkAs, "Save As Link");
model.AddItem((CefMenuCommand)SaveImageAs, "Save as images");
// model.AddItem((CefMenuCommand)OpenLinkInNewTab, "Open link in new tab");
model.AddItem((CefMenuCommand)CopyLinkAddress, "Copy Link Address");
// model.AddItem((CefMenuCommand)50, "Developer Tools");
// model.AddItem((CefMenuCommand)51, "Close Developer Tools");
// model.AddSeparator();
}
// Add another example item
model.AddItem((CefMenuCommand)1, "Back");
model.AddItem((CefMenuCommand)2, "Forward");
model.AddItem((CefMenuCommand)3, "Reload");
model.AddItem((CefMenuCommand)52, "Stop");
model.AddItem((CefMenuCommand)4, "View Source");
model.AddItem((CefMenuCommand)ShowDevTools, "Developer Tools");
model.AddSeparator();
model.AddItem((CefMenuCommand)SaveImageAs, "Save As Images");
model.AddItem((CefMenuCommand)SaveImageGet, "Save As Page");
model.AddItem((CefMenuCommand)SaveLinkAs, "Save As Link");
model.AddSeparator();
// model.AddItem((CefMenuCommand)00, "@");
model.AddItem((CefMenuCommand)10, "Undo");
model.AddItem((CefMenuCommand)11, "Redo");
model.AddItem((CefMenuCommand)5, "Cut");
model.AddItem((CefMenuCommand)6, "Copy");
model.AddItem((CefMenuCommand)7, "Paste");
model.AddItem((CefMenuCommand)8, "Delete");
model.AddItem((CefMenuCommand)9, "SelectAll");
model.AddSeparator();
model.AddItem((CefMenuCommand)12, "Google Chinese");
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
model.AddSeparator();
// model.AddItem((CefMenuCommand)12, "Google CN");
/*
model.AddItem((CefMenuCommand)13, "#");
model.AddItem((CefMenuCommand)14, "#");
model.AddItem((CefMenuCommand)15, "#");
model.AddItem((CefMenuCommand)16, "#");
model.AddItem((CefMenuCommand)17, "#");
model.AddItem((CefMenuCommand)18, "#");
model.AddItem((CefMenuCommand)19, "#");
model.AddItem((CefMenuCommand)20, "#");
*/
// model.AddSeparator();
// Add a new item to the list using the AddItem method of the model
// model.AddItem((CefMenuCommand)26501, "Show DevTools");
// model.AddItem((CefMenuCommand)26502, "Close DevTools");
// model.AddItem((CefMenuCommand)50, "Show DevTools");
// model.AddItem((CefMenuCommand)51, "Close DevTools");
// Add a separator
// model.AddSeparator();
// Add another example item
// model.AddSeparator();
// model.AddItem((CefMenuCommand)26504, "Back");
// model.AddItem((CefMenuCommand)26505, "Forward");
}
public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
////
if (parameters.HasImageContents && parameters.SourceUrl.CheckIfValid())
{
// RIGHT CLICKED ON IMAGE
}
if (parameters.SelectionText != null)
{
// TEXT IS SELECTED
}
int id = (int)commandId;
if (id == ShowDevTools)
{
browser.ShowDevTools();
}
if (id == CloseDevTools)
{
browser.CloseDevTools();
}
if (id == SaveImageAs)
{
browser.GetHost().StartDownload(parameters.SourceUrl);
return true;
}
if (id == SaveLinkAs)
{
browser.GetHost().StartDownload(parameters.LinkUrl);
return true;
}
if (id == OpenLinkInNewTab)
{
// Form1.browserTabControl.Controls.Add(tpage);
// ChromiumWebBrowser newBrowser = myForm.AddNewBrowserTab(parameters.LinkUrl, false, browser.MainFrame.Url);
}
if (id == CopyLinkAddress)
{
Clipboard.SetText(parameters.LinkUrl);
}
if (id == SaveImageGet)
{
browser.GetHost().StartDownload(parameters.PageUrl);
return true;
}
if (commandId == (CefMenuCommand)00) //26501
{
// browser.GetHost().StartDownload(parameters.DictionarySuggestions);
return true;
}
//false,
////
// React to the first ID (show dev tools method)
if (commandId == (CefMenuCommand)50) //26501
{
browser.GetHost().ShowDevTools();
return true;
}
// React to the second ID (show dev tools method)
if (commandId == (CefMenuCommand)51) //26502
{
browser.GetHost().CloseDevTools();
return true;
}
if (commandId == (CefMenuCommand)52) //
{
browser.StopLoad();
return true;
}
// React to the third ID (browser)
if (commandId == (CefMenuCommand)1) //26504
{
if (browser.CanGoBack)
browser.GoBack();
return true;
}
if (commandId == (CefMenuCommand)2) //26505
{
if (browser.CanGoForward)
browser.GoForward();
return true;
}
if (commandId == (CefMenuCommand)3) //
{
browser.MainFrame.Browser.Reload();
return true;
}
if (commandId == (CefMenuCommand)4) //
{
browser.MainFrame.ViewSource();
return true;
}
if (commandId == (CefMenuCommand)5) //
{
browser.MainFrame.Cut();
return true;
}
if (commandId == (CefMenuCommand)6) //
{
browser.MainFrame.Copy();
return true;
}
if (commandId == (CefMenuCommand)7) //
{
browser.MainFrame.Paste();
return true;
}
if (commandId == (CefMenuCommand)8) //
{
browser.MainFrame.Delete();
return true;
}
if (commandId == (CefMenuCommand)9) //
{
browser.MainFrame.SelectAll();
return true;
}
if (commandId == (CefMenuCommand)10) //
{
browser.MainFrame.Undo();
return true;
}
if (commandId == (CefMenuCommand)11) //
{
browser.MainFrame.Redo();
return true;
}
if (commandId == (CefMenuCommand)12) //
{
browser.MainFrame.LoadUrl("https://www.google.com.hk");
return true;
}
if (commandId == (CefMenuCommand)13) //
{
// browser.SetZoomLevel();
// browser.GetHost().SetZoomLevel(50.0);
return true;
}
if (commandId == (CefMenuCommand)14) //
{
// browser.GetHost().SetZoomLevel(100.00);
return true;
}
if (commandId == (CefMenuCommand)15) //
{
return true;
}
// Any new item should be handled through a new if statement
// Return false should ignore the selected option of the user !
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;
}
}
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var headers = request.Headers;
headers["Custom-Header"] = "My Custom Header";
request.Headers = headers;
return CefReturnValue.Continue;
}
Anyone got an answer for my question above re loading a page into the control when the WPF TabItem it is on it doesn't have focus?
@nzmike99 The default WPF TabControl
isn't really suitable for hosting a browser, see
When I add the ChromiumWebBrowserWithScreenshotSupport.cs file to the CefSharp.Wpf.Example project it needs the "using GalaSoft.MvvmLight.Command; " dependency which destroys the project with errors
@jeremiahjordani_twitter You could use any Delegate/RelayCommand implementation you like. Pretty common for WPF apps to have one.
If you don't have one then just change the access modifier for the paramaterless TakeScreenshot so it's usable in your instance. The command is entirely optional.
private void /*Btn*/Sceenshot_Click(object sender, RoutedEventArgs e)
{
/*
//CefSharp - Quick and Dirty Screenshot with no class dependency
MessageBox.Show("Take screenshot");
double dWidth = (webCefSharpChromiumWebBrowser.ActualWidth);
double dHeight = (webCefSharpChromiumWebBrowser.ActualHeight);
int actualWidth = 0;
int actualHeight = 0;
actualWidth = Convert.ToInt32(dWidth);
actualHeight = Convert.ToInt32(dHeight);
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(actualWidth, actualHeight, 90, 90, PixelFormats.Pbgra32);
renderTargetBitmap.Render(webCefSharpChromiumWebBrowser);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(@"C:\SaveFolderName\apollo.png")) //uses System.IO for fileStream
{
pngImage.Save(fileStream);
}
*/
}
[C#
] TakeScreenshot [WPF
]
/*Here is an example that reuses the image that's already rendered to the screen.*/
var template = Browser.Template;
var image = (System.Windows.Controls.Image)template.FindName("PART_image", Browser);
var pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create((System.Windows.Media.Imaging.BitmapSource)image.Source));
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");
using (var fileStream = File.Create(screenshotPath))
{
pngImage.Save(fileStream);
}