【发布时间】: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& searchString以避免不必要的复制。 -
奇怪,您在这里调用了
std::string,甚至没有提到按值传递的 array。一个TCHAR数组,所有的东西。如果以正确性为目标,这就是您想要解决的代码类型。 -
@IInspectable - 我对 TCHAR 的讨伐是 well documented 在这个板上。
-
这段代码中完全不需要
const_cast