关于GetWindowText函数,高手请进~

bobob 2003-01-04 04:51:50
GetWindowText只能得到自己应用程序的标题~~
如何得到其他程序的标题?(已经得到了句柄)
查遍了api都找不到~~
高手赐教!!
...全文
1227 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
suntt 2003-01-05
  • 打赏
  • 举报
回复
Dim a As String
a = Space(256)
Call GetWindowText(Form1.hwnd, a, 256)
Text1.Text = a

获得自己的窗口
suntt 2003-01-05
  • 打赏
  • 举报
回复
GetWindowText

VB声明
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
说明
取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性)
返回值
Long,复制到lpString的字串长度;不包括空中止字符。会设置GetLastError
参数表
参数 类型及说明
hwnd Long,欲获取文字的那个窗口的句柄
lpString String,预定义的一个缓冲区,至少有cch+1个字符大小;随同窗口文字载入
cch Long,lpString缓冲区的长度
注解
不能用它从另一个应用程序的编辑控件中获取文字

chanet 2003-01-04
  • 打赏
  • 举报
回复
主要是句柄!
建议与Findwindow一齐用!
zyl910 2003-01-04
  • 打赏
  • 举报
回复
Public Declare Function GetWindowTextA Lib "user32" (ByVal hWnd As Long, lpString As Any, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long


Public Function GetText(ByVal hWnd As Long) As String
Dim TextStr() As Byte
Dim TextLen As Long

TextLen = GetWindowTextLength(hWnd)
'Debug.Print TextLen
If TextLen > 0 Then
ReDim TextStr(0 To TextLen)

TextLen = GetWindowTextA(hWnd, TextStr(0), TextLen + 1)
If TextLen > 0 Then
GetText = StrConv(LeftB(TextStr, TextLen), vbUnicode)

End If

End If

End Function
bobob 2003-01-04
  • 打赏
  • 举报
回复
我试过了,返回值总是0?怎么会事?
资料上是这样说的:“但是,GetWindowTeXt不能接收在其他应用程序中的控制文本。”
应该怎么理解?
feixuehenshui 2003-01-04
  • 打赏
  • 举报
回复
呵呵!~~
学习

microyzy 2003-01-04
  • 打赏
  • 举报
回复
GetWindowText可以得到其他进程的窗口的标题
能得到句柄应该可以得到标题的,你检查函数返回值了吗?

msdn:
GetWindowText
The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

int GetWindowText(
HWND hWnd, // handle to window or control with text
LPTSTR lpString, // address of buffer for text
int nMaxCount // maximum number of characters to copy
);

Parameters
hWnd
Handle to the window or control containing the text.
lpString
Pointer to the buffer that will receive the text.
nMaxCount
Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated.
Return Values
If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, callGetLastError.

This function cannot retrieve the text of an edit control in another application.

Remarks
If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string.

bobob 2003-01-04
  • 打赏
  • 举报
回复
高手~~
期待高手~~

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧