如何通过鼠标移动来自动选中光标所在的列表项

progame 2003-06-11 03:07:28
.
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
progame 2003-06-11
  • 打赏
  • 举报
回复
同喜同喜
rappercn 2003-06-11
  • 打赏
  • 举报
回复
恭喜恭喜。
rappercn 2003-06-11
  • 打赏
  • 举报
回复
什么控件?ListBox吗?
progame 2003-06-11
  • 打赏
  • 举报
回复
LB_ITEMFROMPOINT

应该是这个了
bydisplay 2003-06-11
  • 打赏
  • 举报
回复
'下面程序给你一个思路,稍做改动即可
'添加模块
Option Explicit

Public Const EM_CHARFROMPOS = &HD7
Public Const EM_GETLINECOUNT = &HBA
Public Const EM_GETLINE = &HC4
Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1
Public Const EM_SETSEL = &HB1

Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
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 Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pos As Long, lc As Long
Dim Line As Integer, CharPos As Integer

pos = X / Screen.TwipsPerPixelX + Y / Screen.TwipsPerPixelY * 65536
lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)

Line = lc \ 65536
CharPos = lc Mod 65536

MsgBox " = " & GetLine(Text1, Line) & vbCrLf & "单词= " & GetWord(Text1, CharPos)
End Sub

Function GetWord(txt As TextBox, pos As Integer) As String
Dim bArr() As Byte, pos1 As Integer, pos2 As Integer, i As Integer

bArr = StrConv(txt.Text, vbFromUnicode)
pos1 = 0: pos2 = UBound(bArr)

For i = pos - 1 To 0 Step -1
If IsDelimiter(bArr(i)) Then
pos1 = i + 1
Exit For
End If
Next

For i = pos To UBound(bArr)
If IsDelimiter(bArr(i)) Then
pos2 = i - 1
Exit For
End If
Next

If pos2 > pos1 Then
ReDim bArr2(pos2 - pos1) As Byte
For i = pos1 To pos2
bArr2(i - pos1) = bArr(i)
Next

GetWord = StrConv(bArr2, vbUnicode)


SendMessage txt.hwnd, EM_SETSEL, pos1, ByVal CLng(pos2 + 1)
Else
GetWord = ""
End If
End Function

Function IsDelimiter(ByVal Char As Byte) As Boolean
Dim S As String

S = Chr(Char)
IsDelimiter = False
If S = " " Or S = "," Or S = "." Or S = "?" Or S = vbCr Or S = vbLf Then
IsDelimiter = True
End If
End Function

Function GetLine(txt As TextBox, ByVal Line As Integer) As String
Dim S As String, Length As Integer, pos As Long

GetLine = ""
pos = SendMessage(txt.hwnd, EM_LINEINDEX, Line, ByVal 0&)
Length = SendMessage(txt.hwnd, EM_LINELENGTH, pos, ByVal 0&)
S = String(Length, Chr(0))
RtlMoveMemory ByVal S, Length, 2
If SendMessage(Text1.hwnd, EM_GETLINE, Line, ByVal S) > 0 Then
GetLine = S
End If
End Function

Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pos As Long, lc As Long
Dim Line As Integer, CharPos As Integer

pos = X / Screen.TwipsPerPixelX + Y / Screen.TwipsPerPixelY * 65536
lc = SendMessage(Text1.hwnd, EM_CHARFROMPOS, 0, ByVal pos)

Line = lc \ 65536
CharPos = lc Mod 65536

Text1.ToolTipText = GetWord(Text1, CharPos)
End Sub

anosoft 2003-06-11
  • 打赏
  • 举报
回复
如果是listview控件 listview.HoverSelection = True

7,763

社区成员

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

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