Solved

Host winforms webBrowser in WPF app

Posted on 2013-06-27
5
1,860 Views
Last Modified: 2014-01-31
I am creating a kiosk app for use internally in our business. I need to be able to go directly to printer without invoking the print dialog when someone clicks the "Print" button. That said, I learned that the "easiest" way to do this is to use the winforms version of the webBrowser control, and simply call

webBrowser.Print();

Well, so much for easy, I can't even get the winforms webBrowser control to work in my WPF app.

If you have a better way of doing this, please let me know.

Here's my XAML:

   <Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="Manual" Left="0" Top="0" Cursor="None" Topmost="True">
    <Grid Name="grid1">
        <WindowsFormsHost>
            <wf:WebBrowser WebBrowserShortcutsEnabled="False" Name="webBrowser" Url="http://test.outerdata.com"></wf:WebBrowser>
        </WindowsFormsHost>
        <Button Content="Print" Height="23" HorizontalAlignment="Left" Margin="356,232,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Canvas.ZIndex="100" />
    </Grid>
</Window>

Open in new window


Here's my C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;

namespace MyApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            System.Windows.Forms.Integration.WindowsFormsHost host =
            new System.Windows.Forms.Integration.WindowsFormsHost();

            System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();

            host.Child = webBrowser;

            this.grid1.Children.Add(host);

            InitializeComponent();
            ReadFromAppConfig();
            this.Activate();

        }

        private void ReadFromAppConfig()
        {
            this.Width = Convert.ToDouble(ConfigurationManager.AppSettings["mywidth"]);
            this.Height = Convert.ToDouble(ConfigurationManager.AppSettings["myheight"]);
            this.WindowStartupLocation = WindowStartupLocation.Manual;
            this.Left = Convert.ToDouble(ConfigurationManager.AppSettings["myleft"]);
            this.Top = Convert.ToDouble(ConfigurationManager.AppSettings["mytop"]);
            this.webBrowser.Url = new Uri(ConfigurationManager.AppSettings["mysource"]);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            webBrowser.Print();
        }
    }
}

Open in new window

0
Comment
Question by:PhatFrogDev
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 39300406
Hi,

WPF app itself has Web Browser control. Why don't you use that instead of Windows Forms?
0
 

Author Comment

by:PhatFrogDev
ID: 39348912
Because the WPF Web Browser control does not have webBrowser.Print(); method.
0
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
ID: 39356544
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to dynamically set the form action using jQuery.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question