C # Design and Implementation of browser controls with multiple tabs

Source: Internet
Author: User
ArticleDirectory
    • 2.1. webbrowser Control
    • 2.2. tabcontrol Control
1. Why do we need browser controls with multiple tabs?

Use the winform application in the projectProgramTo package the browser shell of the BS application.. Net webbrowser does not have the built-in configuration properties of Multi-tab browsing. We need to implement multi-tab browser controls to wrap the BS application, instead of the IE browser window.

2. What knowledge points do we need to know? 2.1. webbrowser Control

The webbrowser control provides managed packaging for the webbrowser ActiveX control. Hosted packaging allows you to display web pages in Windows Forms client applications. You can use the webbrowser control to copy the Internet Explorer Web browsing function in an application, disable the default Internet Explorer function, and use the control as a simple HTML document viewer.

LHow to: UseWebbrowserControlURL

This. webbrowser1.navigate ("http://www.microsoft.com ");

LWebbrowserOfCreatesinkMethod andDetachsinkMethod

The createsink method associates the Basic ActiveX control with the client that can handle the control event.

The detachsink method releases the event processing client attached to the createsink method from the Basic ActiveX control.

The followingCodeThe example shows how to use this method in a class derived from webbrowser. This method uses the navigateerror event in the OLE dwebbrowserevents2 interface to supplement the general webbrowser event.

Using system;

Using system. Windows. forms;

Using system. runtime. interopservices;

Using system. Security. permissions;

 

Namespace webbrowserextensibility

{

[Permissionsetattribute (securityaction. Demand, name = "fulltrust")]

Public class form1: Form

{

[Stathread]

Public static void main ()

{

Application. Run (New form1 ());

}

Private webbrowser2 WB = new webbrowser2 ();

Public form1 ()

{

WB. Dock = dockstyle. Fill;

WB. navigateerror + = new

Webbrowsernavigateerroreventhandler (wb_navigateerror );

Controls. Add (WB );

WB. navigate ("www.widgets.microsoft.com ");

}

Private void wb_navigateerror (

Object sender, webbrowsernavigateerroreventargs E)

{

// Display an error message to the user.

MessageBox. Show ("cannot navigate to" + E. url );

}

}

 

Public class webbrowser2: webbrowser

{

Axhost. connectionpointcookie;

Webbrowser2eventhelper helper;

[Permissionsetattribute (securityaction. linkdemand, name = "fulltrust")]

Protected override void createsink ()

{

Base. createsink ();

Helper = new webbrowser2eventhelper (this );

Cookie = new axhost. connectionpointcookie (

This. activexinstance, helper, typeof (dwebbrowserevents2 ));

}

 

[Permissionsetattribute (securityaction. linkdemand, name = "fulltrust")]

Protected override void detachsink ()

{

If (cookie! = NULL)

{

Cookie. Disconnect ();

Cookie = NULL;

}

Base. detachsink ();

}

Public event webbrowsernavigateerroreventhandler navigateerror;

Protected virtual void onnavigateerror (

Webbrowsernavigateerroreventargs E)

{

If (this. navigateerror! = NULL)

{

This. navigateerror (this, e );

}

}

Private class webbrowser2eventhelper:

Standardoleexternalobject, dwebbrowserevents2

{

Private webbrowser2 parent;

 

Public webbrowser2eventhelper (webbrowser2 parent)

{

This. Parent = parent;

}

 

Public void navigateerror (Object Pdisp, ref object URL,

Ref object frame, ref object statuscode, ref bool cancel)

{

// Raise the navigateerror event.

This. Parent. onnavigateerror (

New webbrowsernavigateerroreventargs (

(String) URL, (string) frame, (int32) statuscode, cancel ));

}

}

}

Public Delegate void webbrowsernavigateerroreventhandler (Object sender,

Webbrowsernavigateerroreventargs E );

Public class webbrowsernavigateerroreventargs: eventargs

{

Private string urlvalue;

Private string framevalue;

Private int32 statuscodevalue;

Private Boolean cancelvalue;

 

Public webbrowsernavigateerroreventargs (

String URL, string frame, int32 statuscode, Boolean cancel)

{

Urlvalue = URL;

Framevalue = frame;

Statuscodevalue = statuscode;

Cancelvalue = Cancel;

}

 

Public String URL

{

Get {return urlvalue ;}

Set {urlvalue = value ;}

}

 

Public String Frame

{

Get {return framevalue ;}

Set {framevalue = value ;}

}

 

Public int32 statuscode

{

Get {return statuscodevalue ;}

Set {statuscodevalue = value ;}

}

 

Public Boolean cancel

{

Get {return cancelvalue ;}

Set {cancelvalue = value ;}

}

}

[Comimport, GUID ("34a715a0-6587-11d0-924a-0020afc7ac4d "),

Interfacetype (cominterfacetype. interfaceisidispatch ),

Typelibtype (typelibtypeflags. fhidden)]

Public interface dwebbrowserevents2

{

[Dispid (271)]

Void navigateerror (

[IN, financialas (unmanagedtype. idispatch)] object Pdisp,

[In] ref object URL, [in] ref object frame,

[In] ref object statuscode, [In, out] ref bool cancel );

}

}

LWebbrowser. documentcompletedEvent

This occurs when the webbrowser Control completes document loading.

Process the documentcompleted event and receive notifications when the new document is loaded. If the documentcompleted event occurs, the new document is fully loaded, which means that the content of the document can be accessed through the document, documenttext, or documentstream attributes.

2.2. tabcontrol Control

The tabcontrol control is a Windows form of multiple tab controls. These tabs are similar to the tabs in the separator card and archive folder in the notebook. The tab can contain images and other controls. You can use this tab control to generate a multi-page dialog box, which can be found in many Windows operating systems (such as the "display" attribute of the Control Panel.

LHow to: add controls to the tab page

Tabpage1.controls. Add (New button ());

 

LHow to: UseWindowsFormTabcontrolAdd and remove tabs

Add Tab

String title = "tabpage" + (tabcontrol1.tabcount + 1). tostring ();

Tabpage mytabpage = new tabpage (title );

Tabcontrol1.tabpages. Add (mytabpage );

Remove Tab

Tabcontrol1.tabpages. Remove (tabcontrol1.selectedtab );

 

LTabcontrol. drawitemEvent

If you set the drawmode attribute to ownerdrawfixed, it triggers the drawitem event whenever tabcontrol needs to draw a tab of it. To customize the appearance of a tab, provide your own drawing code in the processing program used for the drawitem event.

The following code example creates a tabcontrol that contains a tabpage. This example declares an event handler and is used to draw strings and rectangle on the tab of tabpage1. The event handler is bound to the drawitem event.

Using system. drawing;

Using system. Windows. forms;

Public class form1: Form

{

Private rectangle tabarea;

Private rectanglef tabtextarea;

Public form1 ()

{

Tabcontrol tabcontrol1 = new tabcontrol ();

Tabpage tabpage1 = new tabpage ();

Tabcontrol1.drawmode = tabdrawmode. ownerdrawfixed;

Tabcontrol1.sizemode = tabsizemode. fixed;

Tabcontrol1.controls. Add (tabpage1 );

Tabcontrol1.itemsize = new size (80, 30 );

Tabcontrol1.location = new point (25, 25 );

Tabcontrol1.size = new size (250,250 );

Tabpage1.tabindex = 0;

Clientsize = new size (300,300 );

Controls. Add (tabcontrol1 );

Tabarea = tabcontrol1.gettabrect (0 );

Tabtextarea = (rectanglef) tabcontrol1.gettabrect (0 );

Tabcontrol1.drawitem + = new drawitemeventhandler (drawontab );

}

Private void drawontab (Object sender, drawitemeventargs E)

{

Graphics G = E. graphics;

Pen P = new pen (color. Blue );

Font font = new font ("Arial", 10.0f );

Solidbrush brush = new solidbrush (color. Red );

G. drawrectangle (p, tabarea );

G. drawstring ("tabpage1", Font, brush, tabtextarea );

}

Static void main ()

{

Application. Run (New form1 ());

}

}

3. How do we design browser controls with multiple tabs?

Features to be implemented:

L open the BS application link or window to jump to the tab rather than the new window.

L disable and create tabs. Note that when only one tab is available, the "Close image" button cannot appear on the tab.

We mainly use tabcontrol and webbrowser to develop multi-tab browser controls.

This section describes the main control implementation code.

The code for creating a tab page is as follows:

Public void createnewtabpage (string URL)

{

Extendedwebbrowser web = new extendedwebbrowser ();

Web. Name = "webbroswer" + _ webbrowserlists. Count. tostring ();

Web. Dock = dockstyle. Fill;

Web. Margin = new padding (0, 0, 0, 0 );

Web. documentcompleted + = new webbrowserdocumentcompletedeventhandler (webbrowser1_documentcompleted );

Web. beforenewwindow + = new eventhandler (webbrowserappsbeforenewwindow );

Web. navigate (URL );

_ Webbrowserlists. Add (Web );

 

Tabpage TBP = new tabpage ();

TBP. Name = "tabpage" + tabcontrol1.tabcount. tostring ();

TBP. Text = "blank page ";

TBP. Padding = new padding (0, 3, 0, 0 );

TBP. Margin = new padding (0, 3, 0, 0 );

TBP. imageindex = 0;

TBP. Controls. Add (Web );

 

This. tabcontrol1.controls. Add (TBP );

This. tabcontrol1.selectedtab = TBP;

}

 

U implements the code in the drawing Tab Of the webpage title and picture close button as follows:

Private void tabcontrolincludrawitem (Object sender, drawitemeventargs E)

{

 

Try

{

Graphics G = E. graphics;

 

Rectangle tabrectangle = This. tabcontrol1.gettabrect (E. Index );

 

// Add the tabpage attribute first

G. drawstring (this. tabcontrol1.tabpages [E. Index]. Text

, This. Font, systembrushes. controltext, tabrectangle. x + 3, tabrectangle. Y + 3 );

 

If (tabcontrol1.tabcount> 1)

{

// Draw another rectangle

Using (pen p = new pen (systemcolors. Control ))

{

Tabrectangle. offset (tabrectangle. Width-(close_size + 3), 2 );

Tabrectangle. width = close_size;

Tabrectangle. Height = close_size;

G. drawrectangle (p, tabrectangle );

}

 

G. drawimage (E. State = drawitemstate. Selected? Imagelist1.images ["closeselected"]: imagelist1.images ["close"], new point (tabrectangle. X, tabrectangle. y ));

 

}

G. Dispose ();

}

Catch (exception ex)

{

Throw (Ex );

}

}

 

When the U webbrowser control is complete and the webbrowser control is used to create a window time code, the implementation is as follows:

Private void webbrowserappsdocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)

{

Extendedwebbrowser web = (extendedwebbrowser) (sender );

String title = web. Document. Title. Trim ();

Tabpage TB = (tabpage) web. parent;

TB. Text = title. length> 6? Title. substring (0, 6) + "...": title;

If (tabcontrol1.selectedtab = Tb)

{

This. Text = title;

}

}

Private void webbrowser1_beforenewwindow (Object sender, system. eventargs E)

{

Webbrowserextendednavigatingeventargs eventargs = e as webbrowserextendednavigatingeventargs;

Createnewtabpage (eventargs. url );

Eventargs. Cancel = true;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.