我正在使用这个:通过其句柄获取窗口的标题:
[DllImport("user32.dll")] private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
StringBuilder title = new StringBuilder(256);
GetWindowText(hWnd, title, 256);
如果标题包含希伯来语字符,则会用问号替换它们。
我猜这个问题与 econding 或其他什么有关......我该如何解决它?
您的问题包含一个小错误,该错误可能不会经常发生。您假设标题的最大长度为 256 个字符,这对于大多数情况来说可能就足够了。但正如这个post https://stackoverflow.com/questions/35135543/what-is-the-maximum-length-of-a-window-title-passed-to-setwindowtext?rq=1显示,长度可能为 100K 个字符,甚至更多。所以我会使用另一个辅助函数:GetWindowTextLength
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
public static string GetWindowTitle(IntPtr hWnd)
{
var length = GetWindowTextLength(hWnd) + 1;
var title = new StringBuilder(length);
GetWindowText(hWnd, title, length);
return title.ToString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)