求教VB高手!!!!
制作的一个小程序,是获取当前句柄TEXT内容的,可是为什么无法实现呢?? 是因为限权的问题吗??? 求教高手指点迷津! 代码如下:
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Dim a As POINTAPI
Dim b As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
Dim aw As String
aw = Space(255)
GetWindowText b, aw, 255
Text1.Text = b
End Sub
Private Sub Timer1_Timer()
GetCursorPos a
b = WindowFromPoint(a.x, a.y)
Me.Caption = "当前指针句并:" & b
End Sub
补充:那该怎么学习API呢? 有什么好的方法或者教程吗? 介绍介绍
追问:895825177@qq.comThank~
答案:想获取任意文本的内容吗?可以留下你的邮箱我发送给你。觉对能满足你的要求。
'--------------------------------------------------------
'代码如下
'窗体1代码
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim a, s
a = Form1.Top '上下
s = Form1.Left + Form1.Width
Form2.Move s, a
Dim lHwnd As Long
Dim lChildHwnd As Long
Dim strRet As String
Dim n As Long
Dim bArr() As Byte, bArr2() As Byte
lHwnd = FindWindow(vbNullString, Form2.Label1)
If lHwnd > 0 Then
lChildHwnd = FindWindowEx(lHwnd, 0&, "Edit", vbNullString)
If lChildHwnd > 0 Then
n = SendMessage(lChildHwnd, WM_GETTEXTLENGTH, 0, 0)
If n > 0 Then
ReDim bArr(n + 1) As Byte
ReDim bArr2(n - 1) As Byte
Call RtlMoveMemory(bArr(0), n, 2)
Call SendMessage(lChildHwnd, WM_GETTEXT, n + 1, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), n)
strRet = StrConv(bArr2, vbUnicode)
Debug.Print "text=", strRet
Text1 = strRet
End If
End If
End If
End Sub
'请在窗体1中画一个时钟和一个文本框控件
'------------------------------
'窗体二代码
Option Explicit
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long
Private Sub Form_Load()
Timer1.Interval = 100
Label1.AutoSize = True
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Me.Hide
End Sub
Private Sub Timer1_Timer()
Static CurrentHwnd As Long
Dim ForegroundWindowHwnd As Long
Dim sText As String * 255
ForegroundWindowHwnd = GetForegroundWindow
If ForegroundWindowHwnd = CurrentHwnd Then Exit Sub
CurrentHwnd = ForegroundWindowHwnd
If CurrentHwnd <> hwnd Then
Label1.Caption = Left$(sText, GetWindowText(CurrentHwnd, sText, 255))
Else
Label1.Caption = ""
End If
End Sub
'请在窗体二中画一个标签和一个时钟控件
'---------------------------------------------------------
'如果不会可以留下你的邮箱我发送源码给你也可以。
请你详细解释程序是做什么功能的,经调试了一下,程序没什么问题,有可能是控件属性设置有误,比如Timer控件的interval属性不能为零,且Enable属性为True(自动运行),程序本身没有出现问题,请详细解释!
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Dim a As POINTAPI
Dim b As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub Form_Load()
Dim aw As String
aw = Space(255)
GetWindowText b, aw, 255
Text1.Text = b
End Sub
Private Sub Timer1_Timer()
GetCursorPos a
b = WindowFromPoint(a.x, a.y)
Me.Caption = "当前指针句并:" & b
End Sub
这代码当然运行不成功!一看逻辑就不对!首先在Private Sub Form_Load()
过程里,是程序加载到内存后就执行的代码,而在这里的变量a,b都是被初始化的。内容是系统分配的随机值,那么在这里GetWindowText b, aw, 255的变量b就不是这个text的句柄,当然就得不到标题了!这段代码应该写在timer里!可能还有别的问题,我没有运行代码,但是我上面说的就是首要因素,Private Sub Form_Load()
这个只执行一次,而timer是时间间隔循环,当Private Sub Form_Load()执行后,再执行timer的代码,这时A,B变量都有了值但已经没有用武之地了!
很复杂的问题。。。
上一个:怎样学好VB?
下一个:vb问题 急