日期:2013-04-28  浏览次数:20366 次

本文内容:

概述

建立自己的地址栏

建立示例程序

最后

---------------------------------------------------------------------------------------------

概述:
本文描述了如何建立一个简单的、常用的用户控件——地址栏。

相信只要上网的朋友,都知道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;

}



/// <summary>

/// All unmanaged DllImport methods used in this assembly

/// </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

{

/// <summary>

/// A control that extends the regular combo box to show URLs.

/// </summary>

public class URLComboBox : ComboBox

{

/// <summary>

/// Initilaizes a new instance of URLComboBox

/// </summary>

public URLComboBox() : base()

{

}

}

}




首先,我们添加如下引用:

using Microsoft.Win32;




在该控件内要用到下面一些东西,我们给它添加如下代码(添加到命名空间里面):

/// <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\)