Click here to Skip to main content
15,886,362 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  
Favorites List or Hyper Links List Box
/*///////////////////////////////////////////////////////////////////

Copyright(c) 2000, Masoud Samimi - All Rights Reserved.

File:		FavorList.cpp - implementation file of favorite list box

History:	Created 14/12/2000 (dd/mm/yyyy)

Used for:	Loads all your favorites URLs from the 
			windows favorites directory "C:\windows\favorites"
			and adds/displays them inside the list box

Deriving:	This class can be used as a base class to your own 
			derived class inorder to have your own specialized
			AddString(you rown stuff) by just overriding one 
			virtual function the PreSubclassWindow() funtion.

Notes:		This class and the code is freely destributed, you
			cannot sell it. You can modify to fit your own
			style, but you will have to mention your changes and
			your name here in the changes log below!

			Whatever you do, please do not remove this info header
			from the files.
			

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Changes:	---->
By:			---->

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
	List of further enhancements:
	
	1. To add all the subdiretctory URLs as well.
	  
	2. To add a tree control! if possible, to have
	     the top level directories as expandable parents 
		 and the URLs files as children.
		 ( Something like the Windows Explorer! )
	3. Add your wishes! :)

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


//////////////////////////////////////////////////////////////////*/

#include "stdafx.h"
#include "MyFavourites.h"
#include "FavorList.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFavorList

CFavorList::CFavorList()
{

	backBrush.CreateSolidBrush(RGB(0,95,55));
	hCursor = AfxGetApp()->LoadStandardCursor(MAKEINTRESOURCE(32649));//IDC_HAND);
	
	hFind = NULL;

}

CFavorList::~CFavorList()
{
}


BEGIN_MESSAGE_MAP(CFavorList, CListBox)
	//{{AFX_MSG_MAP(CFavorList)
	ON_WM_CTLCOLOR_REFLECT()
	ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
	ON_WM_DESTROY()
	ON_WM_MOUSEMOVE()
	ON_CONTROL_REFLECT(LBN_DBLCLK, OnDblclk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFavorList message handlers

HBRUSH CFavorList::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	
	pDC->SetTextColor(RGB(205,205,0));
	pDC->SetBkMode(TRANSPARENT);

	return (HBRUSH) backBrush;

}


void CFavorList::OnSelchange() 
{

	SetCursor(hCursor);

	CString url = _T("");	
	int index = GetCurSel();
	GetText(index, url);
	//
	// Create and initialize a tooltip control.
	//
    m_ctlTT.Create (this);

	CRect rect;
	GetWindowRect(&rect);
	int url_length = url.GetLength(); 
	int width = rect.Width()/7+50;

	if(url_length > width){
		m_ctlTT.AddWindowTool(this,
				url.GetBuffer(1024));	
		}

	url.ReleaseBuffer();
	

}

void CFavorList::OnDestroy() 
{
	CListBox::OnDestroy();
	
	backBrush.DeleteObject();	// delete background brush
		
}

void CFavorList::OnMouseMove(UINT nFlags, CPoint point) 
{
	

	SetCursor(hCursor);
		
	CListBox::OnMouseMove(nFlags, point);

}

void CFavorList::OnDblclk() 
{

	CString url = _T("");	
	int index = GetCurSel();
	GetText(index, url);

	iReturn = (int) ShellExecute(NULL, "open", url, NULL, NULL, 0);

          // If ShellExecute returns an error code, let the user know.

     if (iReturn <= 32)
     {
          wsprintf (szBuffer, 
                    "Cannot launch browser and cannot continue! "
                    "(ShellExecute error code is %i)", iReturn) ;
          MessageBox (szBuffer, "Error!", MB_OK | MB_ICONEXCLAMATION) ;
     }

	 if(url.GetLength()==0){
		  wsprintf (szBuffer, 
                    "This is an empty entry! Try clicking a filled link! "
					"(ShellExecute error code is %i)", iReturn) ;

          MessageBox (szBuffer, "Error!", MB_OK | MB_ICONEXCLAMATION) ;
	}

 
	

	 url.ReleaseBuffer();
	 
}

void CFavorList::PreSubclassWindow() 
{

	GetFavoritesDirectory();
	LoadFiles();
	
	SetCurSel(0);


	CListBox::PreSubclassWindow();
}


int CFavorList::GetFavoritesDirectory()
{

	lReturn = RegOpenKeyEx(HKEY_CURRENT_USER,
		"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
		0, KEY_QUERY_VALUE, &hKey);

	if(lReturn != ERROR_SUCCESS)
		{
			MessageBox("Cannot obtain Favourites directory and cannot continue!",
				"Error!", MB_OK|MB_ICONEXCLAMATION);

			return 0;

		}


	dwLength = sizeof(szFavoritesDir);

	 lReturn = RegQueryValueEx (hKey, "Favorites", NULL,
                                &dwType, szFavoritesDir, &dwLength) ;


	if(lReturn != ERROR_SUCCESS)
		{
			MessageBox("Cannot obtain Favourites directory and cannot continue!",
				"Error!", MB_OK|MB_ICONEXCLAMATION);

			return 0;

		}

	return 0;

}


void CFavorList::LoadFiles()
{

	::SetCurrentDirectory((const char*) szFavoritesDir);
	
	hFind = ::FindFirstFile (_T ("*.url"), &fd);

	if (hFind != INVALID_HANDLE_VALUE) {
		do {
			
			if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			AddString(fd.cFileName);
			} while (::FindNextFile (hFind, &fd));
			::FindClose (hFind);

	}

}

void CFavorList::EnumerateFolders ()
{
    
	hFind = ::FindFirstFile (_T ("*.*"), &fd);

    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                CString name = fd.cFileName;
                if (name != _T (".") && name != _T ("..")) {
                    TRACE (_T ("%s\n"), fd.cFileName);
                    ::SetCurrentDirectory (fd.cFileName);
				    EnumerateFolders ();
                    ::SetCurrentDirectory (_T (".."));
					
                }
            }
        } while (::FindNextFile (hFind, &fd));
        ::FindClose (hFind);
    }

}


/////////////////////////////////////////////////////////////////////////////
// CMyToolTipCtrl

CMyToolTipCtrl::CMyToolTipCtrl()
{
}

CMyToolTipCtrl::~CMyToolTipCtrl()
{
}


BEGIN_MESSAGE_MAP(CMyToolTipCtrl, CToolTipCtrl)
	//{{AFX_MSG_MAP(CMyToolTipCtrl)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyToolTipCtrl message handlers

BOOL CMyToolTipCtrl::AddWindowTool(CWnd *pWnd, LPCTSTR pszText)
{
    TOOLINFO ti;
    ti.cbSize = sizeof (TOOLINFO);
    ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    ti.hwnd = pWnd->GetParent ()->GetSafeHwnd ();
    ti.uId = (UINT) pWnd->GetSafeHwnd ();
    ti.hinst = AfxGetInstanceHandle ();
    ti.lpszText = (LPTSTR) pszText;

    return (BOOL) SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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