Click here to Skip to main content
13,299,977 members (67,363 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 24 Jan 2001

FavorList control (A Hyperlink CListBox)

, 24 Jan 2001
Rate this:
Please Sign up or sign in to vote.
Favorites List or Hyper Links List Box
  • Download source files - 28 Kb

    Screen Shots

    Screen Shot 1

    1 - Screen shot of the default favorites URLs loaded.

    Screen Shot 2

    2 - Screen shot of another derived version.

    Screen Shot 3

    3 - Screen shot of the About dialog derived version.

    Introduction and Credits:

    This control was inspired by the Hyperlink Static controls developed by others like, Chris Maunder, Paul De Lascia and PJ Naughter (Thanks guys!). The implementation of favorites was also inspired by Charles Petzold's Favorites.EXE . I am using some of his code in my implementation, the exact lines are in the favorite directory opening routines. I further took the idea and put it into a CListBox derived version.

    Also, I wish to say a very special thanks to Erik Funkenbusch for his help and the fixing reply to my posted request on CP's The Lounge titled: CListBox!? Thank you Erik! As well to all the other replies! You are all good people. Thanks!

    One could ask why a hyperlink list box? Because sometimes you have more than one Hyperlink to add, and just adding many Static controls would be a mess. Also because there are often times when there are many people I would like to credit, list their web contacts, or add more than one link to of my own in desired places inside my applications.

    It works like a standard list box; by selecting and then double click a hyperlink list item you are taken to the URL! Have fun surfin! It also sports a tooltip when you select an item where the string length is longer than the width of the list control! :)

    It can be used in many different ways. A few examples are:

    • It could be used inside an About dialog (see the example graphic above) that would show all your web based contact details such as an Email or a personal website URL.
    • It can be used in the Credits dialog to show the credit details of people involved in the project.

    How to use it

    First add the header and source (.h & .cpp) files to your project. Then, in your dialog or any window that you produce with the resource editor in VC++ while using the ClassWizard (Ctrl+W), add an instance of your list control. In the source code manually change the declaration of the listbox to the desired class.


    CListBox   m_List;	// This is the default instance that the Classwizard adds


    CFavorList m_List;	// This is the instance of the control

    The above will automatically subclass the window to the desired class. If you look at the DDX lines you would see this:

    void CMyFavouritesDlg::DoDataExchange(CDataExchange* pDX)
    	DDX_Control(pDX, IDC_LIST_FAVORITES, m_List); 	
    The IDC_LIST_FAVORITES is mapped to the m_List which in turn is the CFavorList class instance! :)

    In the accompanied demo project, I have shown 3 different ways (See screen shots) of how to use this control. The first demo (Screen shot 1) and default ( without deriving and overriding ) is that it opens your computer's Favorites directory and loads all the URL files with the extension *.url into itself (ListBox). The second demo (Screen shot 2) is when you click the "Another..." button, you will view the list of links that I have put into the list with different colors by overriding the supplied virtual functions. The third demo (Screen shot 3) is inside the About dialog in which I am inserting my web contacts (email & site).

    Overridable Functions:

    virtual void PreSubclassWindow();
    virtual HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);

    To produce your own customized URL's list box, you can simply derive using the CFavorList class as a base and then override the above mentioned virtuals.

    Override to add your own strings:

    For example, if you want to add your own string data into the list, then you could override the derived CFavorList class' implementation of PreSubclassWindow() function like this:

    void Cxxxxxxx::PreSubclassWindow() 
    	InsertString(0,"Visit Website:");
    	InsertString(4,"Send Email:");

    Override to have your desired colors for text or background:

    For exmaple, if you want your list's Text color to be Black then you could do this:

    HBRUSH Cxxxxxxx::CtlColor(CDC* pDC, UINT nCtlColor)
    	pDC->SetTextColor(RGB(0,0,0));	// Black Text
    	return (HBRUSH) backBrush;

    And if you want the background color to be different, a Dark Green example, you'd have to just set the Back Brush inside the default constructor of your derived class like this:

    	backBrush.CreateSolidBrush(RGB(0,95,55)); // Dark Green

    Ideas to further enhance this control

    1. To add subdirectory level functionality. However, you may have noticed that I have put a function called EnumerateFolders() inside the code, it is there for later. It is not currently supported. But I am looking forward to fixing it. Any help is appreciated for sure!
    2. Add multi leveled Parent->Child relations, something like a Tree control. Parents would be the Directory names and then the URL Files as children, using tabs spaces to forward them. This would serve good in case the control is fully used for the Favorites Loading and Formatting!
    3. Any thing you come up with! :)

    Closing Remarks

    Suggestions, Corrections, Comments, and any ideas for enhancements are most welcome. You can post them here or email them to me if you wish.

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    About the Author

    Masoud Samimi
    Technical Lead Samimi Information Technology
    United Arab Emirates United Arab Emirates
    My company provides services in the fields of:

    LAN/WAN Networking
    Data Management and Security
    Data Recovery
    ERP/CRM Solutions
    IT Infrastructure Solutions
    Software/Hardware Sales/Service

    You may also be interested in...


    Comments and Discussions

    GeneralCrash Pin
    Brad Bruce23-Aug-02 8:25
    memberBrad Bruce23-Aug-02 8:25 
    GeneralRe: Crash Pin
    Anonymous25-Aug-02 3:31
    sussAnonymous25-Aug-02 3:31 
    GeneralRe: Crash Pin
    Brad Bruce25-Aug-02 4:44
    memberBrad Bruce25-Aug-02 4:44 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.171207.1 | Last Updated 25 Jan 2001
    Article Copyright 2001 by Masoud Samimi
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid