2014-09-22 61 views
2

我有一个Windows窗体应用程序。我正在使用webbrowser控件。我想模拟ctrl + u函数允许显示html页面的源代码。c#webbrowser控件,如何模拟ctrl +ü

+0

当我们在普通网络浏览器(如chrome,Firfox,IE,...)中模拟ctrl + u时,会出现一个新选项卡并包含网页的源代码。 我想在我的网页浏览器控件中创建这个功能。 – user3501155 2014-09-22 11:01:14

+0

那么,这是什么问题? – 2014-09-22 11:10:02

+0

我该如何做到这一点 – user3501155 2014-09-22 11:11:11

回答

0

希望这可以帮助你。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new MainForm()); 
     } 

     // form which contains the web browser 
     public partial class MainForm : Form 
     { 
      public WebBrowser web = new WebBrowser(); 
      public MainForm() 
      { 


       web.Height = this.Height; 
       web.Width = this.Width; 
       web.Top = 0; 
       web.Left = 0; 
       web.Dock = DockStyle.Fill; 
       this.Controls.Add(web); 

       web.Navigate("http://www.google.com"); 
      } 
      protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
      { 
       if (keyData == (Keys.Control | Keys.U)) 
       { 
        SourceForm scr = new SourceForm(this.web); 
        scr.Show(); 

        return true; 
       } 
       return base.ProcessCmdKey(ref msg, keyData); 
      } 
     } 


     //the form which will be shown once you press CTRL+U 
     public class SourceForm : Form 
     { 
      public TextBox sourceText; 
      public SourceForm(WebBrowser web) 
      { 
       sourceText = new TextBox(); 
       sourceText.Multiline = true; 
       sourceText.ScrollBars = ScrollBars.Both; 
       sourceText.Left = 0; 
       sourceText.Top = 0; 
       sourceText.Dock = DockStyle.Fill; 
       sourceText.Height = this.Height; 
       sourceText.Width = this.Width; 
       this.Controls.Add(sourceText); 
       this.sourceText.Text = web.DocumentText; 
       this.Text = web.DocumentTitle; 
      } 
     } 

    } 
}