c# - c# 中的 GetWindowText 返回一个矩形而不是文本

标签 c# winapi pinvoke

我有这样的代码:

IntPtr hwndGetValue = new IntPtr(67904);
List<IntPtr> windows = GetChildWindows(hwndGetValue);

int textLength = GetWindowTextLength(windows[0]);
StringBuilder outText = new StringBuilder(textLength + 1);
int a = GetWindowText(windows[0], outText, outText.Capacity);

在 Windows 中我有 49 个指针。 Windows[0] 有我可以在 Spy++ 中看到的文本,但是方法 GetWindowText 返回 outText 矩形而不是这个文本。我得到 {慬潹瑵潃瑮潲ㅬ}(在 Visual 和记事本中,这个中文符号显示为矩形。我的编码有问题吗?

谢谢

最佳答案

这些症状表明您正在调用 ANSI 版本的 GetWindowText 但将返回值解释为 Unicode。

解决方案是确保调用 GetWindowText 的 Unicode 版本。通过在 DllImport 中指定 Charset=Charset.Unicode 来执行此操作。

[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
    int nMaxCount);

关于c# - c# 中的 GetWindowText 返回一个矩形而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9540895/

相关文章:

c# - 字符串格式不对

c++ - 计算C++中相同运行进程的总数

c# - 如何以编程方式使用 PIN 码登录 Windows

c# - 以编程方式查明系统是否支持待机

c# - 如何使用 XPathSelectElement 提取元素

c# - 如何从 C# 中的字符串中提取多个子字符串匹配项?

c++ - 只有 wWinMain MSVC 2019 的链接器错误无法解析的外部符号

winapi - 如何使用 P/Invoke 在 PowerShell 1.0 中调用 Win32 函数?

c# - 重复事件——使用 DateTime?

c++ - 关于windows下文件操作的问题