Listbox 拖放的问题!解决就给100分!

stwx 2003-03-12 08:00:01
listbox中有多个item, 一个对像拖放到一个listbox,
在listbox 的事件

List1_DragDrop(Source As Control, X As Single, Y As Single) 中

如何由X,Y得到拖放到listbox 的那个 list.ListIndex,要用API吗?
...全文
给本帖投票
113 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-03-14
  • 打赏
  • 举报
回复
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 Const LB_ITEMFROMPOINT = &H1A9

Private Sub Form_Load()
Dim i As Long
For i = 1 To 100
List1.AddItem i
Next
Text1.Text = "aaaa"'dragmode=1
Text2 = "bbbb"'dragmode=1
Text3 = "cccc"'dragmode=1
End Sub

Private Sub List1_DragDrop(Source As Control, X As Single, Y As Single)
Dim lXPoint As Long, lYPoint As Long, lIndex As Long
lXPoint = CLng(X / Screen.TwipsPerPixelX)
lYPoint = CLng(Y / Screen.TwipsPerPixelY)
If TypeOf Source Is TextBox Then lIndex = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint)) ' 获得当前的光标所在的的屏幕位置
List1.AddItem Source.Text, lIndex
End Sub
stwx 2003-03-13
  • 打赏
  • 举报
回复
我没弄过这些,谢谢了。100分请笑纳
northwolves 2003-03-13
  • 打赏
  • 举报
回复
奇怪。

Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
lXPoint = CLng(X / Screen.TwipsPerPixelX)
lYPoint = CLng(Y / Screen.TwipsPerPixelY)
lIndex = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint))
MsgBox List1.List(lIndex)
stwx 2003-03-13
  • 打赏
  • 举报
回复
to :northwolves(野性的呼唤

lYPoint 和 lXPoint 不用给值吗? 我何我的 LINDEX 总是0 因为lYPoint 和 lXPoint都是0 ,怎办
northwolves 2003-03-13
  • 打赏
  • 举报
回复
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 Const LB_ITEMFROMPOINT = &H1A9

Dim lXPoint As Long
Dim lYPoint As Long
Dim lIndex As Long
LINDEX = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint))
MsgBox List1.List(LINDEX)
stwx 2003-03-13
  • 打赏
  • 举报
回复
to :northwolves(野性的呼唤

你的代码出现 "DLL 调用约定错误",如何解决? lYpoint 和 lXPoint 为何值?

其它各位方法并不可取
gang75 2003-03-12
  • 打赏
  • 举报
回复
gz
northwolves 2003-03-12
  • 打赏
  • 举报
回复

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 Const LB_ITEMFROMPOINT = &H1A9
Dim LINDEX As Long
LINDEX = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, _
ByVal ((lYPoint * 65536) + lXPoint))
MsgBox List1.List(LINDEX)
xixixi9988 2003-03-12
  • 打赏
  • 举报
回复
应该很简单:List1.ListIndex = Y / Me.TextHeight ("A")
bobob 2003-03-12
  • 打赏
  • 举报
回复
是否可以用焦点来考虑?拖放的目的地应该具有焦点,再用api得到当前焦点的句柄~仅供参考
stephenlv 2003-03-12
  • 打赏
  • 举报
回复
这个我刚刚做过。
可以对list进行划分,计算得到一个数组。数组的每一个项对应于list的每一项。这样可以自己编一个函数来测试鼠标所在点是否在你想要的区域里,这样,就可以得到数组的index,进而知道list的当前index,使用index+list.topindex则可以得到相应的list.listindex
自定义类型:
type Point
x as single
y as single
end type

type Rect
point1 as Point
point2 as point
end type

dim m_Rects(dimension) as Rect'dimension为和list中可视的项目数相等。

7,785

社区成员

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

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

手机看
关注公众号

关注公众号

客服 返回
顶部