您的位置:首页 > 编程语言 > C#

[回答sqzxcv 的]如何实现 c# 调用 搜索引擎

2009-04-24 14:24 316 查看
问题:

我想在一个TextBox里面输入一段内容,然后用程序转到百度里面进行搜索,将搜索结果用百度的网页在我的WebBrowser里面显示,我该怎么做?
就像在浏览器上的插件,在一个文本框里面输入内容,然后就会自动跳到 狗狗搜索里面那样 ,该怎么做?

今天下午无聊,看到这个帖子,我想了下并不难,因此花了半小时把这东西全部做好.

baidu的搜索方法是:http://www.baidu.com/s?ie=gb2312&wd=要搜索的内容
思路是先做个webbrowser插件,然后通过在html页面中调用该插件的对外类,用webbrowser打开搜索结果.
粗略的做了下:
要做IE插件,首先先在项目中创建一个WindowsFormsControlLibrary,对AssemblyInfo.cs里的ComVisible设置为true,

在项目里添加IObjectSafty.cs类,通知IE浏览器这个是安全程序.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace WindowsFormsControlLibrary1
{

[
Serializable,
ComVisible(true)
]
public enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
};
[
ComImport(),
Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface IObjectSafety
{
[PreserveSig]
long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions);

[PreserveSig]
long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions);
};
}


再新建一个form1 windows窗口程序,添加webbrowser控件,将webbrowser设置为public

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsControlLibrary1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
}
}


接着修改usercontrol的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsControlLibrary1
{

[Guid("136D5CDF-C954-44dc-9345-F7E63F456C1D")]
public partial class UserControl1 : UserControl, IObjectSafety
{
#region [IObjectSafety implementation]
private ObjectSafetyOptions m_options =
ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER |
ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;

public long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return 0;
}

public long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions)
{
return 0;
}
#endregion
public UserControl1()
{
InitializeComponent();
}
public void webbrowser(string url)
{
Form1 f = new Form1();
f.webBrowser1.Navigate(url);
f.Show();
}
}
}


注:[Guid("136D5CDF-C954-44dc-9345-F7E63F456C1D")]里面的136D5CDF-C954-44dc-9345-F7E63F456C1D你可以通过 vs工具选项里的创建Guid随便创建个.

最后修改项目属性:在生成栏目中为com相互操作注册勾上.这样代码写好了

现在制作安装包,添加安装部署里的安装项目,添加项目,将项目的注册方式设为vsdrpCOM,然后安装.

安装好了以后在html页面中调用:

<body>
<object classid="clsid:136D5CDF-C954-44dc-9345-F7E63F456C1D"
id="AutoVueX1">
</object>
<input type='button' onclick='AutoVueX1.webbrowser("http://www.baidu.com/s?ie=gb2312&wd=要搜索的内容");' value='搜索google'>
</body>


OK了哈哈

如果要源代码的请留言!~

源代码下载地址:http://download.csdn.net/source/1255816
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: