欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件

 更新時(shí)間:2016年03月03日 15:18:41   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件的相關(guān)資料,感興趣的小伙伴們可以參考一下

本文詳細(xì)為大家分享了C#多選項(xiàng)卡的瀏覽器控件的設(shè)計(jì)與實(shí)現(xiàn),供大家參考,具體內(nèi)容如下

1.  為什么我們需要多選項(xiàng)卡的瀏覽器控件
項(xiàng)目中需要使用WinForm應(yīng)用程序來包裝BS應(yīng)用程序的瀏覽器外殼,在.NET的WebBrowser中沒有多選項(xiàng)卡瀏覽的自帶配置屬性,我們需要實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件來實(shí)現(xiàn)包裝BS應(yīng)用程序的目的,而不會彈出IE瀏覽器窗口。

2. 我們需要了解哪些知識點(diǎn)
2.1.     WebBrowser控件
WebBrowser 控件為 WebBrowser ActiveX 控件提供了托管包裝。托管包裝使您可以在 Windows 窗體客戶端應(yīng)用程序中顯示網(wǎng)頁。使用 WebBrowser 控件,可以復(fù)制應(yīng)用程序中的 Internet Explorer Web 瀏覽功能,還可以禁用默認(rèn)的 Internet Explorer 功能,并將該控件用作簡單的 HTML 文檔查看器。

l  如何:使用 WebBrowser 控件定位到 URL

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

l  WebBrowser的 CreateSink 方法和DetachSink 方法

CreateSink方法使基礎(chǔ) ActiveX 控件與可以處理控件事件的客戶端相關(guān)聯(lián)。

DetachSink方法使從基礎(chǔ) ActiveX 控件中釋放附加在 CreateSink 方法中的事件處理客戶端。

下面的代碼示例演示如何在派生自 WebBrowser 的類中使用此方法,該方法使用 OLE DWebBrowserEvents2 接口中的 NavigateError 事件對常規(guī) WebBrowser 事件進(jìn)行補(bǔ)充。

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 cookie;

    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 :

      StandardOleMarshalObject, 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, MarshalAs(UnmanagedType.IDispatch)] object pDisp,

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

      [In] ref object statusCode, [In, Out] ref bool cancel);

  }

}

l  WebBrowser. DocumentCompleted 事件

在 WebBrowser 控件完成加載文檔時(shí)發(fā)生。

處理 DocumentCompleted 事件,在新文檔完成加載時(shí)接收通知。如果 DocumentCompleted 事件發(fā)生,則新文檔已完全加載,這意味著可以通過 Document、DocumentText 或 DocumentStream 屬性訪問該文檔的內(nèi)容。

2.2.   TabControl控件
TabControl 控件是Windows 窗體多個選項(xiàng)卡控件,這些選項(xiàng)卡類似于筆記本中的分隔卡和檔案柜文件夾中的標(biāo)簽。選項(xiàng)卡中可包含圖片和其他控件。您可以使用該選項(xiàng)卡控件來生成多頁對話框,這種對話框在 Windows 操作系統(tǒng)中的許多地方(例如控制面板的“顯示”屬性中)都可以找到。

l  如何:將控件添加到選項(xiàng)卡頁

tabPage1.Controls.Add(new Button()); 

l  如何:使用 Windows 窗體 TabControl 添加和移除選項(xiàng)卡

添加選項(xiàng)卡

string title = "TabPage " + (tabControl1.TabCount + 1).ToString();

TabPage myTabPage = new TabPage(title);

tabControl1.TabPages.Add(myTabPage);

移除選項(xiàng)卡

tabControl1.TabPages.Remove(tabControl1.SelectedTab); 

l  TabControl.DrawItem 事件

如果將 DrawMode 屬性設(shè)置為 OwnerDrawFixed,則每當(dāng) TabControl 需要繪制它的一個選項(xiàng)卡時(shí),它就會引發(fā) DrawItem 事件。若要自定義選項(xiàng)卡的外觀,請?jiān)谟糜?DrawItem 事件的處理程序中提供自己的繪制代碼。

下面的代碼示例創(chuàng)建一個包含一個 TabPage 的 TabControl。本示例聲明一個事件處理程序,并用來在 tabPage1 的選項(xiàng)卡上繪制字符串和 Rectangle。該事件處理程序綁定到 DrawItem 事件。

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.  我們怎么設(shè)計(jì)多選項(xiàng)卡的瀏覽器控件
需要實(shí)現(xiàn)的功能特性:

l  實(shí)現(xiàn)打開BS應(yīng)用程序的鏈接或窗口跳轉(zhuǎn)到選項(xiàng)卡中而不是新窗口。

l  實(shí)現(xiàn)選項(xiàng)卡的關(guān)閉和新建,注意只有一個選項(xiàng)卡得時(shí)候不可以選項(xiàng)卡不可以出現(xiàn)關(guān)閉圖片按鈕。

我們主要采用TabControl和WebBrowser來實(shí)現(xiàn)多選項(xiàng)卡瀏覽器控件開發(fā)。

現(xiàn)介紹主要控件實(shí)現(xiàn)代碼。

u  新建選項(xiàng)卡頁面代碼實(shí)現(xiàn)如下:

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(webBrowser1_BeforeNewWindow);

  web.Navigate(url);

  _webBrowserLists.Add(web);

 

  TabPage tbp = new TabPage();

  tbp.Name = "TabPage" + tabControl1.TabCount.ToString();

  tbp.Text = "空白頁";

  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 把網(wǎng)頁標(biāo)題及圖片關(guān)閉按鈕的繪制選項(xiàng)卡中代碼實(shí)現(xiàn)如下:

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)

{

 

  try

  {

    Graphics g = e.Graphics;

 

    Rectangle tabRectangle = this.tabControl1.GetTabRect(e.Index);

 

    //先添加TabPage屬性 

    g.DrawString(this.tabControl1.TabPages[e.Index].Text

    , this.Font, SystemBrushes.ControlText, tabRectangle.X + 3, tabRectangle.Y + 3);

 

    if (tabControl1.TabCount > 1)

    {

      //再畫一個矩形框

      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);

  }

}

u  Webbrowser控件完成時(shí)及Webbrowser控件新建窗口時(shí)代碼實(shí)現(xiàn)如下:

private void webBrowser1_DocumentCompleted(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;

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論