첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ Dispatcher 클래스의 Run 정적 메소드를 사용해 다중 윈도우 다중 스레드를 사용하는 방법을 보여준다. 이 메소드는 새 스레드의 시작점이다. 이 스레드의 제어하에 새 창을 만든다. WPF는 새 스레드를 관리하기 위해 새 Dispatcher를 자동으로 만든다. 창을 기능적으로 만들기 위해 해야 할 일은 Dispatcher를 시작하는 것뿐이다.

TestProject.zip
0.01MB

▶ 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="TestProject"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0"
            Orientation="Horizontal">
            <Button Name="newWindowButton"
                Width="100"
                Height="30"
                Content="New Window" />
            <TextBox Name="newLocationTextBox"
                Margin="10 0 0 0"
                Width="500"
                BorderThickness="1"
                BorderBrush="Black"
                VerticalContentAlignment="Center" />
            <Button Name="browseButton"
                Margin="10 0 0 0"
                Width="100"
                Height="30"
                Content="Browse" />
        </StackPanel>
        <Frame Name="frame" Grid.Row="1"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Margin="0 10 0 0"
            BorderThickness="1"
            BorderBrush="Black"
            NavigationUIVisibility="Hidden"
            Source="https://www.daum.net" />
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;

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

        #region 생성자 - MainWindow()

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

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

            Loaded                     += Window_Loaded;
            this.newWindowButton.Click += newWindowButton_Click;
            this.browseButton.Click    += browseButton_Click;
            this.frame.Navigated       += frame_Navigated;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

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

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

        #endregion
        #region New Window 버튼 클릭시 처리하기 - newWindowButton_Click(sender, e)

        /// <summary>
        /// New Window 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void newWindowButton_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(ProcessNewWindow);

            thread.IsBackground = true;

            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();
        }

        #endregion
        #region Browse 버튼 클릭시 처리하기 - browseButton_Click(sender, e)

        /// <summary>
        /// Browse 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            this.frame.Source = new Uri(this.newLocationTextBox.Text);
        }

        #endregion
        #region 프레임 이동 완료시 처리하기 - frame_Navigated(sender, e)

        /// <summary>
        /// 프레임 이동 완료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void frame_Navigated(object sender, NavigationEventArgs e)
        {
            NavigationService navigationService = this.frame.NavigationService;

            dynamic browser = navigationService.GetType().GetField("_webBrowser", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(navigationService);

            dynamic iWebBrowser2 = browser.GetType().GetField("_axIWebBrowser2", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(browser);

            iWebBrowser2.Silent = true;
        }

        #endregion

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

        #region 새 윈도우 처리하기 - ProcessNewWindow()

        /// <summary>
        /// 새 윈도우 처리하기
        /// </summary>
        private void ProcessNewWindow()
        {
            MainWindow mainWindow = new MainWindow();

            mainWindow.Show();

            Dispatcher.Run();
        }

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

댓글을 달아 주세요