Skip to content

Instantly share code, notes, and snippets.

@privatedev11
Last active October 26, 2022 17:12
Show Gist options
  • Save privatedev11/9d61b17f13c4167dddeca13d5bd20640 to your computer and use it in GitHub Desktop.
Save privatedev11/9d61b17f13c4167dddeca13d5bd20640 to your computer and use it in GitHub Desktop.
Sleepyheads for Windows framework

Sleepyheads for Windows framework

THIS IS OUTDATED. SLEEPYHEADS NOW USES ELECTRON.

Sleepyheads for Windows uses the CEFSharp NuGet package for WinForms (which is based on C#) to display the website using a webview element. CEFSharp is a NuGet package which allows you do display webpages and HTML documents in your app using the Chromium Embedded Framework.

Here is the framework of the code.

Start a Windows Forms project in Visual Studio. I have used .Net Framework, not .Net Core, so use that for this tutorial. You need to get the package from NuGet. It can be found here: https://www.nuget.org/packages/CefSharp.WinForms/

Now we can get started.

Add these imports to the 'using' section of the form

using CefSharp;
using CefSharp.WinForms; 

We now need to add the webview element, which can be done by pasting this code. Paste it into the public partial class element

public ChromiumWebBrowser chromeBrowser;

        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("https://privatedev11.github.io/Sleepyheads/app");
            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();

Remember to change the url to your url!

Your entire file should look a bit like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace Sleepyheads
{


    public partial class Form1 : Form
    {
        public ChromiumWebBrowser chromeBrowser;

        public Form1()
        {
            InitializeComponent();
            // Start the browser after initialize global component
            InitializeChromium();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            chromeBrowser = new ChromiumWebBrowser("https://privatedev11.github.io/Sleepyheads/app");
            // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser);
            chromeBrowser.Dock = DockStyle.Fill;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }
    }
}

The window should look a little bit like this: Screenshot-9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment