C# load local relative path HTML page

1. Add a WebBrowser control to the page, the default name is: webBrowser1

1.1 First create a WinForm program, and then drag in a panel control, as shown in the figure:
write picture description here
1.2 After dragging in the panel control, find the WebBrowser control and double-click, the WebBrowser control will be automatically filled on the panel control, like the following:
write picture description here

1.3 Need to write the sizeChanged event of Form

private void mainForm_SizeChanged(object sender, EventArgs e)
{
      panel1.Width = this.Width;
      panel1.Height = this.Height;
      webBrowserForm.Dock = DockStyle.Fill;
}

In this way, when the size of the Form form changes, the size of the panel will also change, and the webBrowser control will always fill the panel control.

1.4 In order to make the content in the Form form fully displayed, and to add a scroll bar to it, you can directly add the following statement in the construction method of the Form:

this.AutoScroll = true;

Such a simple WebBrowser is ready.

2. Create a new HTML page with the name TextHTMLPage.htm and place it in the debug directory of the program.

Note: The bin directory and the obj directory are not displayed in the project by default. You have to click to display all files before it will be displayed (if you click and there is no Debug directory, it may be that your project has not been generated. Generate the project first, and there will be this directory)

write picture description here
write picture description here

3, TextHTMLPage.htm page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title></title>
    </head>
    <body>
    哈哈哈哈
    </body>
</html>

4. Load the html page in the code

Note: Look at the name of the HTML page you created. When loading, it must be the same as the name of your newly created HTML (including the file name and extension) to be loaded. For example, my HTML file name is TextHTMLPage.htm. If I am loading When it is written as TextHTMLPage.html, it cannot be loaded (the difference between the two is that the extension has an additional l)

private void Form1_Load(object sender, EventArgs e)
        {
            try
            {  
                Debug.WriteLine("Chunna.zheng 項目路徑:" + AppDomain.CurrentDomain.BaseDirectory);

                //这个文件于可执行文件放在同一目录
                webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "TextHTMLPage.htm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }  
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860061&siteId=291194637