5,447,640 members and growing! (21,297 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » Beginners     Beginner

Auto-complete edit and combo, without Platform SDK

By Chris Losinger

How to enable file system and URL auto-complete for edit controls and combo-boxes, without downloading the entire Platform SDK
VC6, VC7, C++Windows, NT4, Win2K, WinXP, Visual Studio, MFC, Dev

Posted: 25 Feb 2002
Updated: 26 Feb 2002
Views: 148,775
Bookmarked: 41 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 5.56 Rating: 4.14 out of 5
1 vote, 10.0%
1
0 votes, 0.0%
2
2 votes, 20.0%
3
3 votes, 30.0%
4
4 votes, 40.0%
5

Introduction

Here's a quick article that shows how you can add the handy auto-completion effect that you see in the Win2K file-open dialog for your own edits and combo boxes - without having to download the entire Platform SDK (9 hours on a 56K modem!). If you already have the Platform SDK, this article is probably useless to you.

Demo

I'm not providing any demo project here, the code is small enough that you can just copy and paste (one function!).

Some constants

These would be defined in the Platform SDK, but since we're not using that, I'll include these here. You'll have to copy these to your app.

// See the MSDN for SHAutoComplete for a full 

// description of what these options do.

#define SHACF_DEFAULT                   0x00000000
#define SHACF_FILESYSTEM                0x00000001
#define SHACF_URLALL                    (SHACF_URLHISTORY | SHACF_URLMRU)
#define SHACF_URLHISTORY                0x00000002
#define SHACF_URLMRU                    0x00000004
#define SHACF_USETAB                    0x00000008
#define SHACF_FILESYS_ONLY              0x00000010

#define SHACF_AUTOSUGGEST_FORCE_ON      0x10000000
#define SHACF_AUTOSUGGEST_FORCE_OFF     0x20000000
#define SHACF_AUTOAPPEND_FORCE_ON       0x40000000
#define SHACF_AUTOAPPEND_FORCE_OFF      0x80000000

The simple wrapper function

typedef HRESULT (WINAPI *SHAUTOCOMPLETEFN) 
    (HWND hTarget, DWORD dwFlags);
static HINSTANCE hSHLWAPIDLL = NULL;

bool SetupForAutoComplete(HWND hTarget, DWORD dwFlags)
{
    if (hTarget == NULL)
    {
        return false;
    }

    bool ok = false;

    if (hSHLWAPIDLL == NULL)
    {
        hSHLWAPIDLL= LoadLibrary("SHLWAPI.DLL");
        if (hSHLWAPIDLL== NULL)
        {
            return false;
        }
    }

    SHAUTOCOMPLETEFN pSHAC = 
        (SHAUTOCOMPLETEFN)GetProcAddress(hSHLWAPIDLL, 
        "SHAutoComplete");

    if (pSHAC != NULL)
    {
        ok = SUCCEEDED(pSHAC(hTarget, dwFlags));
    }

    return ok;
}

Using it

First, make sure you've called ::CoInitialize(NULL); in your CWinApp's InitInstance. Without this, the call to SHAutoComplete will fail. You also need to call CoUninitialize(); in your CWinApp's ExitInstance. This initializes COM for your app.

I'd also recommend that you call FreeLibrary(hSHLWAPIDLL) in your CWinApp::ExitInstance(), just to be nice to the OS.

Now the good stuff

If you have an an edit control in your dialog class that's hooked up to a member variable, all you have to do is:

BOOL CMyDialog::OnInitDialog()
{
...
    // enable auto complete for m_myEdit

    SetupForAutoComplete(m_myEdit.m_hWnd, 
        SHACF_FILESYSTEM);
...
}

And that's it. Now, when you start typing a path, the edit control will drop down a list box with paths that match what you've typed so far - just like the file open dialog.

If you want to do this to a combo box, it's slightly more complicated:

BOOL CMyDialog::OnInitDialog()
{
...
    // the edit control of a CComboBox is always at dlg ID 1001

    CEdit * pMyEdit = (CEdit *)m_myCombo.GetDlgItem(1001);
    if (pMyEdit)
    {
        // enable auto complete for m_myCombo

        SetupForAutoComplete(pMyEdit->m_hWnd, SHACF_FILESYSTEM);
    }
...
}

And that's that.

Concerns

The heart of this whole thing is SHAutoComplete. This is a function that lives in Shlwapi.DLL and is only available on Win2K or on machines with IE 5.0 or higher installed. But, you can safely call my SetupForAutoComplete function on machines that don't meet those requirements because the function checks for the DLL and then for the particular function. If neither are there, the function leaves gracefully.

More info

SHAutoComplete can do more than file system auto-complete. It can also do URL auto-complete (like the navigation bar in IE), and it can do it all with various other fun options. See the MSDN for SHAutoComplete for a full description of what these options are.

And that's that. Enjoy responsibly. Oh yeah, thanks Michael Dunn, for your invaluable input :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chris Losinger


Supporter
Chris Losinger is the president of Smaller Animals Software, Inc..
Occupation: Web Developer
Location: United States United States

Other popular Edit Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 42 (Total in Forum: 42) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralI do just like that, but unuseful!!memberfdmakk16:44 2 Feb '05  
Generalcombo boxsussAnonymous22:46 10 Jul '04  
GeneralRe: combo boxmembertoxcct4:32 4 Dec '04  
Generalhow can enter key close the drop down box?sussDanny Neijns1281:28 5 Jul '04  
GeneralWith Platform SDKmemberAnakin_Skaywalker13:43 17 Jan '04  
GeneralHelp! Locks programmemberblindcop8:12 8 Jan '04  
GeneralRe: Help! Locks programmemberblindcop5:25 9 Jan '04  
GeneralVC++ using edit box and dialogsussHaider Chy21:59 5 Jan '03  
GeneralRe: VC++ using edit box and dialogsupporterChris Losinger15:54 6 Jan '03  
GeneralSHACF_FILESYSTEM ?memberme15:24 27 Mar '02  
GeneralRe: SHACF_FILESYSTEM ?supporterChris Losinger15:52 27 Mar '02  
Generalhow to make the Edit on the htmlpages has the auto-completion effect?memberbenben1:41 8 Mar '02  
GeneralHow to add strings in auto complete droplistmemberAnonymous4:25 1 Mar '02  
Generali was wondering how that worked...memberShog96:40 27 Feb '02  
GeneralRe: i was wondering how that worked...memberChris Losinger6:44 27 Feb '02  
GeneralProblemsmemberDerek Price3:14 27 Feb '02  
GeneralRe: ProblemsmemberChris Losinger4:05 27 Feb '02  
GeneralRe: ProblemsmemberChris Losinger4:06 27 Feb '02  
GeneralRe: ProblemsmemberDerek Price4:29 27 Feb '02  
GeneralRe: ProblemsmemberMagnus H4:07 27 Feb '02  
GeneralGreat stuff!memberJörgen Sigvardsson23:01 26 Feb '02  
GeneralMore optimizationmemberRavi Bhavnani3:31 27 Feb '02  
GeneralRe: More optimizationmemberJörgen Sigvardsson3:46 27 Feb '02  
GeneralRe: More optimizationmemberAnonymous4:46 27 Feb '02  
GeneralAlmost there...memberChris Losinger4:02 27 Feb '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Feb 2002
Editor: Nishant Sivakumar
Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project