안녕하세요. 언제나 휴일에 언휴예요.
이번 강의에서는 Proxy Browser 만들기입니다.
여기에서는 사용자 정의 컨트롤 형태로 만들 거예요.
내부에 있는 실제 개체는 System.Windows.Forms.WebBrowser 컨트롤입니다.
구현은 실제 개체에 노출한 속성과 메서드, 이벤트를 같은 형태로 캡슐화합니다.
Proxy에게 명령을 내리면 Proxy는 실제 개체에게 명령을 전달하는 역할을 수행합니다. 구현은 특별한 것이 없습니다.
ProxyBrowser.xaml
ProxyBrowser.cs
using System; using System.Windows.Controls; namespace ProxyBrowser_만들기 { /// /// ProxyBrowser.xaml에 대한 상호 작용 논리 /// public partial class ProxyBrowser : UserControl { System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser(); public ProxyBrowser() { InitializeComponent(); wfh.Child = wb; wb.CanGoBackChanged += Wb_CanGoBackChanged; wb.CanGoForwardChanged += Wb_CanGoForwardChanged; wb.DocumentCompleted += Wb_DocumentCompleted; wb.DocumentTitleChanged += Wb_DocumentTitleChanged; wb.EncryptionLevelChanged += Wb_EncryptionLevelChanged; wb.FileDownload += Wb_FileDownload; wb.Navigated += Wb_Navigated; wb.Navigating += Wb_Navigating; wb.NewWindow += Wb_NewWindow; wb.ProgressChanged += Wb_ProgressChanged; wb.StatusTextChanged += Wb_StatusTextChanged; } #region 멤버 메서드 public bool GoBack() { return wb.GoBack(); } public bool GoForward() { return wb.GoForward(); } public void GoHome() { wb.GoHome(); } public void GoSearch() { wb.GoSearch(); } public void Navigate(Uri url, string targetFrameName, byte[] postData, string additionalHeaders) { wb.Navigate(url, targetFrameName, postData, additionalHeaders); } public void Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders) { wb.Navigate(urlString, targetFrameName, postData, additionalHeaders); } public void Navigate(Uri url, bool newWindow) { wb.Navigate(url, newWindow); } public void Navigate(Uri url, string targetFrameName) { wb.Navigate(url, targetFrameName); } public void Navigate(string urlString, bool newWindow) { wb.Navigate(urlString, newWindow); } public void Navigate(string urlString, string targetFrameName) { wb.Navigate(urlString, targetFrameName); } public void Navigate(Uri url) { wb.Navigate(url); } public void Navigate(string urlString) { wb.Navigate(urlString); } public void Print() { wb.Print(); } public void Refresh() { wb.Refresh(); } public void Refresh(System.Windows.Forms.WebBrowserRefreshOption opt) { wb.Refresh(opt); } public void ShowPageSetupDialog() { wb.ShowPageSetupDialog(); } public void ShowPrintDialog() { wb.ShowPrintDialog(); } public void ShowPrintPreviewDialog() { wb.ShowPrintPreviewDialog(); } public void ShowPropertiesDialog() { wb.ShowPropertiesDialog(); } public void ShowSaveAsDialog() { wb.ShowSaveAsDialog(); } public void Stop() { wb.Stop(); } #endregion 멤버 메서드 #region 멤버 속성 public bool ScriptErrosCuppressed { get { return wb.ScriptErrorsSuppressed; } set { wb.ScriptErrorsSuppressed = value; } } public bool AllowNavigation { get { return wb.AllowNavigation; } set { wb.AllowNavigation = value; } } public bool AllowWebBrowserDrop { get { return wb.AllowWebBrowserDrop; } set { wb.AllowWebBrowserDrop = value; } } public bool CanGoBack { get { return wb.CanGoBack; } } public bool CanGoForward { get { return wb.CanGoForward; } } public System.Windows.Forms.HtmlDocument Document { get { return wb.Document; } } public System.IO.Stream DocumentStream { get { return wb.DocumentStream; } set { wb.DocumentStream = value; } } public string DocumentText { get { return wb.DocumentText; } set { wb.DocumentText = value; } } public string DocumentTitle { get { return wb.DocumentTitle; } } public string DocumentType { get { return wb.DocumentType; } } public System.Windows.Forms.WebBrowserEncryptionLevel EncryptionLevel { get { return wb.EncryptionLevel; } } public bool Focused { get { return wb.Focused; } } public bool IsBusy { get { return wb.IsBusy; } } public bool IsOffline { get { return wb.IsOffline; } } public bool IsWebBrowserContextMenuEnabled { get { return wb.IsWebBrowserContextMenuEnabled; } set { wb.IsWebBrowserContextMenuEnabled = value; } } public object ObjectForScripting { get { return wb.ObjectForScripting; } set { wb.ObjectForScripting = true; } } public System.Windows.Forms.WebBrowserReadyState ReadyState { get { return wb.ReadyState; } } public bool ScriptErrorsSuppressed { get { return wb.ScriptErrorsSuppressed; } set { wb.ScriptErrorsSuppressed = value; } } public bool ScrollBarsEnabled { get { return wb.ScrollBarsEnabled; } set { wb.ScrollBarsEnabled = value; } } public string StatusText { get { return wb.StatusText; } } public Uri Url { get { return wb.Url; } set { wb.Url = value; } } public Version Version { get { return wb.Version; } } public bool WebBrowserShortcutsEnabled { get { return wb.WebBrowserShortcutsEnabled; } set { wb.WebBrowserShortcutsEnabled = value; } } #endregion 멤버 속성 #region 이벤트 발생 래핑 메서드 private void Wb_StatusTextChanged(object sender, EventArgs e) { if(StatusTextChanged != null) { StatusTextChanged(sender, e); } } private void Wb_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e) { if(ProgressChanged!= null) { ProgressChanged(sender, e); } } private void Wb_NewWindow(object sender, System.ComponentModel.CancelEventArgs e) { if(NewWindow!=null) { NewWindow(sender, e); } } private void Wb_Navigating(object sender, System.Windows.Forms.WebBrowserNavigatingEventArgs e) { if(Navigating!=null) { Navigating(sender, e); } } private void Wb_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e) { if(Navigated!=null) { Navigated(sender, e); } } private void Wb_FileDownload(object sender, EventArgs e) { if(FileDownload!=null) { FileDownload(sender, e); } } private void Wb_EncryptionLevelChanged(object sender, EventArgs e) { if(EncryptionLevelChanged != null) { EncryptionLevelChanged(sender, e); } } private void Wb_DocumentTitleChanged(object sender, EventArgs e) { if(DocumentTitleChanged != null) { DocumentTitleChanged(sender, e); } } private void Wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e) { if(DocumentCompleted != null) { DocumentCompleted(sender, e); } } private void Wb_CanGoForwardChanged(object sender, EventArgs e) { if(CanGoForwardChanged != null) { CanGoForwardChanged(sender, e); } } private void Wb_CanGoBackChanged(object sender, EventArgs e) { if(CanGoBackChanged!=null) { CanGoForwardChanged(sender, e); } } #endregion 이벤트 발생 래핑 메서드 #region 멤버 이벤트 public event EventHandler CanGoBackChanged; public event EventHandler CanGoForwardChanged; public event System.Windows.Forms.WebBrowserDocumentCompletedEventHandler DocumentCompleted; public event EventHandler DocumentTitleChanged; public event EventHandler EncryptionLevelChanged; public event EventHandler FileDownload; public event System.Windows.Forms.WebBrowserNavigatedEventHandler Navigated; public event System.Windows.Forms.WebBrowserNavigatingEventHandler Navigating; public event System.ComponentModel.CancelEventHandler NewWindow; public event System.Windows.Forms.WebBrowserProgressChangedEventHandler ProgressChanged; public event EventHandler StatusTextChanged; #endregion 멤버 이벤트 } }
MainWindows.xaml
MainWindows.cs
using System; using System.Windows; namespace ProxyBrowser_만들기 { /// /// MainWindow.xaml에 대한 상호 작용 논리 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btn_navi_Click(object sender, RoutedEventArgs e) { pb.Navigate(tbox_url.Text); } private void pb_DocumentTitleChanged(object sender, EventArgs e) { Title = pb.DocumentTitle; } } }