Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Host winforms webBrowser in WPF app

Posted on 2013-06-27
5
Medium Priority
?
2,041 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 2000 total points
ID: 39356544
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

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. …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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