Solved

Host winforms webBrowser in WPF app

Posted on 2013-06-27
5
1,808 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
Comment Utility
Hi,

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

Author Comment

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

Accepted Solution

by:
Roopesh Reddy earned 500 total points
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now