Tải bản đầy đủ - 0 (trang)
7-20. Display a Web Page in a Windows-Based Application

7-20. Display a Web Page in a Windows-Based Application

Tải bản đầy đủ - 0trang

CHAPTER 7 ■ WINDOWS FORMS



How It Works

The WebBrowser control makes it a trivial task to embed highly functional web browser capabilities into

your Windows applications. The WebBrowser control is responsible for the display of web pages and

maintaining page history, but it does not provide any controls for user interaction. Instead, the

WebBrowser control exposes properties and events that you can manipulate programmatically to control

the operation of the WebBrowser. This approach makes the WebBrowser control highly flexible and

adaptable to most common browsing requirements. Table 7-1 summarizes some of the WebBrowser

members related to web navigation that you will find particularly useful.

Table 7-1. Commonly Used Members of the WebBrowser Control



Member



Description



Property

AllowNavigation



Controls whether the WebBrowser can navigate to another page after its initial page

has been loaded



CanGoBack



Indicates whether the WebBrowser currently holds back page history, which would

allow the GoBack method to succeed



CanGoForward



Indicates whether the WebBrowser currently holds forward page history, which

would allow the GoForward method to succeed



IsBusy



Indicates whether the WebBrowser is currently busy downloading a page



Url



Holds the URL of the currently displayed/downloading page



Method

GoBack



Displays the previous page in the page history



GoForward



Displays the next page in the page history



GoHome



Displays the home page of the current user as configured in Windows



Navigate



Displays the web page at the specified URL



Stop



Stops the current WebBrowser activity



Event

DocumentCompleted



Signals that the active download has completed and the document is displayed in

the WebBrowser



357



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



You can also use the WebBrowser.DocumentText property to set (or get) the currently displayed HTML

contents of the WebBrowser. To manipulate the contents using the Document Object Model (DOM), get

an HtmlDocument instance via the Document property.



The Code

The following example uses the WebBrowser control to allow users to navigate to a web page whose

address is entered into a TextBox. Buttons also allow users to move forward and backward through page

history and navigate directly to their personal home page.

using System;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_20 : Form

{

public Recipe07_20()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-20.Designer.cs.

InitializeComponent();

}

private void goButton_Click(object sender, EventArgs e)

{

// Navigate to the URL specified in the text box.

webBrowser1.Navigate(textURL.Text);

}

private void homeButton_Click(object sender, EventArgs e)

{

// Navigate to the current user's home page.

webBrowser1.GoHome();

}

protected override void OnLoad(EventArgs e)

{

// Call the OnLoad method of the base class to ensure the Load

// event is raised correctly.

base.OnLoad(e);

// Navigate to the Apress home page when the application first

// loads.

webBrowser1.Navigate("http://www.apress.com");

}



358



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



private void backButton_Click(object sender, EventArgs e)

{

// Go to the previous page in the WebBrowser history.

webBrowser1.GoBack();

}

private void forwarButton_Click(object sender, EventArgs e)

{

// Go to the next page in the WebBrowser history.

webBrowser1.GoForward();

}

// Event handler to perform general interface maintenance once a document

// has been loaded into the WebBrowser.

private void webBrowser1_DocumentCompleted(object sender,

WebBrowserDocumentCompletedEventArgs e)

{

// Update the content of the TextBox to reflect the current URL.

textURL.Text = webBrowser1.Url.ToString();

// Enable or disable the Back button depending on whether the

// WebBrowser has back history.

if (webBrowser1.CanGoBack)

{

backButton.Enabled = true;

}

else

{

backButton.Enabled = false;

}

// Enable or disable the Forward button depending on whether the

// WebBrowser has forward history.

if (webBrowser1.CanGoForward)

{

forwarButton.Enabled = true;

}

else

{

forwarButton.Enabled = false;

}

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_20());

}

}

}



359



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-21. Display WPF Windows in a Windows Forms Application

Problem

You need to display a WPF window in a Windows Forms application.



Solution

Create an instance of the WPF window (System.Windows.Window) you want to display in your Windows

Forms code. Call Window.ShowDialog to display a modal window, or call Window.Show to display a

modeless window.



How It Works

The trickiest thing about displaying a WPF window in a Windows Forms application is actually

integrating the WPF source code into your project correctly if you are using Visual Studio. There is no

option in your Windows Forms project to add a WPF Window when you select Add New Item in Solution

Explorer.

The easiest way around this is to import an existing WPF Window using the Add Existing option in

Solution Explorer. This will set everything up appropriately (adding the necessary assembly references),

and you can then edit the WPF Window as you would when creating a WPF application. Alternatively,

Visual Studio will allow you to add a new WPF user control to your Windows Forms application. You can

use that option and then change the XAML and code-behind as required.

Once you have a WPF Window declared, you can reference and instantiate the class the same as you

would any other class. Calling Window.ShowDialog will display the window modally, meaning that the

user can interact with only that window and must close it before they can interact again with the rest of

the application. Calling Window.Show will display a modeless window, allowing the user to interact with

the new window as well as the rest of the application.



The Code

The following example (shown running in Figure 7-13) displays a Windows Form with two buttons. The

left button opens and closes a modeless WPF window, and the right button opens a modal window.

When the example creates the modeless window, it subscribes an event handler to the Window.Closing

event so that the application can update the button state should the user choose to close the window

directly instead of using the button. The following code is the code-behind for the main Windows Form:

using System;

using System.ComponentModel;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_21 : Form

{

private Window1 modelessWindow;



360



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



private CancelEventHandler modelessWindowCloseHandler;

public Recipe07_21()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-21.Designer.cs.

InitializeComponent();

modelessWindowCloseHandler = new CancelEventHandler(Window_Closing);

}

// Handles the button click event to open and close the modeless

// WPF window.

private void OpenModeless_Click(object sender, EventArgs e)

{

if (modelessWindow == null)

{

modelessWindow = new Window1();

// Add an event handler to get notification when the window

// is closing.

modelessWindow.Closing += modelessWindowCloseHandler;

// Change the button text.

btnOpenModeless.Text = "Close Modeless Window";

// Show the Windows Form.

modelessWindow.Show();

}

else

{

modelessWindow.Close();

}

}

// Handles the button click event to open the modal WPF Window.

private void OpenModal_Click(object sender, EventArgs e)

{

// Create and display the modal window.

Window1 window = new Window1();

window.ShowDialog();

}

// Handles the WPF Window's Closing event for the modeless window.

private void Window_Closing(object sender, CancelEventArgs e)

{

// Remove the event handler reference.

modelessWindow.Closing -= modelessWindowCloseHandler;

modelessWindow = null;



361



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-20. Display a Web Page in a Windows-Based Application

Tải bản đầy đủ ngay(0 tr)

×