質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

2147閲覧

WebBrowser とウインドウを重ねるように表示していますが、

byori

総合スコア71

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

0クリップ

投稿2020/09/30 03:58

github にあったサンプルを使用して WebBrowser 上にウインドウを重ねて線やコントロールを重ねて表示させています。

サンプル:[https://github.com/michaelsutton/hwnd-adorner]

WebBrowserでは画像を主に表示させていて BrowserPresenter() で 初回作成時に

C#

1 public BrowserPresenter() 2 { 3 var browser = new WebBrowser(); 4 browser.Source = new Uri(adress); 5 6 HwndHost = browser;

で、html を表示しています。
このサンプルだと、後で adress を変えて次を表示させようとしても一度ウインドウを終了し再度表示させないと次の画像(サイト)に切り替わりません。Presenterのコンストラクタでブラウザに表示させていてHwndHost = browser;でサンプルの本体にコピーしているようです。
ここがちょっと不便なので、ウインドウを表示させたまま画像(サイト)を切り替えたいのですが、そのように改変できますでしょうか?

多分ですが、下記のxaml で初期化している部分を任意のタイミングで 上記のBrowserPresenter.csのBrowserPresenter() をビハインドコード(DisplayForm.xaml.cs)から呼び出すように変更できないでしょうか?

C#

1DisplayForm.xaml 2 <adorner:HwndAdornerManager> 3 <Grid> 4 <ReportLite3.Adorner:BrowserPresenter> 5 <ReportLite3.Adorner:BrowserPresenter.Adornment> 6 <ReportLite3.Adorner:BrowserAdornment /> 7 </ReportLite3.Adorner:BrowserPresenter.Adornment> 8 </ReportLite3.Adorner:BrowserPresenter> 9 </Grid> 10 </adorner:HwndAdornerManager>

サンプルと実コードの抜粋

C#,

1DisplayForm.xaml 2 <Grid> 3 <Grid.ColumnDefinitions> 4 <ColumnDefinition Width="705"/> 5 </Grid.ColumnDefinitions> 6 <Grid.RowDefinitions> 7 <RowDefinition Height="35"/> 8 <RowDefinition Height="780" /> 9 </Grid.RowDefinitions> 10 11 <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="0" Background="White"> 12 <ScrollViewer Height="770"> 13 <adorner:HwndAdornerManager> 14 <Grid> 15 <ReportLite3.Adorner:BrowserPresenter> 16 <ReportLite3.Adorner:BrowserPresenter.Adornment> 17 <ReportLite3.Adorner:BrowserAdornment /> 18 </ReportLite3.Adorner:BrowserPresenter.Adornment> 19 </ReportLite3.Adorner:BrowserPresenter> 20 </Grid> 21 </adorner:HwndAdornerManager> 22 </ScrollViewer> 23 </StackPanel> 24 </Grid> 25 26 27BrowserAdornment.xaml 28<UserControl x:Class="ReportLite3.Adorner.BrowserAdornment" 29 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 30 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 31 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 32 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 33 mc:Ignorable="d" 34 d:DesignHeight="800" d:DesignWidth="710" 35 HorizontalAlignment="Center" VerticalAlignment="Center" Loaded="UserControl_Loaded"> 36 37 <ScrollViewer Height="770" Width="700"> 38 <Canvas Name="canvas1" Height="770" Width="700"> 39 40 </Canvas> 41 </ScrollViewer> 42</UserControl> 43 44 45BrowserPresenter.cs 46 public class BrowserPresenter : HwndHostPresenter 47 { 48 public static string adress = @"\LANDISK-610775\ReportConfig\RequestForm\InitImage.html"; 49 public static BrowserPresenter _presenterInstance; 50 51 public BrowserPresenter() 52 { 53 var browser = new WebBrowser(); 54 browser.Source = new Uri(adress); 55 56 HwndHost = browser; 57 RegisterToAppShutdown(); 58 } 59 60 private void RegisterToAppShutdown() 61 { 62 Application.Current.Dispatcher.ShutdownStarted += OnShutdownStarted; 63 } 64 65 private void OnShutdownStarted(object sender, EventArgs e) 66 { 67 Dispose(); 68 } 69 70 protected override void Dispose(bool disposing) 71 { 72 if (disposing) 73 { 74 var host = HwndHost; 75 if(host != null) 76 host.Dispose(); 77 } 78 } 79 } 80 81 82HwndHostPresenter.cs 83 public class HwndHostPresenter : FrameworkElement, IDisposable 84 { 85 #region Fields 86 87 private UIElement m_child; 88 private HwndHost m_hwndHost; 89 private bool m_mouseIsOverHwnd; 90 91 private readonly HwndAdorner m_hwndAdorner; 92 private UIElement m_adornment; 93 94 static HwndHostPresenter() 95 { 96 EventManager.RegisterClassHandler(typeof(HwndHostPresenter), MouseEnterEvent, new RoutedEventHandler(OnMouseEnterOrLeave)); 97 EventManager.RegisterClassHandler(typeof(HwndHostPresenter), MouseLeaveEvent, new RoutedEventHandler(OnMouseEnterOrLeave)); 98 99 EventManager.RegisterClassHandler(typeof(HwndHostPresenter), HwndExtensions.HwndMouseEnterEvent, new RoutedEventHandler(OnHwndMouseEnterOrLeave)); 100 EventManager.RegisterClassHandler(typeof(HwndHostPresenter), HwndExtensions.HwndMouseLeaveEvent, new RoutedEventHandler(OnHwndMouseEnterOrLeave)); 101 } 102 103 public HwndHostPresenter() 104 { 105 m_hwndAdorner = new HwndAdorner(this); 106 AddLogicalChild(m_hwndAdorner.Root); 107 } 108 109 : 110 111 public HwndHost HwndHost 112 { 113 get { return m_hwndHost; } 114 set 115 { 116 if (m_hwndHost == value) return; 117 118 RemoveLogicalChild(m_hwndHost); 119 120 m_hwndHost = value; 121 122 AddLogicalChild(value); 123 if (Hosting) 124 { 125 Child = value; 126 } 127 } 128 }
TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

BrowserPresenterHwndHostWebBrowserが入っているわけですから、適当に取得してNavigateなりすればいいだけでは?

xml

1<demo:BrowserPresenter x:Name="browserPresenter">

cs

1var browser = (WebBrowser)browserPresenter.HwndHost; 2browser.Navigate(new Uri("https://teratail.com/"));

投稿2020/09/30 08:29

編集2023/07/23 07:18
TN8001

総合スコア9326

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

byori

2020/10/01 02:10

お世話になります。 > 適当に取得してNavigateなりすればいいだけでは? ですが、下記のように xaml で作成されているので、どのようにして var browser = (WebBrowser)browserPresenter.HwndHost; を取得するのですか? <adorner:HwndAdornerManager> <Grid> <ReportLite3.Adorner:BrowserPresenter> <ReportLite3.Adorner:BrowserPresenter.Adornment> <ReportLite3.Adorner:BrowserAdornment /> </ReportLite3.Adorner:BrowserPresenter.Adornment> </ReportLite3.Adorner:BrowserPresenter> </Grid> </adorner:HwndAdornerManager>
TN8001

2020/10/01 02:25

例えば <ReportLite3.Adorner:BrowserPresenter x:Name="bp"> と xamlで名前を付けますよね。 で DisplayForm.xaml.csから var browser = (WebBrowser)bp.HwndHost; とすれば取れませんか?
byori

2020/10/01 03:09

お世話になります。 > どのようにしてvar browser = (WebBrowser)browserPresenter.HwndHost; を取得するのですか? Xaml で定義しているものは何であれ TextBoxなどのコントロールと同じように取得できるのですね。 考えてもいませんでした。 目的通りの動作するようになりました。ありがとうございました。
guest

0

WebBrowerコントロールのNavigateメソッドを使えばどうでしょうか

WebBrowser.Navigate メソッド (System.Windows.Forms) | Microsoft Docs


ああ、WPFだったのね。

WebBrowser.Navigate メソッド (System.Windows.Controls) | Microsoft Docs

投稿2020/09/30 04:16

編集2020/09/30 04:27
y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

byori

2020/09/30 04:24

サンプルと実コードの抜粋の DisplayForm.xaml で初期化している BrowserPresenter にビハインドコード側から現状アクセスできません。 操作できるようコードをへんこうできれば!と思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問