菜鸟求助:获取类似文本框窗口中光标所在位置的字符串
有这么一个想法不知能不能实现:
1、我发现某个程序中有一个输入框有点类似vb6的文本框,那个窗口的句柄可以获得,怎么样才能获取其中的光标所在位置的字符串?
2、能不能用一个textbox的类模块来与那个窗口进行绑定,当窗口中输入的字符发生变化时,类模块自动响应
3、能不能向其中任意位置输入某些字符?
求高人给点提示或解决方案,先谢谢啦。。。。。。。
[解决办法]
'****************************************************************************'恶魔界'恶魔界 -- 精心为你挑选,软件,VB源码,游戏,技术文章。总之很经典·····'发布日期:09/01/24/'描 述:查看记事本中的单词'网 站:http://www.emj-h.cn/'E-mail:270431700@163.com'QQ:270431700'经过在三考虑,本站决定,开启投稿机制(邮箱),欢迎广大高手投稿!'如果觉得本站好的话,就宣传下!'有什么好的源码,记得发给 恶魔界 哦!'感谢您使用本站源码,如果方便的话请给于本站一点支持,(点击一下广告)谢谢。'****************************************************************************Option ExplicitPrivate Const EM_CHARFROMPOS& = &HD7Private Type POINTAPI X As Long Y As LongEnd TypePrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Function RichWordOver(rch As RichTextBox, X As Single, Y As Single) As String Dim pt As POINTAPI Dim pos As Integer Dim start_pos As Integer Dim end_pos As Integer Dim ch As String Dim txt As String Dim txtlen As Integer pt.X = X \ Screen.TwipsPerPixelX pt.Y = Y \ Screen.TwipsPerPixelY '获得字符的位置。 pos = SendMessage(rch.hWnd, EM_CHARFROMPOS, 0&, pt) If pos <= 0 Then Exit Function txt = rch.Text For start_pos = pos To 1 Step -1 ch = Mid$(rch.Text, start_pos, 1) If Not ( _ (ch >= "0" And ch <= "9") Or _ (ch >= "a" And ch <= "z") Or _ (ch >= "A" And ch <= "Z") Or _ ch = "_" _ ) Then Exit For Next start_pos start_pos = start_pos + 1 txtlen = Len(txt) For end_pos = pos To txtlen ch = Mid$(txt, end_pos, 1) If Not ( _ (ch >= "0" And ch <= "9") Or _ (ch >= "a" And ch <= "z") Or _ (ch >= "A" And ch <= "Z") Or _ ch = "_" _ ) Then Exit For Next end_pos end_pos = end_pos - 1 If start_pos <= end_pos Then _ RichWordOver = Mid$(txt, start_pos, end_pos - start_pos + 1)End FunctionPrivate Sub Form_Load() RichTextBox1.Text = " Wecome to emj-h" & _ vbCrLf & vbCrLf & " come on! come on!" & _ vbCrLf & vbCrLf & " http://www.emj-h.cn"End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Label1.Caption = ""End SubPrivate Sub RichTextBox1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim txt As String txt = RichWordOver(RichTextBox1, X, Y) If Label1.Caption <> txt Then Label1.Caption = txt End If If txt = "" Then Label1.Caption = "" End IfEnd Sub
[解决办法]
下图是 第一个 程序FindWindow去调取 第二个 不同程序的窗口里面的 TextBox
我只帮你做第一部分获取光标位置 其它你自己再花时间继续搞吧,获取TextBox文字内容也已替你写好了.
需要代码的话上QQ我发给你,(两个程序的代码 在此不好贴)
[解决办法]
lz,他们都不看题目说什么,张嘴就来....
又跨进程又想实现你那种操控方法,是个比较复杂的内容
[解决办法]
咋听起来有点像木马
[解决办法]
同步的解决, 先决条件是 你要指定的那个窗口类是 Edit 等可写入的才能同步更新
'********获取该窗口的内容贴到本地的 Text1
Private Sub Command1_Click()
Phwnd = FindWindow(vbNullString, "隔岸观火")
If Phwnd <> 0 Then
ChildHwnd = FindWindowEx(Phwnd, 0, "ThunderTextBox", vbNullString)
If ChildHwnd > 0 Then
Text1.Text = GetText(ChildHwnd)
Call GetLine(ChildHwnd)
End If
End If
End Sub
Function GetText(THwnd As Long) As String
TextLen = SendMessage(THwnd, WM_GETTEXTLENGTH, 0, 0)
If TextLen = 0 Then GetText = "": Exit Function
TextLen = TextLen + 1
TmpStr = Space(TextLen)
TextLen = SendMessage(THwnd, WM_GETTEXT, TextLen, ByVal TmpStr)
GetText = Left(TmpStr, TextLen)
End Function
'****************本地的 Text1 Change事件或点击按钮一次刷新指定窗口
Private Sub Command2_Click()
SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal Text1.Text
End Sub
[解决办法]
哪个没看你的问题?
类型不知道你可以用工具看看,比如Spy++
类型你都不知道还指望别人给出完整的代码?
Private Declare Function GetForegroundWindow& Lib "user32" ()Private Declare Function GetCaretPos& Lib "user32" (ByVal XY As Long)Private Declare Function ClientToScreen& Lib "user32" (ByVal Hwnd As Long, ByVal XY As Long)Private Declare Function WindowFromPoint& Lib "user32" (ByVal X As Long, ByVal Y As Long)Private Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal Buffer As String, ByVal Size As Long)Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal Buffer As String, ByVal Size As Long)Private Declare Function SendMessageAny& Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal Msg As Long, ByVal WParam As Long, LParam As Any)Private Sub Timer1_Timer() 'Timer周期100Dim Hwnd&, XY&(1), Buffer As String * 260Hwnd = GetForegroundWindowGetCaretPos VarPtr(XY(0))ClientToScreen Hwnd, VarPtr(XY(0))Hwnd = WindowFromPoint(XY(0), XY(1))GetClassName Hwnd, Buffer, 260Me.Caption = XY(0) & "," & XY(1) & "|" & Replace(Buffer, vbNullChar, vbNullString)End Sub
[解决办法]