65.9K
CodeProject is changing. Read more.
Home

Find Word

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.55/5 (5 votes)

Jan 3, 2006

2 min read

viewsIcon

59862

downloadIcon

864

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

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.