Share via

c# wpf web browser change background

Zaug 306 Reputation points
Feb 16, 2022, 4:10 PM

i work a project in c# wpf and using webbrowser. but my web browser background color is white. i want to change background color to #F222. how to make this
my web browser is bottom of the window
175045-image.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
838 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,651 Reputation points Microsoft External Staff
    Feb 24, 2022, 3:03 AM

    It is recommended that you directly set the background color of the html to #F222. It is currently difficult to set the background color of the WebBrowser(wpf) to a custom color.

    Even using Winform's WebBrowser can only set the html document page to black instead of #F222.

    An example of using Winform's Webbrowser in wpf is as follows:
    1.Right-click the References under the project and select Add Reference...
    2.Search for adding System.Windows.Forms and WindowsFormsIntegration references and click OK.
    3.Add namespace in Window.

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
    

    complete code:

        <Window x:Class="WpfApp2.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp2"   
             xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
            mc:Ignorable="d"   
            Title="MainWindow" Height="450" Width="1200">  
        <StackPanel x:Name="grid" Background="#f222" Orientation="Horizontal"  >  
            <WindowsFormsHost x:Name="wfhSample" Width="400" Height="400" Canvas.Right="10">  
                <WindowsFormsHost.Child>  
                    <wf:WebBrowser x:Name="wb" DocumentCompleted="wb_DocumentCompleted"/>  
                </WindowsFormsHost.Child>  
            </WindowsFormsHost>  
        </StackPanel>  
    </Window>  
    

    MainWindow.xaml.cs:

    using System.Windows;  
    using System.Windows.Forms;  
    namespace WpfApp2  
    {  
        public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
    
                wb.Navigate("http://google.com");  
            }  
            private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
            {  
                HtmlDocument document = (HtmlDocument)wb.Document;  
                document.Body.Style = "background-color:black";  
            }  
        }  
    }  
    

    The result:
    177365-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.