需求
- winform 应用程序使用 WebBrowser 控件连续加载两个网页进行信息签署。
遇到的问题
- 使用 DocumentCompleted 事件判断网页加载完成,可能会触发多次。。。
- 使用 NavigateError 事件判断网页加载异常,需要添加 COM Microsoft Internet Controls。
- 使用 Document.Window.Scroll 事件判断网页阅读完成。
- 修改 WebBrowser 使用浏览器内核版本,解决WebBrowser默认使用的内核是 IE7,WebBrowser 加载网页时会提示版本过低信息,发现使用 IE11 会造成 Document.Window.Scroll 事件失效。
代码
{
/// <summary>
/// 用户协议、隐私政策确认提示窗体
/// weick 2021.04.30
/// </summary>
public partial class FrmAgreementConfirm : Form
{
/// <summary>
/// 导航网页控件
/// </summary>
private WebBrowser AgreementConfirmWebBrowser;
/// <summary>
/// 用户协议url
/// </summary>
private readonly string AgreementUrl;
/// <summary>
/// 隐私政策url
/// </summary>
private readonly string PrivacyUrl;
/// <summary>
/// 用户协议再次请求
/// </summary>
private bool AgreementRequestAgain;
/// <summary>
/// 隐私政策再次请求
/// </summary>
private bool PrivacyRequestAgain;
/// <summary>
/// 用户协议隐私政策确认完成
/// </summary>
private bool ConfirmSta;
/// <summary>
/// 窗体屏蔽Alt+F4强制关闭
/// </summary>
protected override CreateParams CreateParams
{
get
{
const int CS_NOCLOSE = 0x200;
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
return cp;
}
}
private FrmAgreementConfirm()
{
InitializeComponent();
}
/// <summary>
/// 构造
/// </summary>
/// <param name="agreementUrl"></param>
/// <param name="privacyUrl"></param>
public FrmAgreementConfirm(string agreementUrl, string privacyUrl)
{
InitializeComponent();
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;//居中显示
this.ShowInTaskbar = false;//任务栏中的程序图标隐藏
//空字符串无法触发网页加载异常事件,设置默认url触发异常。
AgreementUrl = (agreementUrl == null || agreementUrl.Trim().Equals(string.Empty)) ? "http:AgreementUrl" : agreementUrl;
PrivacyUrl = (privacyUrl == null || privacyUrl.Trim().Equals(string.Empty)) ? "http:PrivacyUrl" : privacyUrl;
AgreementConfirmWebBrowser.ScriptErrorsSuppressed = true;//屏蔽脚本报错
try
{
//.NET控件WebBrowser默认使用的内核是IE7。WebBrowser加载网页时会提示版本过低信息。解决办法:调整WebBrowser使用内核版本为是IE8
FixBrowserVersionForWebBrowserControl(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
}
catch (Exception ex)
{
}
}
/// <summary>
/// 导航网页初始化
/// </summary>
private void WebBrowserInit()
{
AgreementConfirmWebBrowser = new WebBrowser();
this.panel1.Controls.Clear();
this.panel1.Controls.Add(AgreementConfirmWebBrowser);
AgreementConfirmWebBrowser.Dock = DockStyle.Fill;
//使用SHDocVw需要添加COM Microsoft Internet Controls
SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)AgreementConfirmWebBrowser.ActiveXInstance;
AgreementConfirmWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
axBrowser.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(AxBrowser_NavigateError);
//axBrowser.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(WebBrowser_NavigateComplete2);
}
/// <summary>
/// 导航网页指定资源加载
/// </summary>
/// <param name="url"></param>
private void WebBrowserNavigate(string url)
{
if (AgreementConfirmWebBrowser != null && !AgreementConfirmWebBrowser.IsDisposed)
{
AgreementConfirmWebBrowser.Navigate(url);
}
}
/// <summary>
/// 导航网页释放资源
/// </summary>
private void WebBrowserDispose()
{
if (AgreementConfirmWebBrowser != null && !AgreementConfirmWebBrowser.IsDisposed)
{
AgreementConfirmWebBrowser.Dispose();
}
}
/// <summary>
/// 首次加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmAgreementConfirm_Load(object sender, EventArgs e)
{
AgreementRequestAgain = false;
PrivacyRequestAgain = false;
WebBrowserInit();
WebBrowserNavigate(AgreementUrl);
}
/// <summary>
/// 确认或刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ConfirmBtn_Click(object sender, EventArgs e)
{
if (ClientUserContext.CurrentAccount.IsOffLine)//离线则留到下次再签署
{
this.Dispose();
return;
}
if (ConfirmSta)
{
AgreementConfirmInsert();
this.Dispose();
return;
}
if (AgreementRequestAgain)//用户协议刷新
{
AgreementRequestAgain = false;
WebBrowserInit();
WebBrowserNavigate(AgreementUrl);
return;
}
//隐私政策请求或刷新
PrivacyRequestAgain = false;
WebBrowserInit();
WebBrowserNavigate(PrivacyUrl);
}
/// <summary>
/// 网页加载完成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.ToString().Equals(AgreementUrl))
{
if (!AgreementRequestAgain)
{
//Timing(e.Url.ToString());
ConfirmBtn.Text = "确认并同意";
this.ConfirmBtn.Click -= new System.EventHandler(this.ConfirmBtn_Click);
}
}
else if (e.Url.ToString().Equals(PrivacyUrl))
{
if (!PrivacyRequestAgain)//成功返回
{
//Timing(e.Url.ToString());
ConfirmBtn.Text = "确认并同意";
this.ConfirmBtn.Click -= new System.EventHandler(this.ConfirmBtn_Click);
ConfirmSta = true;
}
}
if (AgreementConfirmWebBrowser.Document != null)
{
AgreementConfirmWebBrowser.Document.BackColor = System.Drawing.Color.Azure;
AgreementConfirmWebBrowser.Document.Window.Scroll += new HtmlElementEventHandler(AgreementConfirmWebBrowserDocumentWindowScroll);
}
}
/// <summary>
/// 导航网页指定资源加载完成
/// </summary>
/// <param name="pDisp"></param>
/// <param name="URL"></param>
private void WebBrowser_NavigateComplete2(object pDisp, ref object URL)
{
}
/// <summary>
/// 网页加载异常
/// </summary>
/// <param name="pDisp"></param>
/// <param name="URL"></param>
/// <param name="Frame"></param>
/// <param name="StatusCode"></param>
/// <param name="Cancel"></param>
void AxBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
if (URL.ToString().Equals(AgreementUrl))
{
AgreementRequestAgain = true;
ConfirmBtn.Text = "刷新";
}
else if (URL.ToString().Equals(PrivacyUrl))
{
PrivacyRequestAgain = true;
ConfirmBtn.Text = "刷新";
}
}
/// <summary>
/// 计时结束才能点击确认
/// </summary>
private void Timing(string url)
{
int Time = 10;
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Time = 10;
this.ConfirmBtn.Click -= new System.EventHandler(this.ConfirmBtn_Click);
while (Time > 0)
{
TimingText(Time);
System.Threading.Thread.Sleep(1000);
Time--;
}
ConfirmBtn.Text = "确认并同意";
this.ConfirmBtn.Click += new System.EventHandler(this.ConfirmBtn_Click);
if (url.Equals(PrivacyUrl))
{
ConfirmSta = true;
}
});
}
private void TimingText(int time)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<int>(TimingText), new object[] { time });
return;
}
ConfirmBtn.Text = $"确认并同意 {time}S";
}
/// <summary>
/// 滚动到底部才能点击确认
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AgreementConfirmWebBrowserDocumentWindowScroll(object sender, HtmlElementEventArgs e)
{
HtmlDocument htmlDoc = this.AgreementConfirmWebBrowser.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
System.Diagnostics.Debug.WriteLine(scrollTop);
System.Diagnostics.Debug.WriteLine(AgreementConfirmWebBrowser.Document.Window.Size.Height);
if (AgreementConfirmWebBrowser.Document.Window.Size.Height <= scrollTop + 500)
{
this.ConfirmBtn.Click += new System.EventHandler(this.ConfirmBtn_Click);
}
}
/// <summary>
/// 修正WebBrowser控件的浏览器内核版本
/// https://www.cnblogs.com/qtiger/p/10836800.html
/// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)?redirectedfrom=MSDN
/// </summary>
/// <param name="processName">待修正的进程名:System.Diagnostics.Process.GetCurrentProcess().ProcessName</param>
private void FixBrowserVersionForWebBrowserControl(string processName)
{
int browserVersion, registerValue;
// get the installed IE version
using (var webBrowser = new System.Windows.Forms.WebBrowser())
{
browserVersion = webBrowser.Version.Major;
}
// set the appropriate IE version
if (browserVersion >= 11) registerValue = 11001;
else if (browserVersion == 10) registerValue = 10001;
else if (browserVersion == 9) registerValue = 9999;
else if (browserVersion == 8) registerValue = 8888;
else registerValue = 7000;
registerValue = 8888;//修改默认使用IE8。发现使用IE11时Document.Window.Scroll事件失效。。。。。
// set the actual key
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true))
{
if (key != null)
{
//key.SetValue(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe", registerValue, Microsoft.Win32.RegistryValueKind.DWord);
key.SetValue(processName + ".exe", registerValue, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
}
}
}
private void AgreementConfirmInsert()
{
}
}
}