我有这样的代码:
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/