GetWindowText

GetWindowText

Windows API宏,在WinUser.h中根據是否已定義Unicode被分別定義為GetWindowTextW和GetWindowTextA。該函式將指定視窗的標題條文本(如果存在)拷貝到一個快取區內。如果指定的視窗是一個控制項,則拷貝控制項的文本。但是,GetWindowText可能無法獲取外部應用程式中控制項的文本,獲取自繪的控制項或者是外部的密碼編輯框很有可能會失敗。

基本介紹

  • 中文名:獲取視窗文本
  • 外文名:GetWindowText
  • 返回值類型:int
  • 參數類型:(HWND,LPTSTR,int)
  • 聲明位置:WinUser.h
函式功能,參數,返回值,備註,函式定義,Parameters參數,Return Value返回值,Remarks,

函式功能

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

參數

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

返回值

如果函式成功,返回值是拷貝的字元串的字元個數,不包括中斷的空字元;如果視窗無標題欄或文本,或標題欄為空,或視窗或控制的句柄無效,則返回值為零。若想獲得更多錯誤信息,請調用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中就保存了 獲取到的字元

相關詞條

熱門詞條

聯絡我們