안녕하세요. 언제나 휴일에 언휴예요.
이번 강의에서는 Proxy Browser 만들기입니다.
여기에서는 사용자 정의 컨트롤 형태로 만들 거예요.
내부에 있는 실제 개체는 System.Windows.Forms.WebBrowser 컨트롤입니다.
구현은 실제 개체에 노출한 속성과 메서드, 이벤트를 같은 형태로 캡슐화합니다.
Proxy에게 명령을 내리면 Proxy는 실제 개체에게 명령을 전달하는 역할을 수행합니다. 구현은 특별한 것이 없습니다.
ProxyBrowser.xaml
1 2 3 4 5 6 7 8 9 10 |
<UserControl x:Class="ProxyBrowser_만들기.ProxyBrowser" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ProxyBrowser_만들기" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <WindowsFormsHost x:Name="wfh" /> </UserControl> |
ProxyBrowser.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
using System; using System.Windows.Controls; namespace ProxyBrowser_만들기 { /// <summary> /// ProxyBrowser.xaml에 대한 상호 작용 논리 /// </summary> 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<Window x:Class="ProxyBrowser_만들기.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ProxyBrowser_만들기" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <TextBox x:Name="tbox_url" Width="600"/> <Button x:Name="btn_navi" Content="탐색" Click="btn_navi_Click" Width="192"/> </StackPanel> <local:ProxyBrowser x:Name="pb" Grid.Row="1" ScriptErrorsSuppressed="True" DocumentTitleChanged="pb_DocumentTitleChanged"/> </Grid> </Window> |
MainWindows.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Windows; namespace ProxyBrowser_만들기 { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> 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; } } } |