Solved

Host winforms webBrowser in WPF app

Posted on 2013-06-27
5
1,895 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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