Sample Project

The sample project includes:

Requirements

  • .NET Framework 4.0 or higher
  • Visual Studio 2010 or higher

License

Licensed for testing and evaluation purposes. You can use the source code of the tutorial if you are a licensed user of DayPilot Pro for ASP.NET WebForms. Buy a license.

Features

DayPilot is a calendar/scheduling control for ASP.NET. If you want to use it in a desktop WinForms application you need to use a WebBrowser control and an embedded web server.

Embedded ASP.NET Web Server (Cassini)

Cassini is a simple web server created by Microsoft and made available under the Microsoft Public License

It's no longer available for download from Microsoft but a few forks are available and one of them is hosted at codeplex (Cassinidev).

We will use the core part of Cassini to run our web server on localhost.

Sample Visual Studio Solution

daypilot-desktop-visual-studio-solution.png

The sample Visual Studio 2010 solution includes three projects:

  • App (a web project with DayPilot)
  • WebServer (Cassini web server, compiled as a dll)
  • Desktop (the wrapper WinForms application, running App using WebServer)

Desktop Application

daypilot-desktop-form1.png

The desktop application is a simple Form with a WebBrowser control.

Upon loading, we start the embedded web server and navigate to the home page:

 private void Form1_Load(object sender, EventArgs e)
{
  string appPath = Path.GetFullPath("..\\..\\..\\App");

  Server server = new Server(0, "/", appPath);
  server.Start();

  webBrowser1.Url = new Uri(server.RootUrl);
}

The Cassini source was updated a bit to support dynamic port assignment:

  • It chooses a free port if you use 0 as the port number.
  • The actual port number is part of RootUrl string.

WebBrowser Control and IE Rendering Mode

By default, the Internet Explorer used in the WebBrowser control runs in IE7 standards mode - see  More IE8 Extensibility Improvements [blogs.msdn.com].

In order to avoid IE7 bugs, you can switch to a higher rendering mode. We are switching to IE8 standards mode by adding the application name and rendering mode code (8000) to HKCU\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION registry key:

 public void BrowserEmulation()
{
  String application = Process.GetCurrentProcess().ProcessName + ".exe";
  RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", RegistryKeyPermissionCheck.ReadWriteSubTree);
  if (key != null)
  {
    try
    {
      key.SetValue(application, 8000, RegistryValueKind.DWord);
      key.Close();
    }
    catch (Exception ex)
    {
    }
  }
}

See also codes for other rendering modes [msdn.microsoft.com].