0

我真的不明白这些来自网络的例子。它们都是零碎的。没有一个简单简洁的示例如何制作经典的查找文本对话框。

我将我所知道的放入其中,但没有显示任何窗口并返回:2147500037 0x80004005

#include <windows.h>
#include <iostream>
#include <iomanip>

int main() {
  using namespace std;
  UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 
  uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);
  wstring search_str = L"text to search";
  HWND findDialog = NULL;
  wchar_t szFindWhat[MAX_PATH];
  FINDREPLACEW fr;
  ZeroMemory( & fr, sizeof( FINDREPLACEW ) );
  fr.lStructSize = sizeof( FINDREPLACEW );
  fr.hwndOwner = NULL;
  fr.lpstrFindWhat = szFindWhat;
  fr.wFindWhatLen = MAX_PATH;
  findDialog = FindTextW(&fr);
  cout << GetLastError() << endl;
  cout << hex << GetLastError() << endl;
}

您能否为我提供有效的代码,以便我可以从中构建。

4

1 回答 1

1

您没有检查来自FindTextW. 也就是说,你有:

findDialog = FindTextW(&fr);
cout << GetLastError() << endl;

如果函数成功,则结果是句柄。如果函数失败,则返回值为 NULL。

根据文档

如果函数失败,则返回值为 NULL。要获取扩展错误信息,请调用 CommDlgExtendedError 函数。CommDlgExtendedError 可能会返回以下错误代码之一:

换句话说,GetLastError不会告诉你任何有意义的事情。CommDlgExtendedError 在检查句柄后调用以查看它是否为 NULL。

于 2013-03-14T18:15:11.197 回答