(C#) WebBrowser: 웹 브라우저 생성

이 코드는 WebBrowser 컨트롤을 사용하여 웹 브라우저를 만듭니다.

1. 프로젝트를 생성합니다.

  • Windows Forms 앱(.NET 프레임워크)

2. 양식을 디자인합니다.

  • 메뉴 및 도구 모음 – ToolStrip
  • WebBrowser – Internet Explorer를 지원하는 컨트롤입니다.

아래와 같이 ToolsStrip에 TextBox 1개와 Button 5개를 배치합니다.

버튼에 사용될 이미지는 아래 링크에서 다운받아 사용할 수 있습니다.

(강의용) 이미지(image), 여러 아이콘(icon) (coding-abc.kr)


홈버튼은 “네이버”로 설정되어 있습니다.

각 콘트롤의 이름은 소스 코드를 참조하여 생성하거나 오류가 발생하지 않는 방식으로 소스를 수정합니다.

using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace Webbrower_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.CanGoBackChanged += new System.EventHandler(this.webBrowser1_CanGoBackChanged);
            this.webBrowser1.CanGoForwardChanged += new System.EventHandler(this.webBrowser1_CanGoForwardChanged);

        }

        private void txtAddr_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Navigate(txtAddr.Text);
            }
        }

        private void Navigate(String address)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://") && !address.StartsWith("https://")) 
            { 
                address = "http://" + address; 
            }
            try { 
                webBrowser1.Navigate(new Uri(address)); 
            }
            catch (System.UriFormatException) { 
                return; 
            }
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            txtAddr.Text = webBrowser1.Url.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtAddr.Text = "naver.com";
            Navigate(txtAddr.Text);
        }

        private void btnGoBack_Click(object sender, EventArgs e)
        {
            // Navigates webBrowser1 to the previous page in the history.
            webBrowser1.GoBack();
        }
        
        // Disables the Back button at the beginning of the navigation history.
        private void webBrowser1_CanGoBackChanged(object sender, EventArgs e)
        {
            btnGoBack.Enabled = webBrowser1.CanGoBack;
        }

        private void btnGoForward_Click(object sender, EventArgs e)
        {
            // Navigates webBrowser1 to the next page in history.
            webBrowser1.GoForward();
        }

        // Disables the Forward button at the end of navigation history.
        private void webBrowser1_CanGoForwardChanged(object sender, EventArgs e)
        {
            btnGoForward.Enabled = webBrowser1.CanGoForward;
        }

        // Reloads the current page.
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            // Skip refresh if about:blank is loaded to avoid removing
            // content specified by the DocumentText property.
            if (!webBrowser1.Url.Equals("about:blank"))
            {
                webBrowser1.Refresh();
            }
        }

        // Navigates webBrowser1 to the home page of the current user.
        private void btnGoHome_Click(object sender, EventArgs e)
        {
            //webBrowser1.GoHome();
            txtAddr.Text = "naver.com";
            Navigate(txtAddr.Text);
        }

        // Halts the current navigation and any sounds or animations on the page.
        private void btnStop_Click(object sender, EventArgs e)
        {
            webBrowser1.Stop();
        }
    }
}

실행 결과 화면입니다.