【问题标题】:How to Highlight a string the was found using Windows C++如何突出显示使用 Windows C++ 找到的字符串
【发布时间】:2021-12-15 15:57:15
【问题描述】:

我在我的 Windows C++ 程序中创建了以下搜索功能

void SearchText(std::string searchString, TCHAR text[_MAX_PATH], std::string searchFor)
{
    std::size_t found;

    searchForx = const_cast<char*>(searchFor.c_str());

    found = searchString.find(searchFor);
    if (found != std::string::npos) {
        MessageBoxA(NULL, searchForx, "string found", MB_OK);
    }
    if (found == std::string::npos) {
        MessageBoxA(NULL, searchForx, "no string found", MB_OK);
    }
}

请帮我突出显示它找到的文本。 我知道它会将找到的文本的位置返回为 size_t found。但是怎么用

hDc = GetDC(hWnd);
SetBkMode (hDC, OPAGUE);
SetBkColor (hDC, RGB (0,255,0));
SetTextColor (hDC, RGB(255,0,255));
TextOut (hDC,10,10, cBuf, strLen (cBuf));
ReleaseDC(hWnd, hDC);

在 Text out 中,它要求 X 和 Y 坐标,但搜索函数只返回 X,即返回字符串的第一个起始字符的位置。

谢谢

【问题讨论】:

  • 无关,但这里有一个专业提示。将您的字符串传递为 const std::string&amp; searchString 以避免不必要的复制。
  • 奇怪,您在这里调用了std::string,甚至没有提到按值传递的 array。一个TCHAR 数组,所有的东西。如果以正确性为目标,这就是您想要解决的代码类型。
  • @IInspectable - 我对 TCHAR 的讨伐是 well documented 在这个板上。
  • 这段代码中完全不需要const_cast

标签: c++ winapi


【解决方案1】:

您可以在绘制字符串之前使用DrawTextA 和DT_CALCRECT 标志来计算字符串的渲染大小。然后,您可以将这些坐标转换应用到您的 TextOut 函数调用。

下面是一个示例 WM_PAINT 处理程序,它在字符串中搜索特定子字符串并将找到的文本涂成红色。

例子:

case WM_PAINT:
    {
        std::string str = "The quick brown fox jumped over the red dog";
        std::string toFind = "red";
        std::string pre = str;
        std::string found;
        std::string post;

        size_t pos = str.find(toFind, 0);

        if (pos != std::string::npos)
        {
            pre = str.substr(0, pos);
            found = str.substr(pos, toFind.size());
            post = str.substr(pos + toFind.size());
        }

        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        int x = 10;
        int y = 10;

        // calculate the sizes of each sub string
        RECT rectPre = {};
        RECT rectFound = {};
        DrawTextA(hdc, pre.c_str(), -1, &rectPre, DT_CALCRECT);
        DrawTextA(hdc, found.c_str(), -1, &rectFound, DT_CALCRECT);

        // draw everything before the found text
        TextOutA(hdc, x, y, pre.c_str(), pre.size());
        x += rectPre.right;

        // draw the highlighted text
        auto oldColor = SetTextColor(hdc, RGB(200, 0, 0));
        TextOutA(hdc, x, y, found.c_str(), found.size());
        x += rectFound.right;
        SetTextColor(hdc, oldColor);

        // draw the remaining text after the highlighted portion
        TextOutA(hdc, x, y, post.c_str(), post.size());

        EndPaint(hWnd, &ps);
    }
    break;

结果:

【讨论】:

  • 谢谢,非常感谢您提供的代码。我正在学习 Windows,但还有一些路要走。接触到了“Windows 95 -Charles Petzold 和 Windows API Bible-James Conger”。于是我开始了我的旅程。我实际上得到了我的打开文件、搜索文件、显示弹出窗格和编辑工作到一定程度。但试图突出发现的单词让我望而却步。我正在创建什么样的应用程序仍然悬而未决。很高兴我能做到这一点。 :)
  • @LoneWarriorForgottenTrails - 这些是经典 Win32 编程的好书,今天仍然具有一定的相关性。但您可能需要考虑使用更现代的 UX 框架(例如 UWP 和/或 XAML)来构建适用于 Windows 10 及更高版本的现代 Windows 应用程序。
猜你喜欢
  • 2011-05-22
  • 2012-12-08
  • 2013-12-17
  • 1970-01-01
  • 2022-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
相关资源
最近更新 更多