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

Favorites MultiView

Rate me:
Please Sign up or sign in to vote.
2.63/5 (5 votes)
28 Dec 20023 min read 107.4K   1.9K   32   16
Favorites in multiple Views.

Sample Image

What's New:

On request from Swinefeaster, I added an image and a demo version. A completely updated version allowing selection of Favorites is under testing.

Introduction

This article demonstrates the technique of dynamically creating multiple Views at run-time and opening web-pages on the Views created. The number of Views created depends on the list of web-pages saved at the root of the Favorites folder in your Internet Explorer.

Sometime in 1997, I had downloaded a shareware software (don't remember it's name). The software had 8 Views in which you could open sites. At that time, I was a Visual Basic developer and used to be fascinated by these software packages which were probably made using C++ (an alien language for me then). But now that I know a little about VC++, I thought why not try something of this sort and then if possible share the code with the Internet community because of which I have learnt a lot over the years. As regards to multiple Views, I searched all over the MSDN CD (in VS6) and in frustration finally posted my question to a site named ..... and after 15 days got a reply from them before which I had managed to get what I wanted by trial and error methods.

I developed this project after seeing a similar project on some other site (???) which had 6 Views (3 columns and 2 rows) and surprisingly there were 6 sub-classes of CHTMLView, one for each View.

Features

  • Retrieve all Favorites sites
  • Stop retrieval of individual sites
  • Stop retrieval of all sites
  • View individual sites in full-screen

How to Use: (the Application)

  1. Build the Project
  2. Exit Visual Studio
  3. Log-on to the Internet
  4. Go to your favorite sites
  5. From the Internet Explorer menu option, select Favourites->Add (do not click on the Create in >> option). Save by clicking OK.
  6. Repeat steps 4-5 till you saved all you favorite sites
  7. Close Internet Explorer (do not log-off the Internet)
To use when Logged-On to the Internet
  • Start FavMV application from the project's Debug folder
  • From the menu, select Favorites->Retreive
  • To view individual sites in full-screen, first click on the site you want to view in full-screen and then from the menu, select View->Full-Screen.
  • To open a site not in your favorites, select File->Open from the menu
To Use when Logged-Off from the Internet
  • Start Internet Explorer
  • From the menu option, select File->Work Offline
  • Minimize Internet Explorer (do not close Internet Explorer)
  • Start FavMV application from the project's Debug folder
  • From the menu, select Favourites->Retreive
  • To view individual sites in full-screen, first click on the site you want to view in full-screen and then from the menu, select View->Full-Screen.

About the Code

I have embedded CWebBrowser2 ActiveX control inside the CHTMLView sub-class which is of type CView and not CHtmlView, because I wanted to display the name of each site on the bar inside each View. I could have used CHtmlView instead.

//
// To start, we get the location of Favourites folder
// on the system from the Registry in this way
//
BOOL CMainFrame::getFavouriteskey(TCHAR szPath[MAX_PATH])
{
    TCHAR           sz[MAX_PATH];
    HKEY            hKey;
    DWORD           dwSize;

    if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\
        CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
    {
        TRACE0("Favorites folder not found\n");
        return FALSE;
    }
    dwSize = sizeof(sz);
    RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
    ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
    RegCloseKey(hKey);
}
//
// Using the returned folder path above
// we find the count of the no. of sites 
// on the Favourites menu option in this way
//
// Note : The that the file-wide Character Array
// if filled in this member function for later
// use in the process of retreiving the sites
//
int CMainFrame::GetFavorites(LPCTSTR pszPath, int nStartPos , 
    CStringArray* caFavourites)
{

    #define INTERNET_MAX_PATH_LENGTH 256

    int nCount = 0;
    CString         strPath(pszPath);
    CString         strPath2;
    CString         str;
    WIN32_FIND_DATA wfd;
    HANDLE          h;
    int             nPos;
    int             nEndPos;
    TCHAR           buf[INTERNET_MAX_PATH_LENGTH];
    CStringArray    astrFavorites;
    CStringArray    astrDirs;

    // make sure there's a trailing backslash
    if(strPath[strPath.GetLength() - 1] != _T('\\'))
        strPath += _T('\\');
    strPath2 = strPath;
    strPath += "*.*";

    // now scan the directory, first for
    // .URL files and then for subdirectories
    // that may also contain .URL files

    h = FindFirstFile(strPath, &wfd);
    if(h != INVALID_HANDLE_VALUE)
    {
        nEndPos = nStartPos;
        do
        {
            if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|
                        FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
            {
                str = wfd.cFileName;
                if(str.Right(4) == _T(".url"))
                {

                    // an .URL file is formatted just like an
                    // .INI file, so we can
                    // use GetPrivateProfileString() to get
                    // the information we want
                    ::GetPrivateProfileString(_T("InternetShortcut"), 
                        _T("URL"), _T(""), buf, 
                        INTERNET_MAX_PATH_LENGTH,
                        strPath2 + str);

                    caFavourites->Add(buf);
                    str = str.Mid(0, str.GetLength()-4);    
                    str = str.Left(str.GetLength() - 4);

                    // scan through the array and perform an insertion sort
                    // to make sure the menu ends up in alphabetic order
                    for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
                    {
                        if(str.CompareNoCase(astrFavorites[nPos]) < 0)
                            break;
                    }
                    astrFavorites.InsertAt(nPos, str);
                    ++nEndPos;
                    nCount++;
                }
            }
        } while(FindNextFile(h, &wfd));
        FindClose(h);
    }

    return nCount;
}
//
// We now create Views depending on the no. of sites
// as returned above, in this way 
//
void CMainFrame::CreateViews(int nRows, int nCols, CCreateContext* pContext)
{
    int i ,j;
    int iIndex = 0;
    m_Splitter.CreateStatic(this, nRows,nCols);
    for( i = 0 ; i <= nRows-1;i++)
        for( j = 0; j <= nCols-1 ; j++)
            m_Splitter.CreateView(i,j, RUNTIME_CLASS(CMyHTMLView), 
                        CSize(0,0), pContext);

}

Having created the View, we wait for the user to click on the menu option "Retrieve".

//
// we now retreive each of the sites 
// into the View using the character array
// filled-in in the member function GetFavorites above
//
void CMainFrame::OnRetrieve() 
{
    int i,j;
    int nCols = m_Splitter.GetColumnCount();
    int nRows = m_Splitter.GetRowCount();
    int iIndex = 0;
    for( i = 0 ; i <= nRows-1;i++)
    {
        for( j = 0; j <= nCols-1 ; j++)
        {
            CString url = caFavourites.GetAt(iIndex);
            CString siteName = url.Mid(url.Find("//", 0)+2);
            siteName = siteName.Left(siteName.Find("/",0));
            m_wndStatusBar.SetPaneText(0, siteName, TRUE);
            CMyHTMLView* cv = (CMyHTMLView*)m_Splitter.GetPane(i,j);
            // the URL is passed here to the respective
            // sub-class view CMyHTMLView to open
            // the site
            cv->NavigateTheURL(url);
            cv->bURLRetreived = TRUE;
            iIndex++;
            if(bStopAll)
                break;
        }
        if(bStopAll)
            break;

    }

    CRect rect;
    GetClientRect(&rect);
    rect.right += 10;
    rect.bottom += 10;
    MoveWindow(rect, TRUE);
    m_wndStatusBar.SetPaneText(0, "", TRUE);
    bURLRetreived=TRUE;
}
//
// The method for displaying sites is quite simple
// as can be seen below in the sub-class
// CHTMLView (all the above is in the Mainframe
// CMainFrame)
//
void CMyHTMLView::NavigateTheURL(CString url)
{

    CString siteName = url.Mid(url.Find("//", 0)+2);
    siteName = siteName.Left(siteName.Find("/",0));

    m_SiteNameBar.SetWindowText(siteName);
    // below if the line for retreiving the site
    m_WebBrowser.Navigate(url, NULL, NULL, NULL, NULL);
    bURLRetreived = TRUE;
}

You can also view individual sites in full-screen, by first setting focus to the site you want to view in full-screen and then from the menu select View->Full-Screen.

Happy Surfing !!!!

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


Written By
India India
Nothing to boast about

Comments and Discussions

 
GeneralMultiview movie Pin
mehmetned7-Nov-06 10:01
mehmetned7-Nov-06 10:01 
GeneralRe: Multiview movie Pin
Barretto VN7-Nov-06 23:16
Barretto VN7-Nov-06 23:16 
Generaldebug assertion failed Pin
hesterloli31-Jul-03 4:27
hesterloli31-Jul-03 4:27 
GeneralRe: debug assertion failed Pin
Barretto VN7-Aug-03 2:34
Barretto VN7-Aug-03 2:34 
GeneralAssert when compiled and run Pin
CodeProjectSQ29-Dec-02 12:24
CodeProjectSQ29-Dec-02 12:24 
GeneralRe: Assert when compiled and run Pin
Barretto VN3-Jan-03 2:17
Barretto VN3-Jan-03 2:17 
GeneralRe: Assert when compiled and run Pin
Barretto VN18-Jan-03 23:09
Barretto VN18-Jan-03 23:09 
GeneralImage didn't come up Pin
Marc Clifton29-Dec-02 4:44
mvaMarc Clifton29-Dec-02 4:44 
GeneralRe: Image didn't come up Pin
Ravi Bhavnani29-Dec-02 6:27
professionalRavi Bhavnani29-Dec-02 6:27 
QuestionNo Binary? Pin
Swinefeaster19-Aug-02 10:55
Swinefeaster19-Aug-02 10:55 
GeneralGood Job!!!! Pin
msousa14-Aug-02 8:33
msousa14-Aug-02 8:33 
GeneralResolving shortcuts Pin
Anonymous14-Aug-02 6:00
Anonymous14-Aug-02 6:00 
GeneralFavorites folder Pin
Anonymous14-Aug-02 5:59
Anonymous14-Aug-02 5:59 
GeneralNow THATS an article!! Pin
Andreas Saurwein14-Aug-02 2:42
Andreas Saurwein14-Aug-02 2:42 
GeneralRe: Now THATS an article!! Pin
John Simpsons14-Aug-02 4:24
sussJohn Simpsons14-Aug-02 4:24 
GeneralRe: Now THATS an article!! Pin
Andreas Saurwein30-Dec-02 5:50
Andreas Saurwein30-Dec-02 5:50 

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.