Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

 

 

 

 

 

 

CREATE MENU IN PARENT FORM

 

 

 

 

 

 

 

 

 

 

 

 

 

Action

 

 

Result

 

 

 

 

 

 

 

7

In the ParentForm.cs [Design]

 

 

window, add a new top-level

 

 

Help menu.

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

 

Value

 

 

 

(Name)

 

menuHelp

 

 

 

MergeOrder

 

9

 

 

 

 

Text

 

 

&Help

 

 

 

 

 

 

 

 

 

 

8

Add a single About MyPhotos

 

 

menu item under this new

 

 

menu.

 

 

 

 

 

 

 

 

Settings

 

 

 

 

 

 

 

 

 

 

 

 

Property

 

 

Value

 

 

 

(Name)

 

 

menuAbout

 

 

 

Text

 

&About MyPhotos…

 

 

 

 

 

 

 

 

 

 

Our design is ready to go. Our next topic is the generation of wrapper classes for ActiveX controls.

18.4.2WRAPPING THE WEB BROWSER CONTROL

As we mentioned earlier, the .NET Framework provides a tool for creating a derived AxHost class from an existing ActiveX control. This section will use this tool to wrap the standard browser control for use in our application.

The Windows Forms ActiveX Control Importer program is called “aximp” and is available as part of the Visual Studio .NET product. This program is run on the command line and accepts an ActiveX control library.

C:\> aximp source-file

The source-file here is the DLL or OCX file containing the ActiveX control. For our purposes, the Web Browser control is located in the file shdocvw.dll in the Windows “system32” directory. An AxHost based class can be created with the following steps:

ACTIVEX CONTROLS

629

CREATE WRAPPER CLASS FOR WEB BROWSER CONTROL

 

Action

Result

 

 

 

1

Display a Visual Studio .NET

 

 

Command Prompt.

 

 

How-to

 

 

This is available from the Start

 

 

menu in the Microsoft Visual

 

 

Studio .NET folder, under the

 

 

Visual Studio .NET Tools heading.

 

 

 

 

2

Create a suitable directory for

 

 

holding the generated wrapper

 

 

class.

 

 

cd Windows Forms\Projects

 

 

mkdir WebBrowser

 

 

Note: This example uses the

 

 

directory “C:\Windows

 

 

Forms\Projects\WebBrowser” for

 

 

this purpose. You should use an

 

 

appropriate directory for your

 

 

application.

 

 

 

 

3

Change the current directory to be

 

 

this new directory.

 

 

cd WebBrowser

 

 

 

 

4

Generate the wrapper class by

Two new assemblies are generated in the current

 

executing the following command:

directory. These are:

 

aximp

aximp c:\winnt\

 

c:\winnt\system32\shdocvw.dll

system32\shdocvw.dll

 

Note: Depending on your operat-

 

 

ing system, you may need to

 

 

replace “c:\winnt” in this com-

 

 

mand with the appropriate Win-

 

 

dows directory.

 

 

 

 

The two generated files work together to present the ActiveX control as a Windows Forms control in the .NET environment. The first file AxShDocVw.dll, is named by prepending “Ax” to the given source file name. This file encapsulates the Windows Forms proxy class for the control, derived from the AxHost class. Each object from the original library is defined under a namespace identical to the assembly name, in this case the AxShDocVw namespace.

The second file ShDocVw.dll, named identical to the given source file name, contains the common language runtime proxy for the COM types from the source library. This file is used implicitly by the Windows Forms control defined in the first file.

With a wrapper for our Web Browser control defined, we are ready to implement the internals of our About box.

630

CHAPTER 18 ODDS AND ENDS .NET

18.4.3USING THE WEB BROWSER CONTROL

So far we have defined a user interface and created a wrapper class for the Web Browser ActiveX control. In this section we will implement the AboutBox form to work as described earlier.

The following table begins this process by describing the changes required for our standard Windows Forms controls.

HANDLE THE STANDARD CONTROLS

 

Action

Result

 

 

 

1

In the AboutBox.cs code window,

protected const int SDI_ICON = 0;

 

create two constants for the two

protected const int MDI_ICON = 1;

 

types of icons in our image list.

 

 

 

 

2

Implement an IsMdiApplication

public bool IsMdiApplication

 

property to define whether the

{

 

active form is a MDI application.

get { return (lblIcon.ImageIndex

 

== MDI_ICON); }

 

 

 

How-to

set

 

a. In the get accessor, return

{

 

if (value)

 

whether the current image index

 

lblIcon.ImageIndex = MDI_ICON;

 

in the lblIcon control is the

else

 

MDI icon.

lblIcon.ImageIndex = SDI_ICON;

 

b. In the set accessor, assign the

}

 

}

 

ImageIndex for the lblIcon

 

 

control based on the assigned

 

 

value setting.

 

 

 

 

3

Implement an AboutText property

public string AboutText

 

to get or set the Text property for

{

 

the lblAboutText control.

get { return lblAboutText.Text; }

 

set { lblAboutText.Text = value; }

 

 

 

 

}

 

 

 

4

In the ParentForm class, add a

private void menuAbout_Click

 

Click handler for the About

(object sender, System.EventArgs e)

 

MyPhotos menu to create an

{

 

AboutBox dlg = new AboutBox();

 

AboutBox instance and assign its

 

dlg.IsMdiApplication = true;

 

settings.

Version ver = new

 

How-to

 

Version(Application.ProductVersion);

 

a. Set IsMdiApplication to

dlg.AboutText

 

true.

= String.Format("MyPhotos (MDI) "

 

b. Set the AboutText property to

+ "Application, Version {0:#}.{1:#} "

 

+ "\nSample for /"Windows Forms "

 

an appropriate string.

+ "Programming with C#\"\nby "

 

c. Set the Owner property to the

+ "Erik Brown \nCopyright (C) 2001 "

 

+ "Manning Publications Co.",

 

current Form.

 

ver.Major, ver.Minor);

 

d. Set the dialog’s Icon to use the

dlg.Owner = this;

 

current form’s icon.

dlg.Icon = this.Icon;

 

 

 

e. Show the dialog.

dlg.Show();

 

 

}

 

 

 

ACTIVEX CONTROLS

631

HANDLE THE STANDARD CONTROLS (continued)

 

Action

Result

 

 

 

5

Back in the AboutBox class, add a

private void linkClose_LinkClicked

 

LinkClicked handler for the

(object sender, System.Windows.Forms.

 

linkClose link label control to

LinkLabelLinkClickedEventArgs e)

 

{

 

close the form.

 

Close();

 

How-to

}

 

 

 

This is the default event for link

 

 

labels, so simply double-click the

 

 

link control in the design window.

 

 

 

 

These changes configure the controls with the appropriate information and behavior. Note that the LinkClicked event handler receives a LinkLabelLinkClickedEventArgs object as its event parameter. The LinkLabel class provides a Links property that defines one or more links, as a collection of LinkLabel.Link objects, within the single link label control. The LinkLabelLinkClickedEventArgs object specifies the link that was clicked by the user.

In our application, our labels use the entire text string as a link. Let’s continue the previous steps and handle the linkWebSite control to see how to bring up a Web Browser.

 

HANDLE THE LINKWEBSITE CONTROL

 

 

 

 

Action

Result

 

 

 

6

Add a reference to the

 

 

generated AxSHDocVW.dll

 

 

assemply in the MyPhotos

 

 

project.

 

 

How-to

 

 

In the Add Reference dialog,

 

 

click the Browse... button to

 

 

locate and select the

 

 

generated assembly.

 

 

 

 

7

In the AboutBox.cs code

using AxSHDocVw;

 

window, indicate that we will

 

 

use this library in our code.

 

 

 

 

632

CHAPTER 18 ODDS AND ENDS .NET

HANDLE THE LINKWEBSITE CONTROL (continued)

 

Action

Result

 

 

 

8

Define the following fields in

private AxWebBrowser browser;

 

our AboutBox class:

private const string startPage

 

a. A browser field representing

= "www.manning.com/eebrown";

 

 

 

a WebBrowser control.

Note: The AxWebBrowser class here is based on

 

b. A constant string contain-

 

Microsoft’s SHDocVw.dll library from the Windows

 

ing the web site we will dis-

directory. We will not cover the contents of this

 

play.

library in detail, as it is beyond the scope of our cur-

 

 

rent discussion. Look up the WebBrowser Control

 

 

index entry in the online documentation provided

 

 

with Visual Studio .NET for more information on this

 

 

class.

 

 

 

9

Add a LinkLabel event

private void linkWebSite_LinkClicked

 

handler for the linkWebSite

(object sender, System.Windows.Forms.

 

control.

LinkLabelLinkClickedEventArgs e)

 

{

 

 

 

 

 

10

If the browser control already

if (browser != null)

 

exists, then shut down the

{

 

web site and hide the Panel

// Shut down existing browser

 

pnlWebSite.Visible = false;

 

object.

 

browser.Dispose();

 

Note: This code resets the

browser = null;

 

 

 

dialog to its original state.

// Reset dialog settings

 

 

linkWebSite.Text

 

 

= "Click for book's web site";

 

 

this.Size = new Size(400, 140);

 

 

this.Text = "About MyPhotos";

 

 

}

 

 

 

11

If the browser control does not

else

 

exist, then create the browser

{

 

and define some initial

// Create web browser object

 

browser = new AxWebBrowser();

 

settings.

 

browser.Dock = DockStyle.Fill;

 

How-to

browser.TitleChange += new

 

a. Create a new AxWeb-

 

DWebBrowserEvents2_TitleChangeEventHandler

 

Browser control.

(this.browser_TitleChange);

 

b. Set its Dock property to

browser.HandleCreated += new

 

EventHandler(this.browser_HandleCreated);

 

Fill.

 

 

 

c. Add a TitleChange event

Note: The HandleCreated event is inherited from

 

handler.

 

the Control class and uses the familiar mechanism.

 

d. Add a HandleCreated

 

The TitleChange event is part of the WebBrowser

 

event handler.

control, and is part of the AxSHDocVw namespace.

 

 

Details on the DWebBrowserEvents2 interface

 

 

and the TitleChange event are included with the

 

 

online documentation for Visual Studio .NET.

 

 

 

12

Make the Panel control on the

// Show panel containing new browser

 

form visible and add the

pnlWebSite.SuspendLayout();

 

browser control to appear

pnlWebSite.Visible = true;

 

pnlWebSite.Controls.Add(browser);

 

within this panel.

 

pnlWebSite.ResumeLayout();

 

 

 

ACTIVEX CONTROLS

633

HANDLE THE LINKWEBSITE CONTROL (continued)

 

Action

Result

 

 

 

13

Modify the text displayed for

linkWebSite.Text = "Click to hide web page";

 

the linkWebSite control and

this.Size = new Size(600, 400);

 

enlarge the Form to be

}

 

}

 

600x400 pixels.

 

 

 

 

 

14

Create the handler for the

private void browser_TitleChange

 

TitleChange event to display

(object sender,

 

the new document title in the

DWebBrowserEvents2_TitleChangeEvent e)

 

{

 

title bar of the AboutBox form.

 

this.Text = e.text;

 

 

}

 

 

 

15

Add a DisplayPage method

protected void DisplayPage(string url)

 

to navigate to a given URL.

{

 

How-to

// These are required because the importer

 

// assumes these are in/out parameters

 

a. Create object instances to

// and defines them as passed by reference.

 

represent the reference

object param2 = 0;

 

object param3 = "";

 

parameters.

 

object param4 = "";

 

b. Display the wait cursor.

object param5 = "";

 

c. Use the Navigate method

try

 

to display the given URL.

{

 

d. Finally, reset the current

Cursor.Current = Cursors.WaitCursor;

 

browser.Navigate(url,

 

cursor.

 

ref param2, ref param3,

 

Note: The Navigate method

ref param4, ref param5);

 

is discussed in the online doc-

}

 

finally

 

umentation.

 

{

 

The four param objects are

Cursor.Current = Cursors.Default;

 

required here to match the

}

 

signature of the Navigate

}

 

 

 

method as defined by the

 

 

ActiveX Control Importer

 

 

(aximp.exe). A future version

 

 

of the importer may permit

 

 

these settings to be null.

 

 

 

 

16

Create the handler for the

public void browser_HandleCreated

 

HandleCreated event to

(object sender, EventArgs evArgs)

 

display the start page.

{

 

// The WebBrowser has been created

 

 

 

How-to

// Display the starting page

 

a. Display the starting page

DisplayPage(startPage);

 

 

 

using the DisplayPage

// Remove this handler

 

method.

browser.HandleCreated -= new

 

b. Remove the Handle-

EventHandler(this.browser_HandleCreated);

 

}

 

Created handler.

 

 

 

 

 

This completes our implementation. Compile and run to view the AboutBox dialog in all its glory. You will notice that when viewing the web page within our application, the user cannot navigate to an arbitrary web address. This is very different than

634

CHAPTER 18 ODDS AND ENDS .NET

Соседние файлы в папке c#