概述:
本文描述了如何建立个简单、常用用户Control控件——地址栏
相信只要上网朋友都知道IE里面有个提供大家输入你想去网站WebSite输入框在该输入框中你只需要输入部分它在其下拉列表框中就显示出来和你所输入相关内容(记忆功能)
如果只要求输入串就可以话那么我们可以直接使用TextBox等Control控件完成输入框但如果你要让你输入框有记忆功能话那么我们所需要就是要求能把以前所输入内容读取出来
好了废话说了半天了那么我们从下面开始讲解如何让我们地址栏有记忆功能
---------------------------------------------------------------------------------------------
建立自己地址栏:
首先我们要分两步走
第步我们首先要明白我们IE地址栏历史记忆内容是从哪来只有知道它是从哪来我们才能明白我们数据嘛
那么我们先看下IE在regedit(注册表)里面都有些什么内容regeidt是Windows里面个非常不错数据库(\')它可以把整台机子相关些东西都存放在里面
在regedit里面和IE相关内容有这些:
当然这只是部分还有部分是:
我们要是第幅图片里面“Software\\Microsoft\\Inte.Net Explorer\\TypedURLs”数据不然我们写记忆功能就起不了什么作用了或者出现些其它数据要知道在regedit里面保存数据可都是些关键数据如果不小心被人XX掉话那么L
OK现在已经找到我们要数据是从什么地方来了那么我们就要开始打造我们自己带记忆功能地址栏了
当然打到这些够了吗?当然够是够了但你不想让你地址栏功能再强大点吗?那么我们写这样个类来看看:
1、 新建项目选择新建类库名字就顺意了比如:ControlSet.URLControl
2、 在资源管理里面添加引用.Windows.Forms.dll
3、 然后在资源管理器里面把Class1.cs改为UnManagedMethods.cs然后用下面代码替换:
using ;
using .Runtime.InteropServices;
ControlSet.URLControl
{
[StructLayout(LayoutKind.Sequential)]
ernal struct Rect
{
public left;
public top;
public right;
public bottom;
}
[StructLayout(LayoutKind.Sequential)]
ernal struct ComboBoxInfo
{
public cbSize;
public Rect rcItem;
public Rect rcButton;
public IntPtr stateButton;
public IntPtr hwndCombo;
public IntPtr hwndEdit;
public IntPtr hwndList;
}
/// <summary>
/// All unmanaged DllImport methods used in this assembly
/// </summary>
ernal UnManagedMethods
{
[DllImport(\"User32.dll\")]
ernal extern bool GetComboBoxInfo(IntPtr hwndCombo, ref ComboBoxInfo info);
[DllImport(\"Shlwapi.dll\")]
ernal extern void SHAutoComplete(IntPtr hwnd, IntPtr flags);
}
}
第 2步我们地址栏出现了那么要用什么做为它基Control控件呢?
我们要有记忆功能那么当然要有个能下拉东西了什么?ComboBox就是最好选择那好我们开始用ComboBox来构建我们自己Control控件
ControlSet.URLControl
{
/// <summary>
/// A control that extends the regular combo box to show URLs.
/// </summary>
public URLComboBox : ComboBox
{
/// <summary>
/// Initilaizes a instance of URLComboBox
/// </summary>
public URLComboBox : base
{
}
}
}
首先我们添加如下引用:
using Microsoft.Win32;
在该Control控件内要用到下面些东西我们给它添加如下代码(添加到命名空间里面):
/// <summary>
/// A simple enumeration that wraps various auto complete flags of SHAutoComplete.
/// See documenation of SHAutoComplete for details
/// </summary>
[Flags]
public enum AutoCompleteFlags :
{
/// <summary>
/// This s the File as well as the rest of the shell (Desktop\\My Computer\\Control Panel\\)
/// </summary>
File = 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 s the File
/// </summary>
FileOnly = 0x00000010,
/// <summary>
/// Same as FileOnly except it _disibledevent=> /// <summary>
/// Ignore the registry default and force the auto suggest feature _disibledevent=> /// <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 _disibledevent=> /// <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 :
{
/// <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,
}
然后再在该类里面加载如下代码来完成它应该有功能:
/// <summary>
/// A control that extends the regular combo box to show URLs.
/// </summary>
public URLComboBox : ComboBox
{
/// <summary>
/// Member variable which stores the autocomplete flags
/// </summary>
private AutoCompleteFlags _flags = AutoCompleteFlags.File | AutoCompleteFlags.URLHistory | AutoCompleteFlags.URLMRU;
/// <summary>
/// Member variable which stores the mru key
/// </summary>
private _mruKey = @\"Software\\Microsoft\\Inte.Net Explorer\\TypedURLs\";
/// <summary>
/// Member variable which stores the mru key hive
/// </summary>
private MRUKeyHive _mruKeyHive = MRUKeyHive.CurrentUser;
/// <summary>
/// Initilaizes a 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>
/// <s>RegistryKey object for the MRU registry key or null none exists</s>
private RegistryKey GetMRUKey(bool writable)
{
(_mruKey.Length 0)
null;
RegistryKey ret = null;
switch(_mruKeyHive)
{
MRUKeyHive.LocalMachine:
ret = Registry.LocalMachine.OpenSubKey(_mruKey, writable);
;
MRUKeyHive.CurrentUser:
ret = Registry.CurrentUser.OpenSubKey(_mruKey, writable);
;
}
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 there is any error
.Diagnostics.Trace.WriteLine(e.Message);
.Diagnostics.Trace.WriteLine(e.StackTrace);
}
/// <summary>
/// Utility function to fill the combob box most recently typed URLs read from registry.
/// </summary>
private void MRUFill
{
(DesignMode)
;
RegistryKey mruKey = null;
try
{
i = 1;
strFormat = \"url{0}\";
object defaultValue = String.Empty;
object url;
mruKey = GetMRUKey(false);
(mruKey != null)
{
while((url = mruKey.GetValue(String.Format(strFormat, i), defaultValue)) != defaultValue)
{
Items.Add(url);
i;
}
}
}
catch(Exception e)
{
TraceIgnoredError(e);
}
finally
{
(mruKey != null)
mruKey.Close;
}
}
/// <summary>
/// Gets or s the auto complete flags
/// </summary>
[Description(\"Gets or s the auto complete flags\")]
public AutoCompleteFlags Flags
{
get
{
_flags;
}
{
_flags = value;
}
}
/// <summary>
/// Gets or s the registry key name where the combo box tains MRU list.
/// </summary>
[DescriptionAttribute(\"The registry key name where the combo box tains MRU list\")]
public MRUKey
{
get
{
_mruKey;
}
{
_mruKey = value;
}
}
/// <summary>
/// Gets or s the registry key hive for the MRUKey property.
/// </summary>
[DescriptionAttribute(\"The registry hive where the combo box tains MRU list\")]
public MRUKeyHive MRUKeyHive
{
get
{
_mruKeyHive;
}
{
_mruKeyHive = value;
}
}
/// <summary>
/// Writes the recntly typed URL to the registry it is not already there
/// </summary>
/// <param name=\"e\"></param>
protected override void _disibledevent=>
((Text.Length != 0) && (Items.IndexOf(Text) -1))
{
Items.Add(Text);
RegistryKey mruKey = null;
//Finally add it to the registry
try
{
mruKey = GetMRUKey(true);
(mruKey != null)
mruKey.SetValue(String.Format(\"url{0}\", Items.Count), Text);
}
catch(Exception ex)
{
TraceIgnoredError(ex);
}
finally
{
(mruKey != null)
mruKey.Close;
}
}
base.OnValidated(e);
}
/// <summary>
/// Finds the handle to the edit control and calls SHAutoComplete _disibledevent=> protected override void _disibledevent=> info.cbSize = .Runtime.InteropServices.Marshal.SizeOf(info);
(UnManagedMethods.GetComboBoxInfo(Handle, ref info))
{
UnManagedMethods.SHAutoComplete(info.hwndEdit, (IntPtr)_flags);
}
MRUFill;
}
}
好了那么到现在为止我们带记忆功能地址栏已经构建完成
你可以在菜单【生成(B)】里面调试生成解决方案
---------------------------------------------------------------------------------------------
建立举例:
1、 新建项目选择Windows应用名称:TestrulComboBox
2、 我们把我们所需要Control控件放到工具箱里面在工具箱上面点右键添加/移除项打开Com组件如下图:
3、 再把我们自己写Control控件也放到工具箱里面如上图点击里面浏览按钮找到你上个解决方案存放目录然后找出它所生成动态链接库文件就可以直接添加到工具箱上面了
4、 在现有项目内把拖放刚才添加到工具箱上面Microsoft Web 浏览器Control控件和刚才刚才写好Control控件拖放到主窗口上面并进行排列
5、 添加个控钮
6、 双击按钮生成事件并在事件中输入如下代码:
Cursor currentCursor = Cursor.Current;
try
{
Cursor.Current = Cursors.WaitCursor;
object arg1 = 0; object arg2 = \"\"; object arg3 = \"\"; object arg4 = \"\";
axWebBrowser1.Navigate(urlComboBox1.Text,ref arg1,ref arg2, ref arg3, ref arg4);
}
finally
{
Cursor.Current = currentCursor;
}
7、 生成解决方案
---------------------------------------------------------------------------------------------
最后:
好了你也可以自己试着做个自己、个性化浏览器了如果还想再加其他功能话那就不属于这篇文章目了
最新评论