简体   繁体   中英

C# WPF window - internet explorer

I need help from someone who work in C#. I'm trying to achieve WPF application that open internet explorer on URL that i supplement him as a parameter. So in perfect world i would like run something like this:

 \\path\window.exe X x Y "URL" "Title" 

Where X,Y is windows height and width, URL and Title is a title of that window. My knowledge of C# is nonexistent so after extensive use of google i manage to get this XAML:

<Window x:Class="WPFWebControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="My Web Browser" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="Auto" Width="Auto">
    <Grid>
        <WebBrowser Height="Auto" HorizontalAlignment="Left" Margin="0,28,0,0" Name="MyWebBrowser" VerticalAlignment="Top" Width="Auto" LoadCompleted="MyWebBrowser_LoadCompleted" />
        <TextBox Height="23" Margin="40,5,0,0" Name="MyTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="708" />
        <Button Content="Go" Margin="10,5,0,476" Name="MyGo" ToolTip="Go" Click="MyGo_Click" RenderTransformOrigin="-0.76,0.565" HorizontalAlignment="Left" Width="25" />
    </Grid>
</Window>

and this C# code:

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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFWebControl
{    /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                MyWebBrowser.Source = new Uri("http://www.google.com");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void MyGo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MyWebBrowser.Source = new Uri("http://" + MyTextBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {}
    }
}

Now i need some advice how can i set window size, url and title via parameters so i can get rid of that address textbox and button.

You could open a new window and set a WebBrowser as its Content , eg:

WebBrowser browser = new WebBrowser();
Window win = new Window();
win.Content = browser;
win.Height = 100;
win.Width = 100;
win.Show();

win.Title = "title...";
browser.Navigate("http://google.com");

If you want your application to be able to accept command line arguments, you could remove the StartupUri attribute from your App.xaml and override the OnStartup method of your App.xaml.cs :

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    try
    {
        double width = Convert.ToDouble(e.Args[0]);
        double height = Convert.ToDouble(e.Args[1]);
        string title = e.Args[2];
        string url = e.Args[3];

        WebBrowser browser = new WebBrowser();
        Window win = new Window();
        win.Content = browser;
        win.Height = height;
        win.Width = width;
        win.Show();

        win.Title = title;
        browser.Navigate(new Uri(url, UriKind.Absolute));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Usage:

WpfApplication1.exe 100 100 "title..." "http://google.com"

You should be able to use

string[] args = Environment.GetCommandLineArgs();

Or, change teh startup uri in App.xaml (remove it) and instead create your own startup like

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //Manually create and show your window, passing the arguments
    }
}

etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM