示例完成 百度和bing同时搜索,要增加其他搜索方式,增加配置即可。
1、引入cef
CefSharp.WinForms
2、绘制界面布局
使用splitContainer完成上下,下(左右)浏览器布局
3、设置双搜参数
配置设定
Dictionary<string, List<string>> urldic = new Dictionary<string, List<string>>() {
{"百度,bing",new List<string>(){ "https://www.baidu.com/s?wd={0}", "https://cn.bing.com/search?q={0}" } }
};
读取配置并搜索
private void button1_Click(object sender, EventArgs e)
{
List<string> ulst = urldic[comboBox1.Text];
chromiumWebBrowser1.Load(string.Format(ulst[0], textBox1.Text));
chromiumWebBrowser2.Load(string.Format(ulst[1], textBox1.Text));
}
下拉框选项绑定
this.comboBox1.Items.AddRange(new object[] {"百度,bing"});
4、搜索结果点击时,使用默认浏览器打开
绑定LifeSpanHandler
chromiumWebBrowser1.LifeSpanHandler = new LifeSpanHandler();
chromiumWebBrowser2.LifeSpanHandler = new LifeSpanHandler();
LifeSpanHandler 实现
internal class LifeSpanHandler : ILifeSpanHandler
{
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
return true;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
{
Process.Start(targetUrl);
newBrowser = null;
return true;
}
}
标签:浏览器,string,C#,自制,LifeSpanHandler,IBrowser,IWebBrowser,new,public
From: https://www.cnblogs.com/lhwsrq/p/17098356.html