-
Notifications
You must be signed in to change notification settings - Fork 7
public Form1()
Mohammed Osama edited this page May 18, 2018
·
1 revision
public Form1() {
InitializeComponent();
CefSettings settings = new CefSettings();
CefSharpSettings.LegacyJavascriptBindingEnabled = true; //true
CefSharpSettings.ConcurrentTaskExecution = true; //true
CefSharpSettings.WcfEnabled = true; //true
settings.LogSeverity = LogSeverity.Verbose;
settings.JavascriptFlags = JavascriptFlags;
settings.ResourcesDirPath = ResourcesDirPath;
settings.LogFile = "$PathUserDataProfile/Debug.log";
settings.CachePath = "$PathUserDataProfile/";
settings.UserDataPath = "$PathUserDataProfile/";
settings.AcceptLanguageList = "ar,en-US,zh-CN,zh-TW,en-GB,ko,ja,ru"; settings.Locale = @"en-US"; settings.LocalesDirPath = "locales";
settings.PersistSessionCookies = true; //true and false // settings.PersistSessionCookies = false;
settings.PersistUserPreferences = true; //true and false
settings.IgnoreCertificateErrors = true; //true
settings.MultiThreadedMessageLoop = true; //true
settings.FocusedNodeChangedEnabled = true; //true
settings.WindowlessRenderingEnabled = true; //true
settings.CommandLineArgsDisabled = false; //false // settings.CommandLineArgsDisabled = false;
// Command Line Arguments
settings.CefCommandLineArgs.Add("disable-application-cache", "1");
settings.CefCommandLineArgs.Add("disable-session-storage", "1");
settings.CefCommandLineArgs.Add("enable-automatic-password-saving", "enable-automatic-password-saving");
settings.CefCommandLineArgs.Add("enable-password-save-in-page-navigation", "enable-password-save-in-page-navigation");
settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling", "1");
settings.CefCommandLineArgs.Add("renderer-process-limit", "1");
settings.CefCommandLineArgs.Add("persist_session_cookies", "1");
// Enable
settings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream", "1");
settings.CefCommandLineArgs.Add("enable-speech-input", "1");
settings.CefCommandLineArgs.Add("enable-usermedia-screen-capture", "1");
settings.CefCommandLineArgs.Add("--load-plugin", "1");
settings.CefCommandLineArgs.Add("load-plugin", "1");
settings.CefCommandLineArgs.Add("extra-plugin-dir", "1");
settings.CefCommandLineArgs.Add("--extra-plugin-dir", "1");
settings.CefCommandLineArgs.Add("--load-plugin--extra-plugin-dir", "1");
settings.CefCommandLineArgs.Add("--extra-plugin-dir--load-plugin", "1");
settings.CefCommandLineArgs.Add("debug-plugin-loading", "1"); //Dumps extra logging about plugin loading to the log file.
settings.CefCommandLineArgs.Add("disable-plugins-discovery", "1"); //Disable discovering third-party plugins. Effectively loading only ones shipped with the browser plus third-party ones as specified by --extra-plugin-dir and --load-plugin switches
settings.CefCommandLineArgs.Add("--enable-widevine-cdm", "1");
//By default, an https page cannot run JavaScript, CSS or plugins from http URLs. This provides an override to get the old insecure behavior. Only available in 47 and above.
settings.CefCommandLineArgs.Add("allow-running-insecure-content", "1");
// <http://xfinitytv.comcast.net/live/network/hgtv?cmpid=livetv_hgtv>
// settings.CefCommandLineArgs.Add("enable-system-flash", "1");
settings.CefCommandLineArgs.Add("enable-npapi", "1");
settings.CefCommandLineArgs.Add("enable-system", "1");
// Enable WebRTC
settings.CefCommandLineArgs.Add("enable-media-stream", "1");
//Extension support can be disabled
//The PDF extension specifically can be disabled
settings.CefCommandLineArgs.Add("enable-extensions", "1"); settings.CefCommandLineArgs.Add("enable-extension", "1");
// settings.CefCommandLineArgs.Add("disable-extensions", "1"); settings.CefCommandLineArgs.Add("disable-extension", "1");
settings.CefCommandLineArgs.Add("disable-pdf-extension", "1");
// Disables the DirectWrite font rendering system on windows.
// Possibly useful when experiencing blury fonts.
settings.CefCommandLineArgs.Add("disable-web-security", "1");
//Load the pepper flash player that comes with Google Chrome - may be possible to load these values from the registry and query the dll for it's version info (Step 2 not strictly required it seems)
settings.CefCommandLineArgs.Add("ppapi-flash-path", @"Main\PepperFlash\pepflashplayer.dll"); //Load a specific pepper flash version (Step 1 of 2)
settings.CefCommandLineArgs.Add("ppapi-flash-version", "29.0.0.171"); //Load a specific pepper flash version (Step 2 of 2)
// settings.CefCommandLineArgs.Add("disable-direct-write", "1");
// settings.SetOffScreenRenderingBestPerformanceArgs();
//Disable GPU Acceleration
// settings.CefCommandLineArgs.Add("disable-gpu-vsync", "1");
// settings.CefCommandLineArgs.Add("disable-gpu", "1");
// Cef.GetGlobalCookieManager().DeleteCookies("", "");
// Proxy Stuff More Refinement is needed for reaching local IP's
// Don't use a proxy server, always make direct connections. Overrides any other proxy server flags that are passed.
// Slightly improves Cef initialize time as it won't attempt to resolve a proxy
settings.CefCommandLineArgs.Add("no-proxy-server", "1");
settings.CefCommandLineArgs.Add("proxy-auto-detect", "1");
settings.CefCommandLineArgs.Add("winhttp-proxy-resolver", "1");
settings.ResourcesDirPath = System.IO.Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase).LocalPath);
var root = System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
string downloadPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string downloadFileName = downloadPath + "\\XMLFILE.XML".ToString();
System.Data.SqlClient.SqlConnection con = null;
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("sp_RetrieveHardwareXML", con); DataSet ds = new DataSet();
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
settings.RemoteDebuggingPort = 8080;
// Set Google API keys, used for Geolocation requests sans GPS. See http://www.chromium.org/developers/how-tos/api-keys
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "AIzaSyBS-qjz0jmK4jvZy06Rq4DLs0_AITqo7lw");
Environment.SetEnvironmentVariable("GOOGLE_DEFAULT_CLIENT_ID", "888034971568-kmdju8hjiem3mqqi3kgjmals6p0l0te4.apps.googleusercontent.com");
Environment.SetEnvironmentVariable("GOOGLE_DEFAULT_CLIENT_SECRET", "6fa_ZYlY8KxDd_KDOz1SdENV");
Cef.Initialize(settings);
toolStripTextBoxAddress.Text = "chrome://version"; // http://seriesmoviesultimate.webege.com/
ChromiumWebBrowser ChromiumWebBrowser = new ChromiumWebBrowser(toolStripTextBoxAddress.Text)
{ BrowserSettings = { DefaultEncoding = "UTF-8", Databases = CefState.Disabled, JavascriptDomPaste = CefState.Disabled, Javascript = CefState.Enabled, ApplicationCache = CefState.Disabled, FileAccessFromFileUrls = CefState.Enabled, TabToLinks = CefState.Default, UniversalAccessFromFileUrls = CefState.Default, WebSecurity = CefState.Enabled, LocalStorage = CefState.Disabled, ImageLoading = CefState.Default, Plugins = CefState.Default, WebGl = CefState.Disabled } };
ChromiumWebBrowser.Dock = DockStyle.Fill; ChromiumWebBrowser.AddressChanged += Chrome_AddressChanged; ChromiumWebBrowser.TitleChanged += Chrome_TitleChanged;
// Handler
ChromiumWebBrowser.DownloadHandler = new DownloadHandler();
ChromiumWebBrowser.MenuHandler = new ChromiumApplication.ExampleMain.Example.Request.AMyCustomMenuHandler();
// Handler
TabPage tpage = new TabPage(); tpage.Text = " Ultimate Browser "; tpage.Show();
browserTabControl.Controls.Add(tpage); ChromiumWebBrowser.Parent = tpage;
}
// Start the browser after initialize global component
/* // ChromiumApplication.ExampleMain.Example.Request.ASystemMenu.CreateSysMenu(this);
// settings.CachePath = AppDomain.CurrentDomain.BaseDirectory + "PathExample/Cache";
// settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\CEF";
// settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\A Fast CefSharp"; */
// InitializeChromium();
// Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
// CefSharp V53.0.1 - CefCustomObject.cs //
// chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this));
// Cef.Initialize();
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);
}