Introduction
This short tutorial explains how to add a Sciter-based control to a WinForms application. Sciter is a cross-platform/single-binary HTML engine, free for commercial use.
You can create entire applications solely with Sciter, or you can embed it as a child control of a WinForms app. To work with Sciter in C#, we need SciterSharp library which offers .NET bindings over the official C/C++ SDK headers, and I am the author.
If you are new to Sciter, read my introductory walkthrough article.
This article is a KISS explanation of how to use Sciter in WinForms as a child control. Three methods are presented, starting with the simplest one.
Make sure to have Sciter SDK (grab it here) because we need to add Sciter DLL to our project.
Method 1 - Sciter Bootstrap
Here we use Sciter Bootstrap, an on-line automation tool to create the WinForms + Sciter project for us. If you are starting from scratch, use this method. The project comes with SciterSharp NuGeT already installed, and with boilerplate code.
- Go to http://misoftware.com.br/Bootstrap/Download
- Type the title of your project
- Select the radiobox WinForms in the C# - Classic desktop section
- Click the Download button
- Extract and then just open the solution in Visual Studio
- Compile and Run!
Method 2 - Code Based
If you already have a WinForms project, or don't want to use Sciter Bootstrap, you have to manually install SciterSharp and add sciter.dll to your project.
- Open or create a new WinForms project.
- Install SciterSharp NuGeT:
PM> Install-Package SciterSharpWindows
- Add sciter.dll to your project (right-click the project and 'Add / Existing item..').
Note: You must add the appropriated x86 or x64 version of sciter.dll, depending on the 'Platform target' you choose for your project in its Properties / Build tab. - In the properties window of sciter.dll, configure 'Copy to Output Directory' property to 'Copy Always', this way sciter DLL can be found when running the project:
- Edit the code-behind of Form1 (click Form1.cs and press F7), and make it to look like:
using System;
using System.Drawing;
using System.Windows.Forms;
using SciterSharp.WinForms;
namespace ProcessTable
{
public partial class Form1 : Form
{
private SciterControl _sciter_ctrl;
public Form1()
{
InitializeComponent();
_sciter_ctrl = new SciterControl();
_sciter_ctrl.Location = new Point(15, 15);
_sciter_ctrl.Size = new Size(500, 350);
_sciter_ctrl.HandleCreated += SciterControl1_HandleCreated;
Controls.Add(_sciter_ctrl);
}
private void SciterControl1_HandleCreated(object sender, EventArgs e)
{
_sciter_ctrl.SciterWnd.LoadHtml("<h1>Hello World!</h1>");
}
}
}
Note that we are simply adding a SciterControl
directly to the Controls
collection of the main form. You might want to tweak the position, size and parent control if desired.
For loading the actual HTML of our control, we need to wait for its window handle to be created, that's why initialization must be done at the HandleCreated
event.
- Compile and run, output should look like:
Method 3 - Toolbox Based
If you prefer the traditional way of drag'n'dropping the control from the toolbox for WYSIWYG composing your UI, follow this method. In the designer, you can add as many controls as you want, and you can also dock the control in the parent.
This first requires to configure the Toolbox so SciterControl appears in the list.
- As in Method 2, you must install SciterSharp NuGeT and add sciter.dll to your project
- Open the designer of
Form1
- Open the toolbox, right-click it and select Choose items, click 'Browse' and then locate SciterSharpWindows.dll; SciterControl should appear in the toolbox list.
- Drag'n'drop from the toolbox to the designer, duuuuu...
Note however that it ends not being truly WYSIWYG since you are not able to preview the Sciter control with the loaded HTML at compose time (naturally, you can only load it at runtime). Instead, what you get is the following:
Note that in the image I've added two Sciter controls (the right one is docked) and they just show some instructions of what you should do to manage this control, like loading the HTML.
System.TypeInitializationException:
If you get this error in the designer:
..that's because VS can't find sciter.dll. You need to download Sciter's SDK, and add /bin/32 folder to your PATH. You might need to restart Visual Studio.
Epilogue
Visit my blog at http://misoftware.com.br where I have a regular series of tutorials covering Sciter development in general.
Ramon has a bachelor in Information Systems at University of Caxias do Sul. He started his career in the creative area, working with web design, and then evolved to work with a more hardcore area of control systems engineering while making C#/.NET systems to automate every kind of process. This was when he discovered his passion for the low-level world, working with C, C++ and D development.
Check my things at http://misoftware.com.br/