複製鏈接
請複製以下鏈接發送給好友

GetWindowText

鎖定
Windows API宏,在WinUser.h中根據是否已定義Unicode被分別定義為GetWindowTextW和GetWindowTextA。該函數將指定窗口的標題條文本(如果存在)拷貝到一個緩存區內。如果指定的窗口是一個控件,則拷貝控件的文本。但是,GetWindowText可能無法獲取外部應用程序中控件的文本,獲取自繪的控件或者是外部的密碼編輯框很有可能會失敗。
中文名
獲取窗口文本
外文名
GetWindowText
返回值類型
int
參數類型
(HWND,LPTSTR,int)
聲明位置
WinUser.h

GetWindowText函數功能

該函數將指定窗口的標題條文本(如果存在)拷貝到一個緩存區內。如果指定的窗口是一個控件,則拷貝控件的文本。但是,GetWindowText可能無法獲取外部應用程序中控件的文本,獲取自繪的控件或者是外部的密碼編輯框很有可能會失敗。
函數原型:Int GetWindowText(HWND hWnd,LPTSTR lpString,Int nMaxCount);

GetWindowText參數

hWnd:帶文本的窗口或控件的句柄
lpString:指向接收文本的緩衝區的指針
nMaxCount:指定要保存在緩衝區內的字符的最大個數,其中包含NULL字符。如果文本超過界限,它就被截斷。
getwindowtext的DELPHI例子:
var
p:pchar;
begin
getmem(p,255);
getwindowtext(application.Handle,p,255);
showmessage(strpas(p));
freemem(p);
end;

GetWindowText返回值

如果函數成功,返回值是拷貝字符串的字符個數,不包括中斷的空字符;如果窗口無標題欄或文本,或標題欄為空,或窗口或控制的句柄無效,則返回值為零。若想獲得更多錯誤信息,請調用GetLastError函數。
函數不能返回在其他應用程序中的編輯控件的文本。
函數定義
如果目標窗口屬於當前進程,GetWindowText函數給指定的窗口或控件發送WM_GETTEXT消息。如果目標窗口屬於其他進程,並且有一個窗口標題,則GetWindowText返回窗口的標題文本,如果窗口無標題,則函數返回空字符串。
速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本;頭文件:Winuser.h;庫文件:user32.lib:Unicode:在Windows NT上實現為Unicode和ANSI兩種版本。
int GetWindowText(LPTSTR lpszStringBuf,int nMaxCount) const;
void GetWindowText(CString& rString) const;
Parameters參數
lpszStringBuf
[out] Pointer to the buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a NULL character.
nMaxCount
[in] Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated.
rString
A CString object that is to receive the copied string of the window's title.
Return Value返回值
Specifies the length, in characters, of the copied string, not including the terminating null character. It is 0 if CWnd has no caption or if the caption is empty.
Remarks
If the CWnd object is a control, the GetWindowText member function copies the text within the control instead of copying the caption.
This member function causes the WM_GETTEXT message to be sent to the CWnd object.
簡單使用:
定義CString str ;
GetDlgItem(控件ID)->GetWindowText(str);
這樣str中就保存了 獲取到的字符