2012-03-02 100 views
3

一個矩形我有這樣的代碼:GetWindowText函數在C#中返回,而不是文本

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矩形而不是此文本。我得到{慬潹瑵潃瑮潲ㅬ}(在視覺和記事本這個中國標誌顯示爲長方形。我有什麼毛病編碼?

感謝

回答

9

這些症狀表明您呼叫的ANSI版本的GetWindowText但解釋返回值爲Unicode。

的解決方案是確保你調用GetWindowText Unicode版本,而不是通過在DllImport

[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)] 
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, 
    int nMaxCount); 
指定 Charset=Charset.Unicode做到這一點
+0

非常感謝,它的工作 – Robert 2012-03-02 22:10:08

+0

哇,謝謝,這個問題讓我瘋狂。將CharSet從'CharSet.Auto'更改爲'CharSet.Unicode'取得了訣竅! – Rasive 2014-06-13 20:29:19