Search code examples
c#winapinode-webkitwin32guiwindow-managers

How to embed web application into windows app with custom behavior


I need to embed a standard webapp inside a windows app container.

The container just needs to be a wrapper around a webkit or similar rendering engine (I would like to avoid using IE rendering engine if possible) and it would contain some minimal window management logic - things like being borderless with no titlebars, being able to fix position and size, and custom overlay/always-on-top rules based on currently focused window.

I've been looking at node-webkit and it seems to fit the bill as far as containing a webapp is concerned, but I'm not sure I would be able to do the latter.

Can this be done with node-webkit, is there some other approach that would fit my use case better? I have absolutely no idea how windows app development is done, so I would appreciate some help.


Solution

  • If you are using WPF, you can create you window with something like this (xaml):

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowStyle="None"
            Title="MainWindow" Left="100" Top="200" Height="350" Width="525" PreviewKeyDown="Window_PreviewKeyDown">
        <Grid x:Name="grdBrowserHost">
        </Grid>
    </Window>
    

    WindowStyle attr None means "borderless with no titlebars" window. Left and Top are for position, and Width and Height are self-explanatory. All those properties can be accessed via code-behind with simple this.Width, etc... PreviewKeyDown I put here because in this example (as you will see), Topmost property will be changed from code behind (dynamically).

    Code-behind should look something like using System;

    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApplication2
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();
                // you can put any other uri here, or better make browser field and navigate to desired uri on some event
                browser.Navigate(new Uri("http://www.blic.rs/")); 
                grdBrowserHost.Children.Add(browser);
            }
    
            private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else
                {
                    this.Topmost = !this.Topmost;
                }
            }
        }
    }
    

    In this example I have created default WebBrowser control for showing html. If you want some other web rendering engine you must find 3rd-part control and include it in your references. For webkit, you can check How to use WebKit browser control in WPF