当listview是icon视图时,用户可以随意拖动图标(即使设置自动排列也一样,只是拖动后会重排,但背景图片不回归位)。这样,当用户拖动图标到边界时,listview会自动滚动,出现滚动条。
我现在想把图标限制在当前的视窗范围内,既不能拖出listview当前的工作区,不让他出现滚动条。实在不行就不让用户拖动图标,可以吗?有解决方法么?头疼了几天!
我现在想把图标限制在当前的视窗范围内,既不能拖出listview当前的工作区,不让他出现滚动条。实在不行就不让用户拖动图标,可以吗?有解决方法么?头疼了几天!
修改DragMode属性即可
===========????
修改为何值?好像它只影响整个listview的拖拉。
Option ExplicitPrivate Sub Form_Load()
Dim i As Long lvw.OLEDragMode = ccOLEDragAutomatic
For i = 1 To 10
lvw.ListItems.Add , "a" & CStr(i), "a" & CStr(i)
Next
End Sub
假设listview名为lvwDesktop,风格为icon(大图标模式)Private Sub Form_Load()
SetWindowLong lvwDesktop.hwnd, GWL_STYLE, (GetWindowLong(lvwDesktop.hwnd, GWL_STYLE) - LVS_NOSCROLL) ' 使之不能滚动
lvwDesktop.OLEDragMode = ccOLEDragManual '图标能拖动
...
End SubPrivate Sub lvwDesktop_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Debug.Print "mouse down"
Dim rc As RECT
If Button <> vbLeftButton Then Exit Sub rc.Left = 0
rc.Top = 0
rc.Right = lvwDesktop.Width / Screen.TwipsPerPixelX
rc.Bottom = lvwDesktop.Height / Screen.TwipsPerPixelY
Dim pt As POINTAPI
pt.x = rc.Left: pt.y = rc.Top
ClientToScreen lvwDesktop.hwnd, pt
rc.Left = pt.x
rc.Top = pt.y
pt.x = rc.Right: pt.y = rc.Bottom
ClientToScreen lvwDesktop.hwnd, pt
rc.Right = pt.x
rc.Bottom = pt.y
ClipCursor rc '限制鼠标移动在listview视口内
End SubPrivate Sub lvwDesktop_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button <> vbLeftButton Then Exit Sub ClipCursor vbNull ' 取消鼠标范围限制
End Sub
这就行了!相应的API申明如下:
Public Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
Public Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPublic Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePublic Type POINTAPI
x As Long
y As Long
End TypePublic Const GWL_STYLE = (-16)
Public Const LVS_NOSCROLL = &H2000