C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件
本文詳細(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)用程序的目的,而不會(huì)彈出IE瀏覽器窗口。
2. 我們需要了解哪些知識(shí)點(diǎn)
2.1. WebBrowser控件
WebBrowser 控件為 WebBrowser ActiveX 控件提供了托管包裝。托管包裝使您可以在 Windows 窗體客戶端應(yīng)用程序中顯示網(wǎng)頁(yè)。使用 WebBrowser 控件,可以復(fù)制應(yīng)用程序中的 Internet Explorer Web 瀏覽功能,還可以禁用默認(rèn)的 Internet Explorer 功能,并將該控件用作簡(jiǎn)單的 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 事件對(duì)常規(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 窗體多個(gè)選項(xiàng)卡控件,這些選項(xiàng)卡類似于筆記本中的分隔卡和檔案柜文件夾中的標(biāo)簽。選項(xiàng)卡中可包含圖片和其他控件。您可以使用該選項(xiàng)卡控件來生成多頁(yè)對(duì)話框,這種對(duì)話框在 Windows 操作系統(tǒng)中的許多地方(例如控制面板的“顯示”屬性中)都可以找到。
l 如何:將控件添加到選項(xiàng)卡頁(yè)
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 需要繪制它的一個(gè)選項(xiàng)卡時(shí),它就會(huì)引發(fā) DrawItem 事件。若要自定義選項(xiàng)卡的外觀,請(qǐng)?jiān)谟糜?DrawItem 事件的處理程序中提供自己的繪制代碼。
下面的代碼示例創(chuàng)建一個(gè)包含一個(gè) TabPage 的 TabControl。本示例聲明一個(gè)事件處理程序,并用來在 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. 我們?cè)趺丛O(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)閉和新建,注意只有一個(gè)選項(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)卡頁(yè)面代碼實(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 = "空白頁(yè)"; 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)頁(yè)標(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) { //再畫一個(gè)矩形框 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; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
欄 目:C#教程
本文標(biāo)題:C#實(shí)現(xiàn)多選項(xiàng)卡的瀏覽器控件
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6660.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置