webBrowser:在extendedwebbrowser中实现IDocHostShowUI.ShowMessage 并判断或触发相应事件

最近接触一客户端,需要欠套Web模式的内容页及实现相关样式和界面,webbrowser正好实现这样的思路。

要在extendedwebbrowser中截获对话框内容并将其屏蔽,详情见此帖
方法是在网上找的:对ExtendedWebBrowser的再扩展(续)

先创建自定义的webbrowser用户控件类:

MyBrowse.cs

代码
using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Windows.Forms;   
using System.Runtime.CompilerServices;   
using System.Runtime.InteropServices;

namespace WindowsApplication1   
{

    
public partial class MyBrowse : System.Windows.Forms.WebBrowser   
    {
        
public new event EventHandler<ExtendedBrowserMessageEventArgs> ShowMessage;
        
protected void OnShowMessage(ExtendedBrowserMessageEventArgs e)
        {
            
if (e == null)
                
throw new ArgumentNullException("e");

            
if (this.ShowMessage != null)
                
this.ShowMessage(this, e);
        }

        
#region ExtendedWebBrowserSite 
   

        
class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
        {
            MyBrowse _Browser;

            
public ExtendedWebBrowserSite(WebBrowser host)
                : 
base(host)
            {
                _Browser 
= (MyBrowse)host;
            }
            
void UnsafeNativeMethods.IDocHostShowUI.ShowMessage(ref UnsafeNativeMethods._RemotableHandle hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, out int plResult)
            {
                ExtendedBrowserMessageEventArgs args 
= new ExtendedBrowserMessageEventArgs(ref hwnd, lpstrText, lpstrCaption, dwType, lpstrHelpFile, dwHelpContext);
                _Browser.OnShowMessage(args);
                
if (args.Cancel)//断点1
                    plResult = args.pResult;
                
else
                {
                    
int i;
                    i 
= (int)MessageBox.Show(lpstrText, lpstrCaption, (MessageBoxButtons)dwType);//这句似乎也不起作用,直接弹出原对话框,下面的也不执行了,只能说影响不大,网上找到的唯一一段代码这里也注释掉了,直接plResult = xxx ……
                    plResult = i;
                }
            }
            
void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
            {
                
//TODO:自定义
            }
        }

  
        
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()   
        {   
            
return new ExtendedWebBrowserSite(this);   
        }  
        
#endregion   
    }   
  
    
public class UnsafeNativeMethods   
    {  
        
#region IDocHostShowUI    
        [StructLayout(LayoutKind.Explicit, Pack 
= 4)]    
        
public struct __MIDL_IWinTypes_0009    
        {    
            
// Fields    
            [FieldOffset(0)]    
            
public int hInproc;    
            [FieldOffset(
0)]    
            
public int hRemote;    
        }    
  
        [StructLayout(LayoutKind.Sequential, Pack 
= 4)]    
        
public struct _RemotableHandle    
        {    
            
public int fContext;    
            
public __MIDL_IWinTypes_0009 u;    
        }    
  
        [StructLayout(LayoutKind.Sequential, Pack 
= 4)]    
        
public struct tagPOINT    
        {    
            
public int x;   
            
public int y;   
        }   
  
        [ComImport, Guid(
"C4D244B0-D43E-11CF-893B-00AA00BDCE1A"), InterfaceType((short)1)]   
        
public interface IDocHostShowUI   
        {   
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType 
= MethodCodeType.Runtime)]   
            
void ShowMessage([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrText, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrCaption, [In] uint dwType, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrHelpFile, [In] uint dwHelpContext, [ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.LONG_PTR")] out int plResult);   
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType 
= MethodCodeType.Runtime)]   
            
void ShowHelp([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszHelpFile, [In] uint uCommand, [In] uint dwData, [In] tagPOINT ptMouse, [Out, MarshalAs(UnmanagedType.IDispatch)] object pDispatchObjectHit);   
        }  
        
#endregion    
  
    }   
}  

MyBrowse.Designer.cs

 

代码
namespace WindowsApplication1
{
    
partial class MyBrowse
    {
        
/// <summary> 
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.IContainer components = null;

        
/// <summary> 
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            
if (disposing && (components != null))
            {
                components.Dispose();
            }
            
base.Dispose(disposing);
        }

        
#region 组件设计器生成的代码

        
/// <summary> 
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            
this.SuspendLayout();
            
// 
            
// webBrowser1
            
// 
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            
this.webBrowser1.Location = new System.Drawing.Point(00);
            
this.webBrowser1.MinimumSize = new System.Drawing.Size(2020);
            
this.webBrowser1.Name = "webBrowser1";
            
this.webBrowser1.Size = new System.Drawing.Size(150150);
            
this.webBrowser1.TabIndex = 0;
            
// 
            
// MyBrowse
            
// 

            
this.Controls.Add(this.webBrowser1);
            
this.Name = "MyBrowse";
            
this.ResumeLayout(false);

        }

        
#endregion

        
private System.Windows.Forms.WebBrowser webBrowser1;
    }
}

ExtendedBrowserMessageEventArgs.cs 

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace WindowsApplication1
{
    
public class ExtendedBrowserMessageEventArgs : CancelEventArgs
    {
        
private int _plResult;
        
public int pResult
        {
            
get { return _plResult; }
        }
        
public System.Windows.Forms.DialogResult DlgResult
        {
            
set { _plResult = (int)value; }
        }

        
private UnsafeNativeMethods._RemotableHandle _hwnd;
        
public UnsafeNativeMethods._RemotableHandle hwnd
        {
            
get { return _hwnd; }
        }
        
//private IntPtr _hwnd;
        
//public IntPtr hwnd
        
//{
        
//    get { return _hwnd; }
        
//}

        
private string _lpstrText;
        
public string lpstrText
        {
            
get { return _lpstrText; }
        }

        
private string _lpstrCaption;
        
public string lpstrCaption
        {
            
get { return _lpstrCaption; }
        }

        
private uint _dwType;
        
public uint dwType
        {
            
get { return _dwType; }
        }

        
public System.Windows.Forms.MessageBoxButtons DlgButtons
        {
            
get
            {
                
switch (_dwType & (int)MsgBoxButton.MASK)
                {
                    
case (int)MsgBoxButton.MB_OKCANCEL:
                        
return System.Windows.Forms.MessageBoxButtons.OKCancel;
                    
case (int)MsgBoxButton.MB_ABORTRETRYIGNORE:
                        
return System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore;
                    
case (int)MsgBoxButton.MB_YESNOCANCEL:
                        
return System.Windows.Forms.MessageBoxButtons.YesNoCancel;
                    
case (int)MsgBoxButton.MB_YESNO:
                        
return System.Windows.Forms.MessageBoxButtons.YesNo;
                    
case (int)MsgBoxButton.MB_RETRYCANCEL:
                        
return System.Windows.Forms.MessageBoxButtons.RetryCancel;
                    
case (int)MsgBoxButton.MB_OK:
                    
default:
                        
return System.Windows.Forms.MessageBoxButtons.OK;
                }
            }
        }
        
public System.Windows.Forms.MessageBoxIcon DlgIcon
        {
            
get
            {
                
switch (_dwType & (int)MsgBoxIcon.MASK)
                {
                    
case (int)MsgBoxIcon.MB_ICONHAND:
                        
return System.Windows.Forms.MessageBoxIcon.Hand;
                    
//case (int)MsgBoxIcon.MB_ICONSTOP:
                    
//    return System.Windows.Forms.MessageBoxIcon.Stop;
                    
//case (int)MsgBoxIcon.MB_ICONERROR:
                    
//    return System.Windows.Forms.MessageBoxIcon.Error;
                    case (int)MsgBoxIcon.MB_ICONQUESTION:
                        
return System.Windows.Forms.MessageBoxIcon.Question;
                    
case (int)MsgBoxIcon.MB_ICONEXCLAMATION:
                        
return System.Windows.Forms.MessageBoxIcon.Exclamation;
                    
//case (int)MsgBoxIcon.MB_ICONWARNING:
                    
//    return System.Windows.Forms.MessageBoxIcon.Warning;
                    case (int)MsgBoxIcon.MB_ICONASTERISK:
                        
return System.Windows.Forms.MessageBoxIcon.Asterisk;
                    
//case (int)MsgBoxIcon.MB_ICONINFORMATION:
                    
//    return System.Windows.Forms.MessageBoxIcon.Information;
                    case (int)MsgBoxIcon.MB_ICONNONE:
                    
case (int)MsgBoxIcon.MB_USERICON:
                    
default:
                        
return System.Windows.Forms.MessageBoxIcon.None;
                }
            }
        }
        
public System.Windows.Forms.MessageBoxDefaultButton DlgDefaultButtons
        {
            
get
            {
                
switch (_dwType & (int)MsgBoxDefButton.MASK)
                {
                    
case (int)MsgBoxDefButton.MB_DEFBUTTON2:
                        
return System.Windows.Forms.MessageBoxDefaultButton.Button2;
                    
case (int)MsgBoxDefButton.MB_DEFBUTTON3:
                        
return System.Windows.Forms.MessageBoxDefaultButton.Button3;
                    
case (int)MsgBoxDefButton.MB_DEFBUTTON1:
                    
default:
                        
return System.Windows.Forms.MessageBoxDefaultButton.Button1;
                }
            }
        }
        
public System.Windows.Forms.MessageBoxOptions DlgOptions
        {
            
get
            {
                
switch (_dwType & (int)MsgBoxOption.MASK)
                {
                    
case (int)MsgBoxOption.MB_DEFAULT_DESKTOP_ONLY:
                        
return System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly;
                    
case (int)MsgBoxOption.MB_RIGHT:
                        
return System.Windows.Forms.MessageBoxOptions.RightAlign;
                    
case (int)MsgBoxOption.MB_RTLREADING:
                        
return System.Windows.Forms.MessageBoxOptions.RtlReading;
                    
case (int)MsgBoxOption.MB_SERVICE_NOTIFICATION1:
                    
case (int)MsgBoxOption.MB_SERVICE_NOTIFICATION2:
                        
return System.Windows.Forms.MessageBoxOptions.ServiceNotification;
                    
default:
                        
return 0;

                }
            }
        }
        
public bool displayHelpButton
        {
            
get { return (_dwType & (int)MsgBoxHelpButton.MASK) == (int)MsgBoxHelpButton.MB_HELP; }
        }

        
private string _lpstrHelpFile;
        
public string lpstrHelpFile
        {
            
get { return _lpstrHelpFile; }
        }

        
private uint _dwHelpContext;
        
public uint dwHelpContext
        {
            
get { return _dwHelpContext; }
        }

        
public ExtendedBrowserMessageEventArgs(ref UnsafeNativeMethods._RemotableHandle Hwnd, string LpstrText, string LpstrCaption, uint DwType, string LpstrHelpFile, uint DwHelpContext)
            
//public ExtendedBrowserMessageEventArgs(ref IntPtr Hwnd, string LpstrText, string LpstrCaption, uint DwType, string LpstrHelpFile, uint DwHelpContext)
            : base()
        {
            
this._hwnd = Hwnd;
            _lpstrText 
= LpstrText;
            _lpstrCaption 
= LpstrCaption;
            _dwType 
= DwType;
            _lpstrHelpFile 
= LpstrHelpFile;
            _dwHelpContext 
= DwHelpContext;
            
switch (DlgButtons)
            {
                
case System.Windows.Forms.MessageBoxButtons.OKCancel:
                
case System.Windows.Forms.MessageBoxButtons.RetryCancel:
                
case System.Windows.Forms.MessageBoxButtons.YesNoCancel:
                    DlgResult 
= System.Windows.Forms.DialogResult.Cancel;
                    
break;
                
case System.Windows.Forms.MessageBoxButtons.YesNo:
                    DlgResult 
= System.Windows.Forms.DialogResult.No;
                    
break;
                
case System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore:
                    DlgResult 
= System.Windows.Forms.DialogResult.Abort;
                    
break;
                
case System.Windows.Forms.MessageBoxButtons.OK:
                
default:
                    DlgResult 
= System.Windows.Forms.DialogResult.OK;
                    
break;
            }
        }
    }

    
public enum MsgBoxButton
    {
        MASK 
= 0x0000000F,
        MB_OK 
= 0x00000000,
        MB_OKCANCEL 
= 0x00000001,
        MB_ABORTRETRYIGNORE 
= 0x00000002,
        MB_YESNOCANCEL 
= 0x00000003,
        MB_YESNO 
= 0x00000004,
        MB_RETRYCANCEL 
= 0x00000005
    }
    
public enum MsgBoxIcon
    {
        MASK 
= 0x000000F0,
        MB_ICONNONE 
= 0x00000000,
        MB_ICONHAND 
= 0x00000010,
        MB_ICONSTOP 
= 0x00000010,
        MB_ICONERROR 
= 0x00000010,
        MB_ICONQUESTION 
= 0x00000020,
        MB_ICONEXCLAMATION 
= 0x00000030,
        MB_ICONWARNING 
= 0x00000030,
        MB_ICONASTERISK 
= 0x00000040,
        MB_ICONINFORMATION 
= 0x00000040,
        MB_USERICON 
= 0x00000080
    }
    
public enum MsgBoxDefButton
    {
        MASK 
= 0x00000F00,
        MB_DEFBUTTON1 
= 0x00000000,
        MB_DEFBUTTON2 
= 0x00000100,
        MB_DEFBUTTON3 
= 0x00000200
        
//MB_DEFBUTTON4           =   0x00000300
    }
    
public enum MsgBoxOption
    {
        MASK 
= 0x003F0000,
        
//MB_SETFOREGROUND        =   0x00010000,
        MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
        
//MB_TOPMOST              =   0x00040000,
        MB_RIGHT = 0x00080000,
        MB_RTLREADING 
= 0x00100000,
        MB_SERVICE_NOTIFICATION1 
= 0x00200000,
        MB_SERVICE_NOTIFICATION2 
= 0x00040000
    }
    
public enum MsgBoxHelpButton
    {
        MASK 
= 0x00004000,
        MB_HELP 
= 0x00004000
    }


}

Form1  : 引用窗体

代码
namespace WindowsApplication1
{
    
partial class Form1
    {
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>
        private System.ComponentModel.IContainer components = null;

        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            
if (disposing && (components != null))
            {
                components.Dispose();
            }
            
base.Dispose(disposing);
        }

        
#region Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.webBrowser1 = new WindowsApplication1.MyBrowse();
            
this.SuspendLayout();
            
// 
            
// webBrowser1
            
// 
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            
this.webBrowser1.Location = new System.Drawing.Point(00);
            
this.webBrowser1.MinimumSize = new System.Drawing.Size(2020);
            
this.webBrowser1.Name = "webBrowser1";
            
this.webBrowser1.Size = new System.Drawing.Size(364330);
            
this.webBrowser1.TabIndex = 0;
            
this.webBrowser1.Url = new System.Uri("http://localhost/ownerClient/", System.UriKind.Absolute);
            
this.webBrowser1.ShowMessage += new System.EventHandler<WindowsApplication1.ExtendedBrowserMessageEventArgs>(this.webBrowser1_ShowMessage);
            
// 
            
// Form1
            
// 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
this.ClientSize = new System.Drawing.Size(364330);
            
this.Controls.Add(this.webBrowser1);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.ResumeLayout(false);

        }

        
#endregion

        
private MyBrowse webBrowser1;
    }
}

触发的事件及调用相关动作

代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Diagnostics;  
namespace WindowsApplication1
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void webBrowser1_ShowMessage(object sender, ExtendedBrowserMessageEventArgs e)
        {
            
string txt, cap;
            txt 
= "new-" + e.lpstrText;
            cap 
= "new-" + e.lpstrCaption;
            MessageBox.Show(txt, cap);


            
if (txt.Contains("IE设置"))
            {
                
string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
                Process myNewProcess 
= new Process();
                myNewProcess.StartInfo.FileName 
= strAppFileName;

                myNewProcess.StartInfo.WorkingDirectory 
= Application.ExecutablePath;
                myNewProcess.Start();
                Application.Exit();
            }
        }
    }
}

在 webBrowser1_ShowMessage 事件中,可以即时判断webBrowser1弹出的消息内容,判断并继续相关动作。

以上是判断符合条件后,主程序自动重启。。。

 

 

封装成了用户控件,添加到解决方案下,直接在工具箱中找到houseWebBrowse 选项标签,直接拖至form就可以了。

houseWebBrowse 下载: /Files/Fooo/houseWebBrowse.rar

原文地址:https://www.cnblogs.com/Fooo/p/1700341.html