첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ TabForm 클래스 : 웹 브라우저 만들기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


EmptyBarDockControl.cs

 

 

using System.Drawing;

using System.Windows.Forms;

 

using DevExpress.XtraBars;

using DevExpress.XtraBars.Controls;

using DevExpress.XtraBars.ViewInfo;

 

namespace TestProject

{

    /// <summary>

    /// 비어있는 바 도킹 컨트롤

    /// </summary>

    public class EmptyBarDockControl : BarDockControl

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Class

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 비어있는 도킹 컨트롤 스킨 뷰 정보 - EmptyDockControlSkinViewInfo

 

        /// <summary>

        /// 비어있는 도킹 컨트롤 스킨 뷰 정보

        /// </summary>

        private class EmptyDockControlSkinViewInfo : DockControlSkinViewInfo

        {

            //////////////////////////////////////////////////////////////////////////////////////////////////// Property

            ////////////////////////////////////////////////////////////////////////////////////////// Public

 

            #region 전경 그리기 여부 - IsDrawForeground

 

            /// <summary>

            /// 전경 그리기 여부

            /// </summary>

            public override bool IsDrawForeground

            {

                get

                {

                    return false;

                }

            }

 

            #endregion

 

            //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

            ////////////////////////////////////////////////////////////////////////////////////////// Public

 

            #region 생성자 - EmptyDockControlSkinViewInfo(manager, parameters, customControl)

 

            /// <summary>

            /// 생성자

            /// </summary>

            /// <param name="manager">바 관리자</param>

            /// <param name="parameters">매개 변수 목록</param>

            /// <param name="customControl">커스텀 컨트롤</param>

            public EmptyDockControlSkinViewInfo(BarManager manager, BarDrawParameters parameters, CustomControl customControl) : base

            (

                manager,

                parameters,

                customControl

            )

            {

            }

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 배경색 - BackColor

 

        /// <summary>

        /// 배경색

        /// </summary>

        public override Color BackColor

        {

            get

            {

                return Color.Transparent;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - EmptyBarDockControl()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public EmptyBarDockControl()

        {

            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 뷰 정보 생성하기 - CreateViewInfo()

 

        /// <summary>

        /// 뷰 정보 생성하기

        /// </summary>

        /// <returns>뷰 정보</returns>

        protected override CustomViewInfo CreateViewInfo()

        {

            return new EmptyDockControlSkinViewInfo(Manager, Manager.GetController().PaintStyle.DrawParameters, this);

        }

 

        #endregion

    }

}

 

 

NavigationControl.cs

 

 

using System.Windows.Forms;

 

using DevExpress.XtraBars;

using DevExpress.XtraEditors;

using DevExpress.XtraEditors.Repository;

 

namespace TestProject

{

    /// <summary>

    /// 네비게이션 컨트롤

    /// </summary>

    public partial class NavigationControl : XtraUserControl

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 조회 항목 왼쪽 패딩

        /// </summary>

        private static readonly int _searchItemLeftPadding = 8;

 

        /// <summary>

        /// 조회 항목 오른쪽 패딩

        /// </summary>

        private static readonly int _searchItemRightPadding = 5;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 관리자 - Manager

 

        /// <summary>

        /// 관리자

        /// </summary>

        public BarManager Manager

        {

            get

            {

                return this.barManager;

            }

        }

 

        #endregion

        #region 뒤로 가기 버튼 - BackButton

 

        /// <summary>

        /// 뒤로 가기 버튼

        /// </summary>

        public BarButtonItem BackButton

        {

            get

            {

                return this.backButton;

            }

        }

 

        #endregion

        #region 앞으로 가기 버튼 - ForwardButton

 

        /// <summary>

        /// 앞으로 가기 버튼

        /// </summary>

        public BarButtonItem ForwardButton

        {

            get

            {

                return this.foreardButton;

            }

        }

 

        #endregion

        #region 리프레쉬 버튼 - RefreshButton

 

        /// <summary>

        /// 리프레쉬 버튼

        /// </summary>

        public BarButtonItem RefreshButton

        {

            get

            {

                return this.refreshButton;

            }

        }

 

        #endregion

        #region 조회 항목 - SearchItem

 

        /// <summary>

        /// 조회 항목

        /// </summary>

        public BarEditItem SearchItem

        {

            get

            {

                return this.searchControl;

            }

        }

 

        #endregion

        #region 조회 패널 - SearchPanel

 

        /// <summary>

        /// 조회 패널

        /// </summary>

        public RepositoryItemSearchControl SearchPanel

        {

            get

            {

                return this.repositoryItemSearchControl;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - NavigationControl()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public NavigationControl()

        {

            InitializeComponent();

 

            Dock = DockStyle.Top;

 

            SearchItem.EditorPadding = new Padding(_searchItemLeftPadding, 0, _searchItemRightPadding, 0);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 경계 설정하기 (코어) - SetBoundsCore(x, y, width, height, specified)

 

        /// <summary>

        /// 경계 설정하기 (코어)

        /// </summary>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        /// <param name="width">너비</param>

        /// <param name="height">높이</param>

        /// <param name="specified">경계 지정</param>

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)

        {

            height = Padding.Vertical + this.barDockControlTop.Height;

 

            base.SetBoundsCore(x, y, width, height, specified);

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 홈 버튼 클릭시 처리하기 - homeButton_ItemClick(sender, e)

 

        /// <summary>

        /// 홈 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void homeButton_ItemClick(object sender, ItemClickEventArgs e)

        {

            MainForm ownerForm = FindForm() as MainForm;

 

            if(ownerForm != null)

            {

                ownerForm.GoToHomePage();

            }

        }

 

        #endregion

    }

}

 

 

TabFormWebBrowser.cs

 

 

using System;

using System.Drawing;

using System.IO;

using System.Net;

using System.Windows.Forms;

 

using DevExpress.XtraBars;

 

namespace TestProject

{

    /// <summary>

    /// 탭 폼 웹 브라우저

    /// </summary>

    public class TabFormWebBrowser : WebBrowser

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 탭 폼 페이지

        /// </summary>

        private TabFormPage page;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 탭 폼 페이지 - Page

 

        /// <summary>

        /// 탭 폼 페이지

        /// </summary>

        public TabFormPage Page

        {

            get

            {

                return this.page;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - TabFormWebBrowser(page)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="page">페이지</param>

        public TabFormWebBrowser(TabFormPage page) : base()

        {

            this.page = page;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 문서 완료시 처리하기 - OnDocumentCompleted(e)

 

        /// <summary>

        /// 문서 완료시 처리하기

        /// </summary>

        /// <param name="e">이벤트 인자</param>

        protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)

        {

            base.OnDocumentCompleted(e);

 

            if(this.page == null || e.Url == null || string.IsNullOrEmpty(e.Url.Host) || Url == null || !object.Equals(Url.Host, e.Url.Host))

            {

                return;

            }

 

            try

            {

                string iconURL = "http://" + e.Url.Host + "/favicon.ico";

 

                WebRequest  request  = HttpWebRequest.Create(iconURL);

                WebResponse response = request.GetResponse();

 

                Stream stream = response.GetResponseStream();

 

                Page.Image = Image.FromStream(stream);

            }

            catch

            {

            }

        }

 

        #endregion

        #region 문서 제목 변경시 처리하기 - OnDocumentTitleChanged(e)

 

        /// <summary>

        /// 문서 제목 변경시 처리하기

        /// </summary>

        /// <param name="e"></param>

        protected override void OnDocumentTitleChanged(EventArgs e)

        {

            base.OnDocumentTitleChanged(e);

 

            if(this.page == null || string.IsNullOrEmpty(DocumentTitle))

            {

                return;

            }

 

            this.page.Text = DocumentTitle;

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using Microsoft.Win32;

using System;

using System.ComponentModel;

using System.Diagnostics;

using System.Windows.Forms;

 

using DevExpress.LookAndFeel;

using DevExpress.Skins;

using DevExpress.Utils;

using DevExpress.XtraBars;

using DevExpress.XtraEditors;

using DevExpress.XtraEditors.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : TabForm

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 브라우저 버전 체크 여부

        /// </summary>

        private static bool _browserVersionChecked = false;

 

        /// <summary>

        /// 오픈 폼 수

        /// </summary>

        private static int _openFormCount = 1;

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 스킨 처리 여부

        /// </summary>

        private bool skinProcessing = false;

 

        /// <summary>

        /// 웹 브라우저 생성 여부

        /// </summary>

        private bool createWebBrowser = true;

 

        /// <summary>

        /// 페이지 제목 숨김 여부

        /// </summary>

        private bool hidePageCaption = false;

 

        /// <summary>

        /// 빈 문자열

        /// </summary>

        private string blankString = "about:blank";

 

        /// <summary>

        /// 현재 주소

        /// </summary>

        private string currentAddress = string.Empty;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 웹 브라우저 - WebBrowser

 

        /// <summary>

        /// 웹 브라우저

        /// </summary>

        private WebBrowser WebBrowser

        {

            get

            {

                if(TabFormControl.SelectedPage == null)

                {

                    return null;

                }

 

                foreach(Control control in TabFormControl.SelectedPage.ContentContainer.Controls)

                {

                    if(control is WebBrowser)

                    {

                        return control as WebBrowser;

                    }

                }

 

                return null;

            }

        }

 

        #endregion

        #region 네비게이션 패널 - NavigationPanel

 

        /// <summary>

        /// 네비게이션 패널

        /// </summary>

        private NavigationControl NavigationPanel

        {

            get

            {

                if(TabFormControl.SelectedPage == null)

                {

                    return null;

                }

 

                foreach(Control control in TabFormControl.SelectedPage.ContentContainer.Controls)

                {

                    if(control is NavigationControl)

                    {

                        return control as NavigationControl;

                    }

                }

 

                return null;

            }

        }

 

        #endregion

        #region 주소 - Address

 

        /// <summary>

        /// 주소

        /// </summary>

        private string Address

        {

            get

            {

                if(this.barManager.ActiveEditor != null && this.barManager.ActiveEditor.EditValue != null)

                {

                    return this.barManager.ActiveEditor.EditValue.ToString();

                }

 

                return null;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm() : this(true)

        {

        }

 

        #endregion

        #region 생성자 - MainForm(createWebBrowser)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="createWebBrowser">웹 브라우저 생성 여부</param>

        public MainForm(bool createWebBrowser)

        {

            InitializeComponent();

 

            Icon = ResourceImageHelper.CreateIconFromResourcesEx("TestProject.ICON.main.ico", typeof(MainForm).Assembly);

            Text = "웹 브라우저 사용하기";

 

            this.createWebBrowser = createWebBrowser;

 

            if(this.createWebBrowser)

            {

                CreateWebBrowser(TabFormControl.SelectedPage);

 

                AddEventHandler();

            }

            else

            {

                TabFormControl.Pages.Clear();

 

                TabFormControl.SelectedPage = null;

            }

 

            this.barManager.ForceLinkCreate();

 

            this.barManager.ForceInitialize();

 

            Focus();

 

            BarAndDockingController controller = this.barManager.GetController();

 

            Load                                  += Form_Load;

            TabFormControl.SelectedPageChanging   += TabFormControl_SelectedPageChanging;

            TabFormControl.SelectedPageChanged    += TabFormControl_SelectedPageChanged;

            TabFormControl.PageCreated            += TabFormControl_PageCreated;

            controller.Changed                    += BarAndDockingController_Changed;

            this.tabFormControl.OuterFormCreating += tabFormControl_OuterFormCreating;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 홈 페이지 이동하기 - GoToHomePage()

 

        /// <summary>

        /// 홈 페이지 이동하기

        /// </summary>

        public void GoToHomePage()

        {

            GoToItem("https://icodebroker.tistory.com/category");

        }

 

        #endregion

        #region 탐색하기 - Navigate(address)

 

        /// <summary>

        /// 탐색하기

        /// </summary>

        /// <param name="address">주소</param>

        public void Navigate(string address)

        {

            this.hidePageCaption = true;

 

            try

            {

                this.tabFormControl.AddNewPage();

            }

            finally

            {

                this.hidePageCaption = false;

            }

 

            GoToItem(address);

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            if(this.createWebBrowser)

            {

                BeginInvoke(new MethodInvoker(GoToHomePage));

            }

        }

 

        #endregion

 

        #region 탭 폼 컨트롤 선택 페이지 변경전 처리하기 - TabFormControl_SelectedPageChanging(sender, e)

 

        /// <summary>

        /// 탭 폼 컨트롤 선택 페이지 변경전 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void TabFormControl_SelectedPageChanging(object sender, TabFormSelectedPageChangingEventArgs e)

        {

            RemoveEventHandler();

        }

 

        #endregion

        #region 탭 폼 컨트롤 선택 페이지 변경후 처리하기 - TabFormControl_SelectedPageChanged(sender, e)

 

        /// <summary>

        /// 탭 폼 컨트롤 선택 페이지 변경후 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void TabFormControl_SelectedPageChanged(object sender, TabFormSelectedPageChangedEventArgs e)

        {

            AddEventHandler();

 

            if(NavigationPanel == null)

            {

                return;

            }

 

            if(WebBrowser == null || WebBrowser.Url == null)

            {

                NavigationPanel.SearchItem.EditValue = string.Empty;

            }

            else

            {

                NavigationPanel.SearchItem.EditValue = WebBrowser.Url.AbsoluteUri;

            }

 

            CheckButton();

        }

 

        #endregion

        #region 탭 폼 컨트롤 페이지 생성시 처리하기 - TabFormControl_PageCreated(sender, e)

 

        /// <summary>

        /// 탭 폼 컨트롤 페이지 생성시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void TabFormControl_PageCreated(object sender, PageCreatedEventArgs e)

        {

            if(this.hidePageCaption)

            {

                e.Page.Text = string.Empty;

            }

 

            CreateWebBrowser(e.Page);

        }

 

        #endregion

        #region 바/도킹 컨트롤러 변경시 처리하기 - BarAndDockingController_Changed(sender, e)

 

        /// <summary>

        /// 바/도킹 컨트롤러 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void BarAndDockingController_Changed(object sender, EventArgs e)

        {

            if(this.skinProcessing)

            {

                return;

            }

 

            string paintStyleName = this.barManager.GetController().PaintStyleName;

 

            if("DefaultSkin".IndexOf(paintStyleName) >= 0)

            {

                SkinManager.EnableFormSkins();

            }

            else

            {

                SkinManager.DisableFormSkins();

            }

 

            this.skinProcessing = true;

 

            LookAndFeelHelper.ForceDefaultLookAndFeelChanged();

 

            this.skinProcessing = false;

        }

 

        #endregion

        #region 탭 폼 컨트롤 외부 폼 생성시 처리하기 - tabFormControl_OuterFormCreating(sender, e)

 

        /// <summary>

        /// 탭 폼 컨트롤 외부 폼 생성시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void tabFormControl_OuterFormCreating(object sender, OuterFormCreatingEventArgs e)

        {

            e.Form = new MainForm(false);

 

            _openFormCount++;

        }

 

        #endregion

 

        #region 웹 브라우저 탐색시 처리하기 - WebBrowser_Navigated(sender, e)

 

        /// <summary>

        /// 웹 브라우저 탐색시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void WebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)

        {

            if(!object.Equals(sender, WebBrowser))

            {

                return;

            }

 

            string documentURI = WebBrowser.Url.AbsoluteUri;

            string eventURI    = e.Url.AbsoluteUri;

 

            if(eventURI == this.blankString)

            {

                return;

            }

 

            if(documentURI != eventURI)

            {

                return;

            }

 

            if(this.barManager.ActiveEditor != null)

            {

                this.barManager.ActiveEditItemLink.CloseEditor();

            }

 

            if(ValidateAddress(eventURI))

            {

                NavigationPanel.SearchItem.EditValue = eventURI;

 

                this.currentAddress = eventURI;

 

                AddNewItem(eventURI);

            }

        }

 

        #endregion

        #region 웹 브라우저 뒤로 가기 가능 여부 변경시 처리하기 - WebBrowser_CanGoBackChanged(sender, e)

 

        /// <summary>

        /// 웹 브라우저 뒤로 가기 가능 여부 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void WebBrowser_CanGoBackChanged(object sender, EventArgs e)

        {

            NavigationPanel.BackButton.Enabled = WebBrowser.CanGoBack;

        }

 

        #endregion

        #region 웹 브라우저 앞으로 가기 가능 여부 변경시 처리하기 - WebBrowser_CanGoForwardChanged(sender, e)

 

        /// <summary>

        /// 웹 브라우저 앞으로 가기 가능 여부 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void WebBrowser_CanGoForwardChanged(object sender, EventArgs e)

        {

            NavigationPanel.ForwardButton.Enabled = WebBrowser.CanGoForward;

        }

 

        #endregion

 

        #region 탐색 패널 팝업 닫는 경우 처리하기 - SearchPanel_CloseUp(sender, e)

 

        /// <summary>

        /// 탐색 패널 팝업 닫는 경우 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void SearchPanel_CloseUp(object sender, CloseUpEventArgs e)

        {

            GoToItem(Address);

        }

 

        #endregion

        #region 탐색 패널 키 DOWN 처리하기 - SearchPanel_KeyDown(sender, e)

 

        /// <summary>

        /// 탐색 패널 키 DOWN 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void SearchPanel_KeyDown(object sender, KeyEventArgs e)

        {

            ComboBoxEdit comboBoxEdit = sender as ComboBoxEdit;

 

            if(e.KeyData == Keys.Escape)

            {

                e.Handled = true;

 

                comboBoxEdit.SelectAll();

            }

 

            if(e.KeyData == Keys.Enter && !comboBoxEdit.IsPopupOpen)

            {

                NavigationPanel.Manager.ActiveEditItemLink.PostEditor();

 

                comboBoxEdit.SelectAll();

 

                e.Handled = true;

 

                GoToItem(NavigationPanel.SearchItem.EditValue.ToString());

            }

        }

 

        #endregion

        #region 탐색 패널 검증시 처리하기 - SearchPanel_Validating(sender, e)

 

        /// <summary>

        /// 탐색 패널 검증시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void SearchPanel_Validating(object sender, CancelEventArgs e)

        {

            GoToItem(Address);

        }

 

        #endregion

 

        #region 뒤로 가기 버튼 항목 클릭시 처리하기 - BackButton_ItemClick(sender, e)

 

        /// <summary>

        /// 뒤로 가기 버튼 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void BackButton_ItemClick(object sender, ItemClickEventArgs e)

        {

            try

            {

                if(WebBrowser != null)

                {

                    WebBrowser.GoBack();

                }

            }

            catch

            {

            }

        }

 

        #endregion

        #region 앞으로 가기 버튼 항목 클릭시 처리하기 - ForwardButton_ItemClick(sender, e)

 

        /// <summary>

        /// 앞으로 가기 버튼 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void ForwardButton_ItemClick(object sender, ItemClickEventArgs e)

        {

            try

            {

                if(WebBrowser != null)

                {

                    WebBrowser.GoForward();

                }

            }

            catch

            {

            }

        }

 

        #endregion

        #region 리프레쉬 버튼 항목 클릭시 처리하기 - RefreshButton_ItemClick(sender, e)

 

        /// <summary>

        /// 리프레쉬 버튼 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void RefreshButton_ItemClick(object sender, ItemClickEventArgs e)

        {

            if(WebBrowser == null || WebBrowser.Url == null)

            {

                return;

            }

 

            WebBrowser.Navigate(WebBrowser.Url.AbsoluteUri);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 이벤트 핸들러 추가하기 - AddEventHandler()

 

        /// <summary>

        /// 이벤트 핸들러 추가하기

        /// </summary>

        private void AddEventHandler()

        {

            if(WebBrowser != null)

            {

                WebBrowser.Navigated           += WebBrowser_Navigated;

                WebBrowser.CanGoBackChanged    += WebBrowser_CanGoBackChanged;

                WebBrowser.CanGoForwardChanged += WebBrowser_CanGoForwardChanged;

 

                NavigationPanel.SearchPanel.CloseUp     += SearchPanel_CloseUp;

                NavigationPanel.SearchPanel.KeyDown     += SearchPanel_KeyDown;

                NavigationPanel.SearchPanel.Validating  += SearchPanel_Validating;

                NavigationPanel.BackButton.ItemClick    += BackButton_ItemClick;

                NavigationPanel.ForwardButton.ItemClick += ForwardButton_ItemClick;

                NavigationPanel.RefreshButton.ItemClick += RefreshButton_ItemClick;

            }

        }

 

        #endregion

        #region 이벤트 핸들러 제거하기 - RemoveEventHandler()

 

        /// <summary>

        /// 이벤트 핸들러 제거하기

        /// </summary>

        private void RemoveEventHandler()

        {

            if(WebBrowser != null)

            {

                WebBrowser.Navigated           -= WebBrowser_Navigated;

                WebBrowser.CanGoBackChanged    -= WebBrowser_CanGoBackChanged;

                WebBrowser.CanGoForwardChanged -= WebBrowser_CanGoForwardChanged;

 

                NavigationPanel.SearchPanel.CloseUp     -= SearchPanel_CloseUp;

                NavigationPanel.SearchPanel.KeyDown     -= SearchPanel_KeyDown;

                NavigationPanel.SearchPanel.Validating  -= SearchPanel_Validating;

                NavigationPanel.BackButton.ItemClick    -= BackButton_ItemClick;

                NavigationPanel.ForwardButton.ItemClick -= ForwardButton_ItemClick;

                NavigationPanel.RefreshButton.ItemClick -= RefreshButton_ItemClick;

            }

        }

 

        #endregion

        #region 버튼 체크하기 - CheckButton()

 

        /// <summary>

        /// 버튼 체크하기

        /// </summary>

        private void CheckButton()

        {

            NavigationPanel.ForwardButton.Enabled = WebBrowser.CanGoForward;

            NavigationPanel.BackButton.Enabled    = WebBrowser.CanGoBack;

        }

 

        #endregion

        #region 브라우저 에뮬레이션 레지스트리 키 구하기 - GetBrowserEmulationRegistryKey()

 

        /// <summary>

        /// 브라우저 에뮬레이션 레지스트리 키 구하기

        /// </summary>

        /// <returns>브라우저 에뮬레이션 레지스트리 키</returns>

        private RegistryKey GetBrowserEmulationRegistryKey()

        {

            try

            {

                RegistryKey registryKey = null;

 

                string name = "SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION";

 

                registryKey = Registry.CurrentUser.OpenSubKey(name, true);

 

                if(registryKey != null)

                {

                    return registryKey;

                }

 

                return Registry.CurrentUser.CreateSubKey(name);

            }

            catch

            {

            }

 

            return null;

        }

 

        #endregion

        #region 최적 브라우저 버전 구하기 - GetOptimalBrowserVersion(browser)

 

        /// <summary>

        /// 최적 브라우저 버전 구하기

        /// </summary>

        /// <param name="browser">웹 브라우저</param>

        /// <returns>최적 브라우저 버전</returns>

        private int GetOptimalBrowserVersion(WebBrowser browser)

        {

            int majorVersion = browser.Version.Major;

 

            if(majorVersion >= 11)

            {

                return 11001;

            }

 

            if(majorVersion == 10)

            {

                return 10001;

            }

 

            if(majorVersion == 9)

            {

                return 9999;

            }

 

            if(majorVersion == 8)

            {

                return 8888;

            }

 

            return 7000;

        }

 

        #endregion

        #region 웹 브라우저 버전 업데이트하기 - UpdateWebBrowserVersion(browser)

 

        /// <summary>

        /// 웹 브라우저 버전 업데이트하기

        /// </summary>

        /// <param name="browser">브라우저</param>

        private void UpdateWebBrowserVersion(WebBrowser browser)

        {

            if(_browserVersionChecked)

            {

                return;

            }

 

            _browserVersionChecked = true;

 

            RegistryKey registryKey = GetBrowserEmulationRegistryKey();

 

            if(registryKey == null)

            {

                return;

            }

 

            int optimalVersion = GetOptimalBrowserVersion(browser);

 

            string processName = Process.GetCurrentProcess().ProcessName + ".exe";

 

            try

            {

                object value = registryKey.GetValue(processName);

 

                if(!(value is int) || (int)value != optimalVersion)

                {

                    registryKey.SetValue(processName, unchecked(optimalVersion), RegistryValueKind.DWord);

                }

            }

            catch

            {

            }

            finally

            {

                registryKey.Close();

            }

        }

 

        #endregion

        #region 웹 브라우저 생성하기 - CreateWebBrowser(page)

 

        /// <summary>

        /// 웹 브라우저 생성하기

        /// </summary>

        /// <param name="page">탭 폼 페이지</param>

        private void CreateWebBrowser(TabFormPage page)

        {

            TabFormWebBrowser browser = new TabFormWebBrowser(page);

 

            UpdateWebBrowserVersion(browser);

 

            page.ContentContainer.Controls.Add(browser);

 

            browser.Dock                   = DockStyle.Fill;

            browser.ScriptErrorsSuppressed = true;

 

            page.ContentContainer.Controls.Add(new NavigationControl());

        }

 

        #endregion

        #region 새 항목 추가하기 - AddNewItem(uri)

 

        /// <summary>

        /// 새 항목 추가하기

        /// </summary>

        /// <param name="uri">URI</param>

        private void AddNewItem(string uri)

        {

            if(NavigationPanel == null)

            {

                return;

            }

 

            if(uri != string.Empty)

            {

                bool isAdded = false;

 

                for(int i = 0; i < NavigationPanel.SearchPanel.Items.Count; i++)

                {

                    if(NavigationPanel.SearchPanel.Items[i].ToString() == uri)

                    {

                        isAdded = true;

 

                        break;

                    }

                }

 

                if(!isAdded)

                {

                    NavigationPanel.SearchPanel.Items.Add(uri);

                }

            }

        }

 

        #endregion

        #region 항목으로 이동하기 - GoToItem(address)

 

        /// <summary>

        /// 항목으로 이동하기

        /// </summary>

        /// <param name="address">주소</param>

        private void GoToItem(string address)

        {

            if(address == null)

            {

                return;

            }

 

            if(this.currentAddress != address)

            {

                NavigationPanel.SearchItem.EditValue = address;

 

                try

                {

                    if(WebBrowser != null && !WebBrowser.IsBusy)

                    {

                        WebBrowser.Navigate(address);

                    }

                }

                catch

                {

                }

            }

        }

 

        #endregion

        #region 주소 검증하기 - ValidateAddress(address)

 

        /// <summary>

        /// 주소 검증하기

        /// </summary>

        /// <param name="address">주소</param>

        /// <returns>주소 검증 여부</returns>

        private bool ValidateAddress(string address)

        {

            string[] nameArray = new string[] { "javascript:" };

 

            foreach(string name in nameArray)

            {

                if(address.IndexOf(name) == 0)

                {

                    return false;

                }

            }

 

            return true;

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.Reflection;

using System.Windows.Forms;

 

using DevExpress.Skins;

using DevExpress.Utils;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    static class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        private static void Main()

        {

            SkinManager.EnableFormSkins();

 

            BrowserEmulationHelper.DisableBrowserEmulation(Assembly.GetEntryAssembly().GetName().Name);

 

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

 

            new MainForm().Show();

 

            Application.Run();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요