首页  编辑  

为TEdit添加IE的地址自动完成功能

Tags: /超级猛料/VCL/Memo&Edit&Richedit/Edit和Memo/   Date Created:

自动补全文件路径和名称。

function SHAutoComplete(h:hwnd;f:dword):dword;stdcall;external  'Shlwapi.dll';

procedure TForm1.Button1Click(Sender: TObject);

begin

 SHAutoComplete(Edit1.Handle,0);

end;

**************************

对文本框添加 IE5 的自动完成功能  

 IE5 introduced the AutoComplete feature, and its available for both standard VB text boxes and combo boxes using style 0 or 1. The routine here just demonstrates the textbox, but its easily extended to include the combo by passing the handle of the edit control (not the handle to the combo box - see related topics above). When used with the combo box, the AutoComplete dropdown appears in addition to the standard drop down; its data does not replace the dropdown contents.

 Use of this API is limited to systems with IE5 or greater, so a handy routine for determining the IE version is included. And although the MSDN states that CoInitialize must be called before using this API, and CoUninitialize called when the edit control has been destroyed, VB takes care of handling this task naturally.

Add a label (Label1), and text box (Text1) and a command button (Command1) to a form, and add the following:

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

Option Explicit

'Flags to control the operation of SHAutoComplete.

'The first four are used to override the Internet

'Explorer registry settings. The user can change

'these settings manually by launching the Internet

'Options property sheet from the Tools menu and

'clicking the Advanced tab.The last five can be

'used to specify which files or URLs will be

'available for autoappend or autosuggest operations.

'Ignore registry default and force feature on

Private Const SHACF_AUTOSUGGEST_FORCE_ON As Long = &H10000000

'Ignore registry default and force feature off.

Private Const SHACF_AUTOSUGGEST_FORCE_OFF As Long = &H20000000

'Ignore registry default and force feature on. (Also know as AutoComplete)

Private Const SHACF_AUTOAPPEND_FORCE_ON As Long = &H40000000

'Ignore registry default and force feature off. (Also know as AutoComplete)

Private Const SHACF_AUTOAPPEND_FORCE_OFF As Long = &H80000000

'Currently (SHACF_FILESYSTEM | SHACF_URLALL)

Private Const SHACF_DEFAULT As Long = &H0

'Includes the File System as well as the rest

'of the shell (Desktop\My Computer\Control Panel\)

Private Const SHACF_FILESYSTEM As Long = &H1

'URLs in the User's History

Private Const SHACF_URLHISTORY As Long = &H2

'URLs in the User's Recently Used list

Private Const SHACF_URLMRU As Long = &H4

Private Const SHACF_URLALL As Long = (SHACF_URLHISTORY Or SHACF_URLMRU)

'Identifies the platform for which the DLL was built.

Private Const DLLVER_PLATFORM_WINDOWS As Long = &H1 'Windows 95

Private Const DLLVER_PLATFORM_NT As Long = &H2    'Windows NT

Private Type DllVersionInfo

  cbSize As Long

  dwMajorVersion As Long

  dwMinorVersion As Long

  dwBuildNumber As Long

  dwPlatformID As Long

End Type

Private Declare Function SHAutoComplete _

  Lib "Shlwapi.dll" _

 (ByVal hwndEdit As Long, _

  ByVal dwFlags As Long) As Long

Private Declare Function DllGetVersion _

  Lib "Shlwapi.dll" _

 (dwVersion As DllVersionInfo) As Long

Private Function GetIEVersion(DVI As DllVersionInfo) As Long

  DVI.cbSize = Len(DVI)

  Call DllGetVersion(DVI)

  GetIEVersion = DVI.dwMajorVersion

End Function

Private Function GetIEVersionString() As String

  Dim DVI As DllVersionInfo

  DVI.cbSize = Len(DVI)

  Call DllGetVersion(DVI)

  GetIEVersionString = "Internet Explorer " & _

            DVI.dwMajorVersion & "." & _

            DVI.dwMinorVersion & "." & _

            DVI.dwBuildNumber

End Function

Private Sub Command1_Click()

  Dim DVI As DllVersionInfo

  If GetIEVersion(DVI) >= 5 Then

   'Turn on auto-complete

   Call SHAutoComplete(Text1.hWnd, SHACF_DEFAULT)

   'update the captions and set focus to the textbox

   Command1.Caption = "SHAutoComplete is On"

   Command1.Enabled = False

   Text1.SetFocus

   Text1.SelStart = Len(Text1.Text)

  Else

   'damn!

   MsgBox "Sorry ... you need IE5 to use this demo", vbExclamation

  End If

End Sub

Private Sub Form_Load()

 'dim a DllVersionInfo type

  Dim DVI As DllVersionInfo

 'display the version of Shlwapi

  Label1 = "Using Shlwapi.dll for " & GetIEVersionString

 'if not 5 or greater, can't do it

  Command1.Enabled = GetIEVersion(DVI) >= 5

  Command1.Caption = "SHAutoComplete is Off"

End Sub

'--end block--'