Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / MFC

FavorList control (A Hyperlink CListBox)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Jan 2001CPOL 93.3K   302   27   5
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.

    Exmaple:

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

    To

    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)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CMyFavouritesDlg)
    	DDX_Control(pDX, IDC_LIST_FAVORITES, m_List); 	
    	//}}AFX_DATA_MAP
    }
    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(1,"");
    	InsertString(2,"www.yourwebsite.com");
    	InsertString(3,"");
    	InsertString(4,"Send Email:");
    	InsertString(5,"");
    	InsertString(6,"mailto: youremail@anymail.com");
    	
    	SetCurSel(0);
    
    	CListBox::PreSubclassWindow();
    }

    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
    	pDC->SetBkMode(TRANSPARENT);
    
    	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:

    Cxxxxxxx::CAboutURL()
    {
    	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)


    Written By
    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

    Comments and Discussions

     
    GeneralChoose RGB color for text Pin
    NightBlade28-Sep-02 10:05
    NightBlade28-Sep-02 10:05 

    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.