65.9K
CodeProject 正在变化。 阅读更多。
Home

查找单词

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.55/5 (5投票s)

2006年1月3日

2分钟阅读

viewsIcon

60277

downloadIcon

864

Windows 查找工具很好用,但有时你想要更多功能。这个程序/工具正好满足了你的需求。

引言

Windows 查找工具很好用,但有时你想要更多功能。这个程序/工具正好满足了你的需求。

背景

在开发新项目时,你有时会想知道编译器预定义宏的值。你可以通过两种方式使用这个工具

  1. 设置文件夹、字符串、文件类型,然后点击“搜索”。结果将显示在列表框中。现在你可以双击一个项目,相应的 Windows 程序将被调用(notepad.exeMSVS_IDE)。
  2. 设置文件夹、字符串、文件类型,然后点击“搜索”。关闭此程序,并在 notepad.exe 中打开 Log.txt 文件,你可以在其中搜索“#define”。

使用代码

可以使用类 CResizeDlg 调整窗体(对话框)的大小,详情请参阅下面的“鸣谢”。我在该网站上找到了这个类。要使用该类,你必须将对话框属性更改为如图所示的调整大小方式。所用方法的原型是 AddControl(rect.left, rect.right, rect.top, rect.bottom, bFlickerFree);。在清单中定义了 XP 样式时,不能在按钮上使用 FlickerFree。

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);
...
...
...

我向 CResizeDlg 类添加了一个函数,用于调整列表控件的第一个字段的大小,并保持第二个字段不变

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);
}

文件比较函数

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;
}

将所有找到的项目记录到 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();
}

关注点

当将项目从 VC++ 6.0 转换为 VC++ 8.0 并且已经具有 XP 样式的清单资源时,VC++ 8.0 编译器将显示错误:IDR_MANIFEST 已经定义。为了避免这种情况:在菜单中打开“项目”选项卡,选择“属性”或使用快捷键 Alt+F7,展开“配置属性”,展开“清单工具”,选择“输入和输出”,点击“嵌入清单”并将其设置为“否”。编译器现在将使用你的清单,并在构建时将其复制到 Release 文件夹中。

致谢

  • CResizeDlg 类 - Robert Python。

历史

  • 2005 年 12 月 30 日 - 版本 1.0。
  • 2006 年 1 月 5 日 - 版本 1.0。
© . All rights reserved.