2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2010 The CefSharp Authors. All rights reserved.
|
2014-06-25 12:00:58 +10:00
|
|
|
//
|
2014-06-30 14:02:22 +10:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
using System;
|
2018-12-28 13:30:58 +10:00
|
|
|
using System.Collections.Generic;
|
2019-06-02 20:34:44 +10:00
|
|
|
using System.ComponentModel;
|
2022-05-03 11:28:25 +10:00
|
|
|
using System.Diagnostics;
|
2018-12-31 13:12:10 +10:00
|
|
|
using System.IO;
|
2021-04-01 09:27:48 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2015-04-27 18:20:32 -04:00
|
|
|
using System.Threading.Tasks;
|
2018-09-10 02:41:13 +02:00
|
|
|
using System.Windows.Forms;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.Example;
|
2018-12-28 13:30:58 +10:00
|
|
|
using CefSharp.Example.Callback;
|
2018-12-31 13:12:10 +10:00
|
|
|
using CefSharp.Example.Handlers;
|
2021-11-26 10:09:59 +10:00
|
|
|
using CefSharp.WinForms.Host;
|
2010-12-03 16:22:41 +00:00
|
|
|
|
2012-02-23 13:20:34 -08:00
|
|
|
namespace CefSharp.WinForms.Example
|
2010-12-03 16:22:41 +00:00
|
|
|
{
|
2013-11-13 18:01:10 +02:00
|
|
|
public partial class BrowserForm : Form
|
2010-12-03 16:22:41 +00:00
|
|
|
{
|
2014-06-25 17:10:12 +10:00
|
|
|
private const string DefaultUrlForAddedTabs = "https://www.google.com";
|
|
|
|
|
|
2015-04-16 18:51:37 -04:00
|
|
|
// Default to a small increment:
|
2015-05-21 11:34:42 +10:00
|
|
|
private const double ZoomIncrement = 0.10;
|
2015-04-16 15:15:01 -04:00
|
|
|
|
2016-09-08 08:52:42 +10:00
|
|
|
private bool multiThreadedMessageLoopEnabled;
|
|
|
|
|
|
|
|
|
|
public BrowserForm(bool multiThreadedMessageLoopEnabled)
|
2012-02-17 14:12:07 -08:00
|
|
|
{
|
2012-02-24 18:33:38 -08:00
|
|
|
InitializeComponent();
|
2014-05-14 17:17:50 +10:00
|
|
|
|
2021-04-01 09:27:48 +02:00
|
|
|
var bitness = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
|
2014-10-27 11:17:33 +10:00
|
|
|
Text = "CefSharp.WinForms.Example - " + bitness;
|
2013-11-11 00:04:30 +02:00
|
|
|
WindowState = FormWindowState.Maximized;
|
2012-02-24 18:33:38 -08:00
|
|
|
|
2016-09-01 16:52:52 +10:00
|
|
|
Load += BrowserFormLoad;
|
2014-10-21 21:50:55 +10:00
|
|
|
|
|
|
|
|
//Only perform layout when control has completly finished resizing
|
|
|
|
|
ResizeBegin += (s, e) => SuspendLayout();
|
|
|
|
|
ResizeEnd += (s, e) => ResumeLayout(true);
|
2016-09-08 08:52:42 +10:00
|
|
|
|
|
|
|
|
this.multiThreadedMessageLoopEnabled = multiThreadedMessageLoopEnabled;
|
2014-06-25 16:46:18 +10:00
|
|
|
}
|
|
|
|
|
|
2019-06-02 20:34:44 +10:00
|
|
|
public IContainer Components
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (components == null)
|
|
|
|
|
{
|
|
|
|
|
components = new Container();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return components;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 16:52:52 +10:00
|
|
|
private void BrowserFormLoad(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
AddTab(CefExample.DefaultUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 15:01:38 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to add a Popup browser as a Tab
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="browserHostControl"></param>
|
2021-11-26 10:09:59 +10:00
|
|
|
public void AddTab(ChromiumHostControl browserHostControl, string url)
|
2018-01-30 15:01:38 +10:00
|
|
|
{
|
|
|
|
|
browserTabControl.SuspendLayout();
|
|
|
|
|
|
2021-11-26 10:09:59 +10:00
|
|
|
var browser = new BrowserTabUserControl(browserHostControl)
|
|
|
|
|
{
|
|
|
|
|
Dock = DockStyle.Fill,
|
|
|
|
|
Bounds = browserTabControl.Bounds
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-30 15:01:38 +10:00
|
|
|
var tabPage = new TabPage(url)
|
|
|
|
|
{
|
|
|
|
|
Dock = DockStyle.Fill
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-26 10:09:59 +10:00
|
|
|
tabPage.Controls.Add(browser);
|
2018-01-30 15:01:38 +10:00
|
|
|
|
|
|
|
|
browserTabControl.TabPages.Add(tabPage);
|
|
|
|
|
|
|
|
|
|
//Make newly created tab active
|
|
|
|
|
browserTabControl.SelectedTab = tabPage;
|
|
|
|
|
|
|
|
|
|
browserTabControl.ResumeLayout(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 17:19:08 +10:00
|
|
|
private void AddTab(string url, int? insertIndex = null)
|
2014-06-25 16:46:18 +10:00
|
|
|
{
|
2014-10-13 16:31:22 +10:00
|
|
|
browserTabControl.SuspendLayout();
|
|
|
|
|
|
2016-09-08 08:52:42 +10:00
|
|
|
var browser = new BrowserTabUserControl(AddTab, url, multiThreadedMessageLoopEnabled)
|
2013-11-13 18:01:10 +02:00
|
|
|
{
|
2014-05-07 15:55:09 +10:00
|
|
|
Dock = DockStyle.Fill,
|
2021-09-07 14:21:16 +10:00
|
|
|
Bounds = browserTabControl.Bounds
|
2013-11-13 18:01:10 +02:00
|
|
|
};
|
2014-05-07 17:13:50 +10:00
|
|
|
|
2014-06-25 16:46:18 +10:00
|
|
|
var tabPage = new TabPage(url)
|
|
|
|
|
{
|
|
|
|
|
Dock = DockStyle.Fill
|
|
|
|
|
};
|
2014-11-18 11:37:10 -05:00
|
|
|
|
|
|
|
|
//This call isn't required for the sample to work.
|
|
|
|
|
//It's sole purpose is to demonstrate that #553 has been resolved.
|
|
|
|
|
browser.CreateControl();
|
|
|
|
|
|
2014-06-25 16:46:18 +10:00
|
|
|
tabPage.Controls.Add(browser);
|
2014-08-25 17:19:08 +10:00
|
|
|
|
|
|
|
|
if (insertIndex == null)
|
|
|
|
|
{
|
|
|
|
|
browserTabControl.TabPages.Add(tabPage);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
browserTabControl.TabPages.Insert(insertIndex.Value, tabPage);
|
|
|
|
|
}
|
2014-06-25 17:12:40 +10:00
|
|
|
|
|
|
|
|
//Make newly created tab active
|
2014-08-25 19:04:39 +10:00
|
|
|
browserTabControl.SelectedTab = tabPage;
|
2014-10-13 16:31:22 +10:00
|
|
|
|
|
|
|
|
browserTabControl.ResumeLayout(true);
|
2014-05-07 16:07:33 +10:00
|
|
|
}
|
2014-05-07 16:37:16 +10:00
|
|
|
|
2014-05-07 17:27:13 +10:00
|
|
|
private void ExitMenuItemClick(object sender, EventArgs e)
|
2014-08-25 20:03:58 +10:00
|
|
|
{
|
|
|
|
|
ExitApplication();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExitApplication()
|
2014-05-07 17:27:13 +10:00
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-24 18:33:38 -08:00
|
|
|
private void AboutToolStripMenuItemClick(object sender, EventArgs e)
|
2012-02-17 14:12:07 -08:00
|
|
|
{
|
2012-02-24 18:33:38 -08:00
|
|
|
new AboutBox().ShowDialog();
|
2010-12-14 12:16:45 +00:00
|
|
|
}
|
2014-05-14 17:17:50 +10:00
|
|
|
|
2022-01-31 12:54:18 +10:00
|
|
|
public void RemoveTab(ChromiumHostControl ctrl)
|
2018-04-04 13:18:21 +10:00
|
|
|
{
|
2022-01-31 12:54:18 +10:00
|
|
|
if (!ctrl.IsDisposed)
|
2018-04-04 13:18:21 +10:00
|
|
|
{
|
2022-01-31 12:54:18 +10:00
|
|
|
var tabPage = ctrl.GetParentOfType<TabPage>();
|
|
|
|
|
|
|
|
|
|
if(tabPage == null)
|
2018-04-04 13:18:21 +10:00
|
|
|
{
|
2022-01-31 12:54:18 +10:00
|
|
|
throw new Exception("Unable to find parent TabPage");
|
2018-04-04 13:18:21 +10:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 12:54:18 +10:00
|
|
|
browserTabControl.TabPages.Remove(tabPage);
|
2018-04-04 13:18:21 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-14 17:17:50 +10:00
|
|
|
private void FindMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 17:07:08 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
control.ShowFind();
|
|
|
|
|
}
|
2014-05-14 17:17:50 +10:00
|
|
|
}
|
|
|
|
|
|
2014-05-29 08:01:25 +10:00
|
|
|
private void CopySourceToClipBoardAsyncClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 16:53:05 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
control.CopySourceToClipBoardAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BrowserTabUserControl GetCurrentTabControl()
|
|
|
|
|
{
|
|
|
|
|
if (browserTabControl.SelectedIndex == -1)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tabPage = browserTabControl.Controls[browserTabControl.SelectedIndex];
|
2018-01-30 15:01:38 +10:00
|
|
|
var control = tabPage.Controls[0] as BrowserTabUserControl;
|
2014-06-25 16:46:18 +10:00
|
|
|
|
2014-06-25 16:53:05 +10:00
|
|
|
return control;
|
2014-05-29 08:01:25 +10:00
|
|
|
}
|
2014-06-25 17:10:12 +10:00
|
|
|
|
|
|
|
|
private void NewTabToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
AddTab(DefaultUrlForAddedTabs);
|
|
|
|
|
}
|
2014-06-25 17:27:40 +10:00
|
|
|
|
|
|
|
|
private void CloseTabToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2018-09-10 02:41:13 +02:00
|
|
|
if (browserTabControl.TabPages.Count == 0)
|
2014-06-25 17:27:40 +10:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 16:39:29 +10:00
|
|
|
var currentIndex = browserTabControl.SelectedIndex;
|
|
|
|
|
|
2018-04-04 13:18:21 +10:00
|
|
|
var tabPage = browserTabControl.TabPages[currentIndex];
|
2014-06-25 17:27:40 +10:00
|
|
|
|
|
|
|
|
var control = GetCurrentTabControl();
|
2018-04-04 13:18:21 +10:00
|
|
|
if (control != null && !control.IsDisposed)
|
2014-06-25 17:27:40 +10:00
|
|
|
{
|
|
|
|
|
control.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 13:18:21 +10:00
|
|
|
browserTabControl.TabPages.Remove(tabPage);
|
2014-08-25 16:54:44 +10:00
|
|
|
|
2015-06-13 10:32:15 -04:00
|
|
|
tabPage.Dispose();
|
|
|
|
|
|
2014-10-13 16:39:29 +10:00
|
|
|
browserTabControl.SelectedIndex = currentIndex - 1;
|
|
|
|
|
|
2018-04-04 13:18:21 +10:00
|
|
|
if (browserTabControl.TabPages.Count == 0)
|
2014-08-25 16:54:44 +10:00
|
|
|
{
|
2014-08-25 20:03:58 +10:00
|
|
|
ExitApplication();
|
2014-08-25 16:54:44 +10:00
|
|
|
}
|
2014-06-25 17:27:40 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
|
|
|
|
|
private void UndoMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Undo();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RedoMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Redo();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CutMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Cut();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CopyMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Copy();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PasteMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Paste();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DeleteMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Delete();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectAllMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-06-25 19:33:41 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.SelectAll();
|
2014-06-25 19:33:41 +10:00
|
|
|
}
|
2014-06-25 19:27:21 +10:00
|
|
|
}
|
2014-06-25 19:49:55 +10:00
|
|
|
|
|
|
|
|
private void PrintToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Print();
|
2014-06-25 19:49:55 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-25 17:19:08 +10:00
|
|
|
|
2019-08-14 01:16:21 +01:00
|
|
|
private async void ShowDevToolsMenuItemClick(object sender, EventArgs e)
|
2014-08-25 17:19:08 +10:00
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2019-08-14 01:16:21 +01:00
|
|
|
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
|
|
|
|
|
if (!isDevToolsOpen)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.ShowDevTools();
|
2019-08-14 01:16:21 +01:00
|
|
|
}
|
2014-08-25 17:19:08 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-26 17:54:23 +10:00
|
|
|
|
2019-08-14 01:16:21 +01:00
|
|
|
private async void ShowDevToolsDockedMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
|
|
|
|
|
if (!isDevToolsOpen)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var chromiumWebBrowser = control.Browser as ChromiumWebBrowser;
|
2021-11-26 10:09:59 +10:00
|
|
|
if (chromiumWebBrowser != null && chromiumWebBrowser.LifeSpanHandler != null)
|
2019-08-14 01:16:21 +01:00
|
|
|
{
|
|
|
|
|
control.ShowDevToolsDocked();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void CloseDevToolsMenuItemClick(object sender, EventArgs e)
|
2014-08-26 17:54:23 +10:00
|
|
|
{
|
2014-10-13 15:43:33 +10:00
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2019-08-14 11:11:10 +10:00
|
|
|
//Check if DevTools is open before closing, this isn't strictly required
|
|
|
|
|
//If DevTools isn't open and you call CloseDevTools it's a No-Op, so prefectly
|
|
|
|
|
//safe to call without checking
|
2019-08-14 01:16:21 +01:00
|
|
|
var isDevToolsOpen = await control.CheckIfDevToolsIsOpenAsync();
|
|
|
|
|
if (isDevToolsOpen)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.CloseDevTools();
|
2019-08-14 01:16:21 +01:00
|
|
|
}
|
2014-10-13 15:43:33 +10:00
|
|
|
}
|
2014-09-26 16:53:50 +10:00
|
|
|
}
|
2015-04-16 15:15:01 -04:00
|
|
|
|
2015-04-16 18:51:37 -04:00
|
|
|
private void ZoomInToolStripMenuItemClick(object sender, EventArgs e)
|
2015-04-16 15:15:01 -04:00
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var task = control.Browser.GetZoomLevelAsync();
|
2015-05-21 11:34:42 +10:00
|
|
|
|
|
|
|
|
task.ContinueWith(previous =>
|
|
|
|
|
{
|
2016-05-24 19:09:56 +10:00
|
|
|
if (previous.Status == TaskStatus.RanToCompletion)
|
2015-05-21 11:34:42 +10:00
|
|
|
{
|
|
|
|
|
var currentLevel = previous.Result;
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.SetZoomLevel(currentLevel + ZoomIncrement);
|
2015-05-21 11:34:42 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
|
|
|
|
|
}
|
|
|
|
|
}, TaskContinuationOptions.ExecuteSynchronously);
|
2015-04-16 15:15:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 18:51:37 -04:00
|
|
|
private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e)
|
2015-04-16 15:15:01 -04:00
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var task = control.Browser.GetZoomLevelAsync();
|
2015-05-21 11:34:42 +10:00
|
|
|
task.ContinueWith(previous =>
|
|
|
|
|
{
|
2016-05-24 19:09:56 +10:00
|
|
|
if (previous.Status == TaskStatus.RanToCompletion)
|
2015-04-27 18:20:32 -04:00
|
|
|
{
|
2015-05-21 11:34:42 +10:00
|
|
|
var currentLevel = previous.Result;
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.SetZoomLevel(currentLevel - ZoomIncrement);
|
2015-05-21 11:34:42 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
|
|
|
|
|
}
|
|
|
|
|
}, TaskContinuationOptions.ExecuteSynchronously);
|
2015-04-16 15:15:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 18:51:37 -04:00
|
|
|
private void CurrentZoomLevelToolStripMenuItemClick(object sender, EventArgs e)
|
2015-04-16 15:15:01 -04:00
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var task = control.Browser.GetZoomLevelAsync();
|
2015-05-21 11:34:42 +10:00
|
|
|
task.ContinueWith(previous =>
|
|
|
|
|
{
|
2016-05-24 19:09:56 +10:00
|
|
|
if (previous.Status == TaskStatus.RanToCompletion)
|
2015-05-21 11:34:42 +10:00
|
|
|
{
|
|
|
|
|
var currentLevel = previous.Result;
|
|
|
|
|
MessageBox.Show("Current ZoomLevel: " + currentLevel.ToString());
|
|
|
|
|
}
|
|
|
|
|
else
|
2015-04-27 18:20:32 -04:00
|
|
|
{
|
2015-05-21 11:34:42 +10:00
|
|
|
MessageBox.Show("Unexpected failure of calling CEF->GetZoomLevelAsync: " + previous.Exception.ToString());
|
|
|
|
|
}
|
|
|
|
|
}, TaskContinuationOptions.HideScheduler);
|
2015-04-16 15:15:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-12-14 14:54:04 -05:00
|
|
|
|
|
|
|
|
private void DoesActiveElementAcceptTextInputToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var frame = control.Browser.GetFocusedFrame();
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2015-12-30 09:57:34 +10:00
|
|
|
//Execute extension method
|
|
|
|
|
frame.ActiveElementAcceptsTextInput().ContinueWith(task =>
|
|
|
|
|
{
|
|
|
|
|
string message;
|
|
|
|
|
var icon = MessageBoxIcon.Information;
|
|
|
|
|
if (task.Exception == null)
|
2015-12-14 14:54:04 -05:00
|
|
|
{
|
2015-12-30 09:57:34 +10:00
|
|
|
var isText = task.Result;
|
|
|
|
|
message = string.Format("The active element is{0}a text entry element.", isText ? " " : " not ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message = string.Format("Script evaluation failed. {0}", task.Exception.Message);
|
|
|
|
|
icon = MessageBoxIcon.Error;
|
|
|
|
|
}
|
2015-12-14 14:54:04 -05:00
|
|
|
|
2015-12-30 09:57:34 +10:00
|
|
|
MessageBox.Show(message, "Does active element accept text input", MessageBoxButtons.OK, icon);
|
|
|
|
|
});
|
2015-12-14 14:54:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-30 09:57:34 +10:00
|
|
|
private void DoesElementWithIdExistToolStripMenuItemClick(object sender, EventArgs e)
|
2015-12-14 14:54:04 -05:00
|
|
|
{
|
|
|
|
|
// This is the main thread, it's safe to create and manipulate form
|
|
|
|
|
// UI controls.
|
2015-12-30 09:57:34 +10:00
|
|
|
var dialog = new InputBox
|
|
|
|
|
{
|
|
|
|
|
Instructions = "Enter an element ID to find.",
|
|
|
|
|
Title = "Find an element with an ID"
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-14 14:54:04 -05:00
|
|
|
dialog.OnEvaluate += (senderDlg, eDlg) =>
|
2015-12-30 09:57:34 +10:00
|
|
|
{
|
|
|
|
|
// This is also the main thread.
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
2015-12-14 14:54:04 -05:00
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var frame = control.Browser.GetFocusedFrame();
|
2015-12-30 09:57:34 +10:00
|
|
|
|
|
|
|
|
//Execute extension method
|
|
|
|
|
frame.ElementWithIdExists(dialog.Value).ContinueWith(task =>
|
2015-12-14 14:54:04 -05:00
|
|
|
{
|
2015-12-30 09:57:34 +10:00
|
|
|
// Now we're not on the main thread, perhaps the
|
|
|
|
|
// Cef UI thread. It's not safe to work with
|
|
|
|
|
// form UI controls or to block this thread.
|
|
|
|
|
// Queue up a delegate to be executed on the
|
|
|
|
|
// main thread.
|
|
|
|
|
BeginInvoke(new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
string message;
|
|
|
|
|
if (task.Exception == null)
|
2015-12-14 14:54:04 -05:00
|
|
|
{
|
2015-12-30 09:57:34 +10:00
|
|
|
message = task.Result.ToString();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message = string.Format("Script evaluation failed. {0}", task.Exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dialog.Result = message;
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-14 14:54:04 -05:00
|
|
|
dialog.Show(this);
|
|
|
|
|
}
|
2015-12-28 14:29:25 -05:00
|
|
|
|
|
|
|
|
private void GoToDemoPageToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.LoadUrl("custom://cefsharp/ScriptedMethodsTest.html");
|
2015-12-28 14:29:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InjectJavascriptCodeToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var frame = control.Browser.GetFocusedFrame();
|
2015-12-30 09:57:34 +10:00
|
|
|
|
|
|
|
|
//Execute extension method
|
|
|
|
|
frame.ListenForEvent("test-button", "click");
|
2015-12-28 14:29:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-12 12:39:38 +10:00
|
|
|
|
2016-05-28 07:22:13 +10:00
|
|
|
private async void PrintToPdfToolStripMenuItemClick(object sender, EventArgs e)
|
2016-04-12 12:39:38 +10:00
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
var dialog = new SaveFileDialog
|
|
|
|
|
{
|
|
|
|
|
DefaultExt = ".pdf",
|
|
|
|
|
Filter = "Pdf documents (.pdf)|*.pdf"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
var success = await control.Browser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
|
2016-04-12 12:39:38 +10:00
|
|
|
{
|
|
|
|
|
MarginType = CefPdfPrintMarginType.Custom,
|
|
|
|
|
MarginBottom = 10,
|
|
|
|
|
MarginTop = 0,
|
|
|
|
|
MarginLeft = 20,
|
|
|
|
|
MarginRight = 10
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Pdf was saved to " + dialog.FileName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Unable to save Pdf, check you have write permissions to " + dialog.FileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-28 07:22:13 +10:00
|
|
|
|
|
|
|
|
private void OpenDataUrlToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
const string html = "<html><head><title>Test</title></head><body><h1>Html Encoded in URL!</h1></body></html>";
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.LoadHtml(html, false);
|
2016-05-28 07:22:13 +10:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-24 09:02:04 +10:00
|
|
|
|
|
|
|
|
private void OpenHttpBinOrgToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.LoadUrl("https://httpbin.org/");
|
2017-02-24 09:02:04 +10:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-28 13:30:58 +10:00
|
|
|
|
|
|
|
|
private void RunFileDialogToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2022-05-17 19:51:08 +10:00
|
|
|
control.Browser.GetBrowserHost().RunFileDialog(CefFileDialogMode.Open, "Open", null, new List<string> { "*.*" }, new RunFileDialogCallback());
|
2018-12-28 13:30:58 +10:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-31 13:12:10 +10:00
|
|
|
|
2019-07-03 20:40:25 +10:00
|
|
|
private void JavascriptBindingStressTestToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.LoadUrl(CefExample.BindingTestUrl);
|
|
|
|
|
control.Browser.LoadingStateChanged += (o, args) =>
|
2019-07-03 20:40:25 +10:00
|
|
|
{
|
|
|
|
|
if (args.IsLoading == false)
|
|
|
|
|
{
|
|
|
|
|
Task.Delay(10000).ContinueWith(t =>
|
|
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
if (control.Browser != null)
|
2019-07-03 20:40:25 +10:00
|
|
|
{
|
2021-11-30 13:46:13 +10:00
|
|
|
control.Browser.Reload();
|
2019-07-03 20:40:25 +10:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-07 13:18:42 +10:00
|
|
|
|
|
|
|
|
private void HideScrollbarsToolStripMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
|
|
|
|
|
_ = control?.HideScrollbarsAsync();
|
|
|
|
|
}
|
2022-05-03 11:28:25 +10:00
|
|
|
|
|
|
|
|
private async void TakeScreenShotMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = GetCurrentTabControl();
|
|
|
|
|
|
|
|
|
|
if(control == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var chromiumWebBrowser = (ChromiumWebBrowser)control.Browser;
|
|
|
|
|
|
|
|
|
|
var contentSize = await chromiumWebBrowser.GetContentSizeAsync();
|
|
|
|
|
|
|
|
|
|
//Capture current scrollable area
|
|
|
|
|
var viewPort = new DevTools.Page.Viewport
|
|
|
|
|
{
|
|
|
|
|
Width = contentSize.Width,
|
|
|
|
|
Height = contentSize.Height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var data = await chromiumWebBrowser.CaptureScreenshotAsync(viewPort: viewPort, captureBeyondViewport: true);
|
|
|
|
|
|
|
|
|
|
// Make a file to save it to (e.g. C:\Users\[user]\Desktop\CefSharp screenshot.png)
|
|
|
|
|
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");
|
|
|
|
|
|
|
|
|
|
File.WriteAllBytes(screenshotPath, data);
|
|
|
|
|
|
|
|
|
|
// Tell Windows to launch the saved image.
|
|
|
|
|
Process.Start(new ProcessStartInfo(screenshotPath)
|
|
|
|
|
{
|
|
|
|
|
// UseShellExecute is false by default on .NET Core.
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
});
|
|
|
|
|
}
|
2010-12-03 16:22:41 +00:00
|
|
|
}
|
|
|
|
|
}
|