Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

Find Word

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
6 Jan 20062 min read 58.8K   862   23   11
Windows Find is a nice tool but sometimes you want a little more. This program/tool does just that.

Image 1

Image 2

Introduction

Windows Find is a nice tool but sometimes you want a little more. This program/tool does just that.

Background

Sometimes while developing a new project you would like to know the value of the compiler's predefined macros. You can use this tool in two ways:

  1. Setup the folder, string, file type, and click Search. The result is displayed in the list box. You can now double click an item and the appropriate Windows program will be called (notepad.exe or MSVS_IDE).
  2. Setup the folder, string, file type, and click Search. Close this program and open the Log.txt file in notepad.exe where you can search for "#define".

Using the code

The form (dialog) can be resized using the class CResizeDlg, see Credits below. I found this class at this site. To use the class, you must change your Dialog Properties to resize as shown in the above image. The prototype for the method used is AddControl(rect.left, rect.right, rect.top, rect.bottom, bFlickerFree);. FlickerFree can't be used on buttons when XP Style is defined in a manifest.

BOOL CFindWordDlg::OnInitDialog()
...
...
...
    // Buttons
    AddControl(IDOK, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 0);
    AddControl(IDCANCEL, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 0);
    AddControl(IDC_BROWSE, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 0);
    AddControl(IDC_SEARCH, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 0);

    // Edit Controls
    AddControl(IDC_EDIT1, CST_RESIZE, CST_RESIZE, 
                            CST_NONE, CST_NONE, 1);
    AddControl(IDC_EDIT2, CST_RESIZE, CST_RESIZE, 
                            CST_NONE, CST_NONE, 1);

    // Check Box
    AddControl(IDC_NO_CASE, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 1);
    AddControl(IDC_LOG_FILE, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 1);

    // List Box
    AddControl(IDC_LIST2, CST_RESIZE, CST_RESIZE, 
                          CST_NONE, CST_RESIZE, 1);

    // Combo Box
    AddControl(IDC_COMBO1, CST_REPOS, CST_REPOS, 
                            CST_NONE, CST_NONE, 1);

    // Static Control
    AddControl(IDC_STATIC_SEARCH, CST_NONE, 
                 CST_NONE, CST_NONE, CST_REPOS, 1);
    AddControl(IDC_SEARCH_STRING, CST_NONE, 
                 CST_NONE, CST_NONE, CST_NONE, 1);
    AddControl(IDC_SELECT_EXT, CST_REPOS, 
                 CST_REPOS, CST_NONE, CST_NONE, 1);
...
...
...

I added a function to the CResizeDlg class to resize the first field of the List control and keep the second field constant:

void CResizeDlg::ResizeListCtrl()
{
    CHeaderCtrl *pHdrCtrl = pgHeaderCtrl;
    HDITEM        hdi;
    CRect        rc;
    CRect        rc1;
    CRect        rc2;
    int        x, x1, x2;

    // Reposition the header control so that it is placed at
    // the top of its parent window's client area.
    pHdrCtrl->GetParent()->GetClientRect(&rc);
    x = rc.Width() - 21;    // header border
    pHdrCtrl->GetItemRect(0, &rc1);
    pHdrCtrl->GetItemRect(1, &rc2);
    x1 = rc1.Width();
    x2 = rc2.Width();
    rc1.right = x - x2;

    hdi.mask = HDI_WIDTH;
    pHdrCtrl->GetItem(0, &hdi);
    hdi.cxy = rc1.Width();
    pHdrCtrl->SetItem(0, &hdi);
}

The file compare function:

BOOL CFindWordDlg::CompareFileDate(CString szFile)
{
    BOOL bRes = TRUE;
    CString szStr;
    CString szStrFormat;
    int len = 0;
    int line = 0;
    TRY
    {
        CStdioFile fd(szFile, CFile::modeRead);
        len = m_strFile.GetLength();
        szFile.Delete(0, len);
        GetDlgItem(IDC_STATIC_SEARCH)->SetWindowText(szFile);
        while (fd.ReadString(szStr))
        {
            line++;
            if (bNoCase)
                szStr.MakeLower();
            if (szStr.Find(m_strSearch, 0) > 0)
            {
                nCount++;
                m_nLineNumber = line;
                szStrFormat.Format("File: %s", szFile);
                AddItem(szStrFormat);
                m_szaFiles.Add(szFullPath);
                Sleep(1);
                nCount++;
                szStrFormat.Format("Text: %s", szStr);
                szStrFormat.Replace("\t", "        ");
                AddItem(szStrFormat);
                szStrFormat.Format("Ln# %.4d %s", line, szStr);
                m_szaFiles.Add(szStrFormat);
            }
        }
        fd.Close();
    }
    CATCH(CFileException, pEx)
    {
        if (pEx->m_cause == CFileException::sharingViolation)
        {
            bRes = FALSE;
        }
        else
        {
            pEx->ReportError();
            bRes = FALSE;
        }
    }
    CATCH_ALL(e)
    {
        e->ReportError();
        bRes = FALSE;
    }
    END_CATCH_ALL
    return bRes;
}

Logs all the found items to log.txt:

void CFindWordDlg::LogFiles()
{
    CStdioFile fd;
    CString szFile;
    
    fd.Open("Log.txt", CFile::modeCreate | CFile::modeWrite);
    if(!fd)
    {
        AfxMessageBox("Couldn't open output file!");
            return;
    }
    for(int index = 0; index < m_szaFiles.GetSize(); index++)
    {
        szFile = m_szaFiles.GetAt(index);
        szFile += "\n";
        fd.WriteString(szFile);
    }
    fd.Close();
}

Points of interest

When you convert a project from VC++ 6.0 to VC++ 8.0 that already has a manifest resource for XP-style, the VC++ 8.0 compiler will show the error: IDR_MANIFEST already defined. To avoid this: open the Project tab in the menu, select Properties or use the hot key Alt+F7, expand Configuration Properties, expand Manifest Tool, select Input and Output, click Embed Manifest and set it to No. The compiler will now use your manifest and copy it to the Release folder when built.

Credits

  • CResizeDlg class - Robert Python.

History

  • 30th December, 2005 - Version 1.0.
  • 5th January, 2006 - Version 1.0.

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldouble click doesn't work properly Pin
avnersimon11-Jan-06 1:17
avnersimon11-Jan-06 1:17 
GeneralRe: double click doesn't work properly Pin
Roger6511-Jan-06 1:27
Roger6511-Jan-06 1:27 
GeneralNice idea... Pin
PJ Arends3-Jan-06 9:31
professionalPJ Arends3-Jan-06 9:31 
GeneralRe: Nice idea... Pin
Darren Schroeder3-Jan-06 13:18
Darren Schroeder3-Jan-06 13:18 
GeneralRe: Nice idea... Pin
Kochise3-Jan-06 21:28
Kochise3-Jan-06 21:28 
GeneralClickety Pin
PJ Arends4-Jan-06 16:51
professionalPJ Arends4-Jan-06 16:51 
GeneralRe: Nice idea... Pin
Roger656-Jan-06 3:59
Roger656-Jan-06 3:59 
GeneralRe: Nice idea... Pin
Rage9-Jan-06 6:14
professionalRage9-Jan-06 6:14 
GeneralRe: Nice idea... Pin
Roger659-Jan-06 7:49
Roger659-Jan-06 7:49 
GeneralRe: Nice idea... Pin
Simon Capewell10-Jan-06 3:42
Simon Capewell10-Jan-06 3:42 
GeneralRe: Nice idea... Pin
Roger6510-Jan-06 4:02
Roger6510-Jan-06 4:02 

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.