첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ IOleServiceProvider.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// OLE 서비스 제공자 인터페이스
    /// </summary>
    [ComImport]
    [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleServiceProvider
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 서비스 쿼리하기 - QueryService(serviceGUID, interfaceGUID, instance)

        /// <summary>
        /// 서비스 쿼리하기
        /// </summary>
        /// <param name="serviceGUID">서비스 GUID</param>
        /// <param name="interfaceGUID">인터페이스 GUID</param>
        /// <param name="instance">인스턴스</param>
        /// <returns>처리 결과</returns>
        [PreserveSig]
        int QueryService([In] ref Guid serviceGUID, [In] ref Guid interfaceGUID, [MarshalAs(UnmanagedType.IDispatch)] out object instance);

        #endregion
    }
}

 

728x90

 

▶ WebBrowserHelper.cs

using System;
using System.Reflection;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 웹 브라우저 헬퍼
    /// </summary>
    public static class WebBrowserHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 스크립트 에러 억제하기 - SuppressScriptError(webBrowser, suppress)

        /// <summary>
        /// 스크립트 에러 억제하기
        /// </summary>
        /// <param name="webBrowser">웹 브라우저</param>
        /// <param name="suppress">억제 여부</param>
        public static void SuppressScriptError(WebBrowser webBrowser, bool suppress)
        {
            IOleServiceProvider oleServiceProvider = webBrowser.Document as IOleServiceProvider;

            if(oleServiceProvider != null)
            {
                Guid IWebBrowserAppGUID = new Guid("0002DF05-0000-0000-C000-000000000046");
                Guid IWebBrowser2GUID   = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");

                object webBrowserObject;

                oleServiceProvider.QueryService(ref IWebBrowserAppGUID, ref IWebBrowser2GUID, out webBrowserObject);

                if(webBrowserObject != null)
                {
                    webBrowserObject.GetType().InvokeMember
                    (
                        "Silent",
                        BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty,
                        null,
                        webBrowserObject,
                        new object[] { suppress }
                    );
                }
            }
        }

        #endregion
    }
}

 

300x250

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="WebBrowser 엘리먼트 : 스크립트 오류 억제하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Border
        Margin="10"
        BorderThickness="1"
        BorderBrush="Black">
        <WebBrowser Name="webBrowser" />
    </Border>
</Window>

 

반응형

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Navigation;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            Loaded                    += Window_Loaded;
            this.webBrowser.Navigated += webBrowser_Navigated;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.webBrowser.Navigate(new Uri("https://www.cnn.com"));
        }

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

        /// <summary>
        /// 웹 브라우저 탐색시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            WebBrowserHelper.SuppressScriptError(this.webBrowser, true);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요