Solved

Host winforms webBrowser in WPF app

Posted on 2013-06-27
5
1,963 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

636 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