在网上找了一些代码,运行不起来,就自己慢慢改了改,现在拿开给分享下。
IE在regedit(注册表)里面都有些什么内容。因为regeidt是Windows里面一个非常不错的数据库,它可以把整台机子相关的一些东西都存放在里面。在regedit里面,与IE相关的内容有这些:
我们要的是“software\microsoft\internet explorer\typedurls”的数据,记录的网址都在里面 如图:
控件代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32; //记得引用命名空间
using System.ComponentModel;
namespace ControlSet.URLControl
{
public class URLComboBox : ComboBox
{
/// <summary>
/// Member variable which stores the autocomplete flags
/// </summary>
private AutoCompleteFlags _flags = AutoCompleteFlags.FileSystem | AutoCompleteFlags.URLHistory | AutoCompleteFlags.URLMRU;
/// <summary>
/// Member variable which stores the mru key
/// </summary>
private string _mruKey = @"software\microsoft\internet explorer\typedurls/";
/// <summary>
/// Member variable which stores the mru key hive
/// </summary>
private MRUKeyHive _mruKeyHive = MRUKeyHive.CurrentUser;
/// <summary>
/// Initilaizes a new instance of URLComboBox
/// </summary>
public URLComboBox() : base() { }
/// <summary>
/// Gets the registry key where MRU URLs are stored
/// </summary>
/// <param name=/"writable/">Indicates whether to get the key so that it values written to it</param>
/// <returns>RegistryKey object for the MRU registry key or null if none exists</returns>
private RegistryKey GetMRUKey(bool writable)
{
if (_mruKey.Length == 0)
return null;
RegistryKey ret = null;
switch(_mruKeyHive)
{
case MRUKeyHive.LocalMachine:
ret = Registry.LocalMachine.OpenSubKey(_mruKey, writable);
break;
case MRUKeyHive.CurrentUser:
ret = Registry.CurrentUser.OpenSubKey(_mruKey, writable);
break;
}
return ret;
}
/// <summary>
/// Writes information about any ignored exception to the trace.
/// </summary>
/// <param name=/"e/">The exception which is being ignored</param>
private void TraceIgnoredError(Exception e)
{
//It/'s ok if there is any error
System.Diagnostics.Trace.WriteLine(e.Message);
System.Diagnostics.Trace.WriteLine(e.StackTrace);
}
/// <summary>
/// Utility function to fill the combob box most recently typed URLs read from registry.
/// </summary>
private void MRUFill()
{
if (DesignMode)
return;
RegistryKey mruKey = null;
try
{
int i = 1;
string strFormat = "url{0}";
object defaultValue = String.Empty;
object url;
mruKey = GetMRUKey(false);
if (mruKey != null)
{
while((url = mruKey.GetValue(String.Format(strFormat, i), defaultValue)) != defaultValue)
{
Items.Add(url);
i++;
}
}
}
catch(Exception e)
{
TraceIgnoredError(e);
}
finally
{
if (mruKey != null)
mruKey.Close();
}
}
/// <summary>
/// Gets or sets the auto complete flags
/// </summary>
[Description("Gets or sets the auto complete flags")]
public AutoCompleteFlags Flags
{
get
{
return _flags;
}
set
{
_flags = value;
}
}
/// <summary>
/// Gets or sets the registry key name where the combo box maintains MRU list.
/// </summary>
[DescriptionAttribute("The registry key name where the combo box maintains MRU list")]
public string MRUKey
{
get
{
return _mruKey;
}
set
{
_mruKey = value;
}
}
/// <summary>
/// Gets or sets the registry key hive for the MRUKey property.
/// </summary>
[DescriptionAttribute("The registry hive where the combo box maintains MRU list")]
public MRUKeyHive MRUKeyHive
{
get
{
return _mruKeyHive;
}
set
{
_mruKeyHive = value;
}
}
/// <summary>
/// Writes the recntly typed URL to the registry if it is not already there
/// </summary>
/// <param name=/"e/"></param>
protected override void OnValidated(System.EventArgs e)
{
if (DesignMode)
return;
if ((Text.Length != 0) && (Items.IndexOf(Text) == -1))
{
Items.Add(Text);
RegistryKey mruKey = null;
//Finally add it to the registry
try
{
mruKey = GetMRUKey(true);
if (mruKey != null)
mruKey.SetValue(String.Format("url{0}", Items.Count), Text);
}
catch(Exception ex)
{
TraceIgnoredError(ex);
}
finally
{
if (mruKey != null)
mruKey.Close();
}
}
base.OnValidated(e);
}
/// <summary>
/// Finds the handle to the edit control and calls SHAutoComplete on it.
/// Also fills the combobox from the values read from the registry
/// </summary>
/// <param name=/"e/">Ignored</param>
protected override void OnHandleCreated(System.EventArgs e)
{
base.OnHandleCreated(e);
if (DesignMode)
return;
//This is the right place do auto completion
ComboBoxInfo info = new ComboBoxInfo();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
if (UnManagedMethods.GetComboBoxInfo(Handle, ref info))
{
UnManagedMethods.SHAutoComplete(info.hwndEdit, (IntPtr)_flags);
}
MRUFill();
}
}
/// <summary>
/// A simple enumeration that wraps various auto complete flags of SHAutoComplete.
/// See documenation of SHAutoComplete for details
/// </summary>
[Flags]
public enum AutoCompleteFlags : int
{
/// <summary>
/// This includes the File System as well as the rest of the shell (Desktop//My Computer//Control Panel//)
/// </summary>
FileSystem = 0x00000001,
/// <summary>
/// URLs in the User/'s History
/// </summary>
URLHistory = 0x00000002,
/// <summary>
/// URLs in the User/'s Recently Used list.
/// </summary>
URLMRU = 0x00000004,
/// <summary>
/// Use the tab to move thru the autocomplete possibilities instead of to the next dialog/window control.
/// </summary>
UseTab = 0x00000008,
/// <summary>
/// This includes the File System
/// </summary>
FileSystemOnly = 0x00000010,
/// <summary>
/// Same as FileSystemOnly except it only includes directories, UNC servers, and UNC server shares.
/// </summary>
FileSystemDirs = 0x00000020,
/// <summary>
/// Ignore the registry default and force the auto suggest feature on.
/// </summary>
AutoSuggestForceOn = 0x10000000,
/// <summary>
/// Ignore the registry default and force the auto suggest feature off
/// </summary>
AutoSuggestForceOff = 0x20000000,
/// <summary>
/// Ignore the registry default and force the auto append on.
/// </summary>
AutoAppendForceOn = 0x40000000,
/// <summary>
/// Ignore the registry default and force auto append off.
/// </summary>
AutoAppendForceOff = -2147483648
}
/// <summary>
/// Enumeration for possible types of registry base keys for storing most recntly typed URLs
/// </summary>
public enum MRUKeyHive : int
{
/// <summary>
/// Value that indicates HKEY_CURRENT_USER should be used for MRUKey property
/// </summary>
CurrentUser = 1,
/// <summary>
/// Value that indicates HKEY_LOCAL_MACHINE should be used for MRUKey property
/// </summary>
LocalMachine = 2,
}
}
测试下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestAutoUrl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Cursor currentcursor = Cursor.Current;
try
{
Cursor.Current = Cursors.WaitCursor;
webBrowser1.Navigate(urlComboBox1.Text);
}catch(Exception ex)
{
}
finally
{
Cursor.Current = currentcursor;
}
}
}
}
好了基本上就这些了。不好的地方请多见谅! 呵呵