mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 19:25:01 +00:00
468 lines
15 KiB
C#
468 lines
15 KiB
C#
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
|
|
//
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
using System;
|
|
using System.Windows.Forms;
|
|
using CefSharp.Example;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CefSharp.WinForms.Example
|
|
{
|
|
public partial class BrowserForm : Form
|
|
{
|
|
private const string DefaultUrlForAddedTabs = "https://www.google.com";
|
|
|
|
// Default to a small increment:
|
|
private const double ZoomIncrement = 0.10;
|
|
|
|
public BrowserForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
|
|
Text = "CefSharp.WinForms.Example - " + bitness;
|
|
WindowState = FormWindowState.Maximized;
|
|
|
|
AddTab(CefExample.DefaultUrl);
|
|
|
|
//Only perform layout when control has completly finished resizing
|
|
ResizeBegin += (s, e) => SuspendLayout();
|
|
ResizeEnd += (s, e) => ResumeLayout(true);
|
|
}
|
|
|
|
private void AddTab(string url, int? insertIndex = null)
|
|
{
|
|
browserTabControl.SuspendLayout();
|
|
|
|
var browser = new BrowserTabUserControl(AddTab, url)
|
|
{
|
|
Dock = DockStyle.Fill,
|
|
};
|
|
|
|
var tabPage = new TabPage(url)
|
|
{
|
|
Dock = DockStyle.Fill
|
|
};
|
|
|
|
//This call isn't required for the sample to work.
|
|
//It's sole purpose is to demonstrate that #553 has been resolved.
|
|
browser.CreateControl();
|
|
|
|
tabPage.Controls.Add(browser);
|
|
|
|
if (insertIndex == null)
|
|
{
|
|
browserTabControl.TabPages.Add(tabPage);
|
|
}
|
|
else
|
|
{
|
|
browserTabControl.TabPages.Insert(insertIndex.Value, tabPage);
|
|
}
|
|
|
|
//Make newly created tab active
|
|
browserTabControl.SelectedTab = tabPage;
|
|
|
|
browserTabControl.ResumeLayout(true);
|
|
}
|
|
|
|
private void ExitMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
ExitApplication();
|
|
}
|
|
|
|
private void ExitApplication()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
private void AboutToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
new AboutBox().ShowDialog();
|
|
}
|
|
|
|
private void FindMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.ShowFind();
|
|
}
|
|
}
|
|
|
|
private void CopySourceToClipBoardAsyncClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.CopySourceToClipBoardAsync();
|
|
}
|
|
}
|
|
|
|
private BrowserTabUserControl GetCurrentTabControl()
|
|
{
|
|
if (browserTabControl.SelectedIndex == -1)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var tabPage = browserTabControl.Controls[browserTabControl.SelectedIndex];
|
|
var control = (BrowserTabUserControl)tabPage.Controls[0];
|
|
|
|
return control;
|
|
}
|
|
|
|
private void NewTabToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
AddTab(DefaultUrlForAddedTabs);
|
|
}
|
|
|
|
private void CloseTabToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
if(browserTabControl.Controls.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var currentIndex = browserTabControl.SelectedIndex;
|
|
|
|
var tabPage = browserTabControl.Controls[currentIndex];
|
|
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Dispose();
|
|
}
|
|
|
|
browserTabControl.Controls.Remove(tabPage);
|
|
|
|
tabPage.Dispose();
|
|
|
|
browserTabControl.SelectedIndex = currentIndex - 1;
|
|
|
|
if (browserTabControl.Controls.Count == 0)
|
|
{
|
|
ExitApplication();
|
|
}
|
|
}
|
|
|
|
private void UndoMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Undo();
|
|
}
|
|
}
|
|
|
|
private void RedoMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Redo();
|
|
}
|
|
}
|
|
|
|
private void CutMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Cut();
|
|
}
|
|
}
|
|
|
|
private void CopyMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Copy();
|
|
}
|
|
}
|
|
|
|
private void PasteMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Paste();
|
|
}
|
|
}
|
|
|
|
private void DeleteMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Delete();
|
|
}
|
|
}
|
|
|
|
private void SelectAllMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.SelectAll();
|
|
}
|
|
}
|
|
|
|
private void PrintToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Print();
|
|
}
|
|
}
|
|
|
|
private void ShowDevToolsMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.ShowDevTools();
|
|
|
|
//Example below shows how to use a control to host DevTools
|
|
//(in this case it's added as a new TabPage)
|
|
//var tabPage = new TabPage("DevTools")
|
|
//{
|
|
// Dock = DockStyle.Fill
|
|
//};
|
|
|
|
//var panel = new Panel
|
|
//{
|
|
// Dock = DockStyle.Fill
|
|
//};
|
|
|
|
////We need to call CreateControl as we need the Handle later
|
|
//panel.CreateControl();
|
|
|
|
//tabPage.Controls.Add(panel);
|
|
|
|
//browserTabControl.TabPages.Add(tabPage);
|
|
|
|
////Make newly created tab active
|
|
//browserTabControl.SelectedTab = tabPage;
|
|
|
|
////Grab the client rect
|
|
//var rect = panel.ClientRectangle;
|
|
//var webBrowser = control.Browser;
|
|
//var browser = webBrowser.GetBrowser().GetHost();
|
|
//var windowInfo = new WindowInfo();
|
|
////DevTools becomes a child of the panel, we use it's dimesions
|
|
//windowInfo.SetAsChild(panel.Handle, rect.Left, rect.Top, rect.Right, rect.Bottom);
|
|
////Show DevTools in our panel
|
|
//browser.ShowDevTools(windowInfo);
|
|
}
|
|
}
|
|
|
|
private void CloseDevToolsMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.CloseDevTools();
|
|
}
|
|
}
|
|
|
|
private void ZoomInToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var task = control.Browser.GetZoomLevelAsync();
|
|
|
|
task.ContinueWith(previous =>
|
|
{
|
|
if (previous.IsCompleted)
|
|
{
|
|
var currentLevel = previous.Result;
|
|
control.Browser.SetZoomLevel(currentLevel + ZoomIncrement);
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
|
|
}
|
|
}, TaskContinuationOptions.ExecuteSynchronously);
|
|
}
|
|
}
|
|
|
|
private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var task = control.Browser.GetZoomLevelAsync();
|
|
task.ContinueWith(previous =>
|
|
{
|
|
if (previous.IsCompleted)
|
|
{
|
|
var currentLevel = previous.Result;
|
|
control.Browser.SetZoomLevel(currentLevel - ZoomIncrement);
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
|
|
}
|
|
}, TaskContinuationOptions.ExecuteSynchronously);
|
|
}
|
|
}
|
|
|
|
private void CurrentZoomLevelToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var task = control.Browser.GetZoomLevelAsync();
|
|
task.ContinueWith(previous =>
|
|
{
|
|
if (previous.IsCompleted)
|
|
{
|
|
var currentLevel = previous.Result;
|
|
MessageBox.Show("Current ZoomLevel: " + currentLevel.ToString());
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Unexpected failure of calling CEF->GetZoomLevelAsync: " + previous.Exception.ToString());
|
|
}
|
|
}, TaskContinuationOptions.HideScheduler);
|
|
}
|
|
}
|
|
|
|
private void DoesActiveElementAcceptTextInputToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var frame = control.Browser.GetFocusedFrame();
|
|
|
|
//Execute extension method
|
|
frame.ActiveElementAcceptsTextInput().ContinueWith(task =>
|
|
{
|
|
string message;
|
|
var icon = MessageBoxIcon.Information;
|
|
if (task.Exception == null)
|
|
{
|
|
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;
|
|
}
|
|
|
|
MessageBox.Show(message, "Does active element accept text input", MessageBoxButtons.OK, icon);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void DoesElementWithIdExistToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
// This is the main thread, it's safe to create and manipulate form
|
|
// UI controls.
|
|
var dialog = new InputBox
|
|
{
|
|
Instructions = "Enter an element ID to find.",
|
|
Title = "Find an element with an ID"
|
|
};
|
|
|
|
dialog.OnEvaluate += (senderDlg, eDlg) =>
|
|
{
|
|
// This is also the main thread.
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var frame = control.Browser.GetFocusedFrame();
|
|
|
|
//Execute extension method
|
|
frame.ElementWithIdExists(dialog.Value).ContinueWith(task =>
|
|
{
|
|
// 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)
|
|
{
|
|
message = task.Result.ToString();
|
|
}
|
|
else
|
|
{
|
|
message = string.Format("Script evaluation failed. {0}", task.Exception.Message);
|
|
}
|
|
|
|
dialog.Result = message;
|
|
}));
|
|
});
|
|
}
|
|
};
|
|
|
|
dialog.Show(this);
|
|
}
|
|
|
|
private void GoToDemoPageToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
control.Browser.Load("custom://cefsharp/ScriptedMethodsTest.html");
|
|
}
|
|
}
|
|
|
|
private void InjectJavascriptCodeToolStripMenuItemClick(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var frame = control.Browser.GetFocusedFrame();
|
|
|
|
//Execute extension method
|
|
frame.ListenForEvent("test-button", "click");
|
|
}
|
|
}
|
|
|
|
private async void printToPdfToolStripMenuItem_Click(object sender, EventArgs e)
|
|
{
|
|
var control = GetCurrentTabControl();
|
|
if (control != null)
|
|
{
|
|
var dialog = new SaveFileDialog
|
|
{
|
|
DefaultExt = ".pdf",
|
|
Filter = "Pdf documents (.pdf)|*.pdf"
|
|
};
|
|
|
|
if (dialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
var success = await control.Browser.PrintToPdfAsync(dialog.FileName, new PdfPrintSettings
|
|
{
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|