本文内容:
概述
建立自己的地址栏
建立示例程序
最后
---------------------------------------------------------------------------------------------
概述:
本文描述了如何建立一个简单的、常用的用户控件——地址栏。
相信只要上网的朋友,都知道 IE 里面有一个提供大家输入你想去的网站的输入框。在该输入框中,你只需要输入部分字符,它在其下拉列表框中,就显示出来与你所输入相关的内容 ( 记忆功能 ) 。
如果只要求输入字符串就可以的话。那么,我们可以直接使用 TextBox 等控件完成输入框。但如果你要让你的输入框有记忆功能的话。那么,我们所需要的就是要求能把以前所输入的内容读取出来。
好了,废话说了半天了。那么,我们从下面开始讲解如何让我们的地址栏有记忆功能的。
---------------------------------------------------------------------------------------------
建立自己的地址栏:
首先,我们要分两步走。
第一步,我们首先要明白,我们 IE 地址栏的历史记忆内容是从哪来的。因为只有知道它是从哪来的,我们才能明白我们的数据嘛。
那么,我们先看一下, IE 在 regedit( 注册表 ) 里面都有些什么内容。因为 regeidt 是 Windows 里面一个非常不错的数据库 (^_^) ,它可以把整台机子相关的一些东西都存放在里面。
在 regedit 里面,与 IE 相关的内容有这些:

当然,这只是一部分,还有一部分是:

我们要的是第一幅图片里面的“ Software\Microsoft\Internet Explorer\TypedURLs ”的数据。不然,我们写的记忆功能就起不了什么作用了。或者,出现一些其它数据。要知道,在 regedit 里面保存的数据可都是一些关键数据。如果一不小心被人 XX 掉的话,那么, L 。
OK ,现在已经找到我们要的数据是从什么地方来的了。那么,我们就要开始打造我们自己的带记忆功能的地址栏了。
当然,打到这些够了吗?当然,够是够了。但,你不想让你的地址栏功能再强大一点吗?那么,我们写这样的一个类来看看:
1、 新建项目,选择新建类库,名字就顺意了。比如: ControlSet.URLControl 。
2、 在资源管理里面添加引用 System.Windows.Forms.dll 。
3、 然后,在资源管理器里面把 Class1.cs 改为 UnManagedMethods.cs ,然后,用下面的代码替换:
using System;
using System.Runtime.InteropServices;
namespace ControlSet.URLControl
{
[StructLayout(LayoutKind.Sequential)]
internal struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
internal struct ComboBoxInfo
{
public int cbSize;
public Rect rcItem;
public Rect rcButton;
public IntPtr stateButton;
public IntPtr hwndCombo;
public IntPtr hwndEdit;
public IntPtr hwndList;
}
///
1<summary>
2
3/// All unmanaged DllImport methods used in this assembly
4
5/// </summary>
internal class UnManagedMethods
{
[DllImport("User32.dll")]
internal static extern bool GetComboBoxInfo(IntPtr hwndCombo, ref ComboBoxInfo info);
[DllImport("Shlwapi.dll")]
internal static extern void SHAutoComplete(IntPtr hwnd, IntPtr flags);
}
}
第二步,我们的地址栏出现了。那么,要用什么做为它的基控件呢?
因为我们要有记忆功能,那么,当然,要有一个能下拉的东西了。什么? ComboBox 就是最好的选择。那好,我们开始用 ComboBox 来构建我们自己的控件。
namespace ControlSet.URLControl
{
///
1<summary>
2
3/// A control that extends the regular combo box to show URLs.
4
5/// </summary>
public class URLComboBox : ComboBox
{
///
1<summary>
2
3/// Initilaizes a new instance of URLComboBox
4
5/// </summary>
public URLComboBox() : base ()
{
}
}
}
首先,我们添加如下引用:
using Microsoft.Win32;
在该控件内要用到下面一些东西,我们给它添加如下代码 ( 添加到命名空间里面 ) :
///
1<summary>
2
3/// A simple enumeration that wraps various auto complete flags of SHAutoComplete.
4
5/// See documenation of SHAutoComplete for details
6
7/// </summary>
[Flags]
public enum AutoCompleteFlags : int
{
///
1<summary>
2
3/// This includes the File System as well as the rest of the shell (Desktop\My Computer\Control Panel\\)
4
5/// </summary>
FileSystem = 0x00000001,
///
1<summary>
2
3/// URLs in the User's History
4
5/// </summary>
URLHistory = 0x00000002,
///
1<summary>
2
3/// URLs in the User's Recently Used list.
4
5/// </summary>
URLMRU = 0x00000004,
///
1<summary>
2
3/// Use the tab to move thru the autocomplete possibilities instead of to the next dialog/window control.
4
5/// </summary>
UseTab = 0x00000008,
///
1<summary>
2
3/// This includes the File System
4
5/// </summary>
FileSystemOnly = 0x00000010,
///
1<summary>
2
3/// Same as FileSystemOnly except it only includes directories, UNC servers, and UNC server shares.
4
5/// </summary>
FileSystemDirs = 0x00000020,
///
1<summary>
2
3/// Ignore the registry default and force the auto suggest feature on.
4
5/// </summary>
AutoSuggestForceOn = 0x10000000,
///
1<summary>
2
3/// Ignore the registry default and force the auto suggest feature off
4
5/// </summary>
AutoSuggestForceOff = 0x20000000,
///
1<summary>
2
3/// Ignore the registry default and force the auto append on.
4
5/// </summary>
AutoAppendForceOn = 0x40000000,
///
1<summary>
2
3/// Ignore the registry default and force auto append off.
4
5/// </summary>
AutoAppendForceOff = -2147483648
}
///
1<summary>
2
3/// Enumeration for possible types of registry base keys for storing most recntly typed URLs
4
5/// </summary>
public enum MRUKeyHive : int
{
///
1<summary>
2
3/// Value that indicates HKEY_CURRENT_USER should be used for MRUKey property
4
5/// </summary>
CurrentUser = 1,
///
1<summary>
2
3&
4
5---</summary>