2017-10-02 1 views
1

J'ai besoin de l'aide de quelqu'un qui travaille en C#. J'essaye de réaliser l'application WPF qui ouvre l'explorateur internet sur l'URL que je complète en paramètre. Donc, dans le monde parfait je voudrais courir quelque chose comme ceci:Fenêtre C# WPF - Internet Explorer

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

où X, Y est la hauteur des fenêtres et la largeur, URL et le titre est un titre de cette fenêtre. Ma connaissance de C# est inexistante si après une longue utilisation de google je parviens à obtenir ce 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> 

et ce code 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.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) 
     {} 
    } 
} 

Maintenant, je besoin de quelques conseils comment puis-je définir la taille de la fenêtre, url et le titre via les paramètres afin que je puisse me débarrasser de cette zone de texte et le bouton de l'adresse.

Répondre

1

Vous pouvez ouvrir une nouvelle fenêtre et définir un WebBrowser comme Content, par exemple:

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"); 

Si vous voulez que votre demande soit en mesure d'accepter les arguments de ligne de commande, vous pouvez supprimer l'attribut StartupUri de votre App.xaml et remplacer la méthode OnStartup de votre 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); 
    } 
} 

Utilisation:

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

Merci beaucoup de son fonctionnement. –

0

Vous devriez pouvoir utiliser

string[] args = Environment.GetCommandLineArgs(); 

Ou, changer Teh démarrage uri App.xaml (retirer) et au lieu de créer votre propre démarrage comme

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

etc