1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
   | ' --------------------------------------------------------------
' Permet de convertir les pixels en twips
' nécessaire pour utiliser "hittest"
 
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
 
Private Const HWND_DESKTOP As Long = 0
Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
 
Function TwipsPerPixelX() As Single
Dim lngDC As Long
    lngDC = GetDC(HWND_DESKTOP)
    TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
    ReleaseDC HWND_DESKTOP, lngDC
End Function
 
Function TwipsPerPixelY() As Single
Dim lngDC As Long
    lngDC = GetDC(HWND_DESKTOP)
    TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
    ReleaseDC HWND_DESKTOP, lngDC
End Function
 
' conversion pixels -> twips
Public Function TwipsX(ByVal NbPixels As Single) As Single
    TwipsX = NbPixels * (TwipsPerPixelX + 5)
End Function
 
Public Function TwipsY(ByVal NbPixels As Single) As Single
    TwipsY = NbPixels * (TwipsPerPixelY + 5)
End Function
 
' --------------------------------------------------------------
 
Private Sub listviewServices_OLEStartDrag(Data As MSComctlLib.DataObject, AllowedEffects As Long)
    'Debug.Print ("drag start")
    Data.SetData (listviewServices.SelectedItem.Index)
    listviewServices.ListItems.Add , , "" ' ajoute un nouvel élément à la listview qui permet
                                          ' de déplacer les élément en bas de la liste
    dragEnCours = True
    listviewServices.Sorted = False   ' désactive le tri automatique
End Sub
 
Private Sub listviewServices_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
    'Debug.Print ("drag over")
 
    On Error GoTo erreur
    If dragEnCours Then
	' les fonctions Twipsx et Twipsy permettent de convertir les pixels en twips
	' drophightlight permet de mettre en couleur l'item sur lequel se trouve la souris
	' hittest permet de retourner l'item sur lequel on se trouve
        Set listviewServices.DropHighlight = listviewServices.HitTest(TwipsX(x), TwipsY(y))
        listviewServices.HitTest(TwipsX(x), TwipsY(y)).EnsureVisible
    End If
 
    Exit Sub
erreur:
    ' la ligne de la listview n'existe pas
End Sub
 
Private Sub listviewServices_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
    'Debug.Print ("drag drop")
    dragEnCours = False
 
    On Error GoTo erreur
 
    Dim indexDrag, indexDrop As Integer
    indexDrag = CInt(Data.GetData(1))	' index de l'item sur lequel on a cliqué au départ
    indexDrop = listviewServices.DropHighlight.Index   ' index de l'item sur lequel on drop
 
    If indexDrag <> indexDrop Then ' si les deux index sont différents
	' on créé un nouvel item (copie de celui qu'on drag)
        listviewServices.ListItems.Add indexDrop, , listviewServices.ListItems.Item(indexDrag).Text
 
	' permet d'ajouter les subitems et de supprimer l'item de départ
        If indexDrag > indexDrop Then
            For i = 1 To 6
                listviewServices.ListItems.Item(indexDrop).ListSubItems.Add , , _
                        listviewServices.ListItems.Item(indexDrag + 1).ListSubItems.Item(i).Text
            Next
 
            listviewServices.ListItems.Remove (indexDrag + 1)
            listviewServices.ListItems.Item(indexDrop).Selected = True
        Else
            For i = 1 To 6
                listviewServices.ListItems.Item(indexDrop).ListSubItems.Add , , _
                        listviewServices.ListItems.Item(indexDrag).ListSubItems.Item(i).Text
            Next
 
            listviewServices.ListItems.Remove (indexDrag)
            listviewServices.ListItems.Item(indexDrop - 1).Selected = True
        End If
    End If
 
erreur:
 
End Sub
 
Private Sub listviewServices_OLECompleteDrag(Effect As Long)
    'Debug.Print ("drag complete")
 
    Set listviewServices.DropHighlight = Nothing
 
    ' on supprime l'item qu'on a créé au début du drag & drop qui permettait de déplacer l'item tout
    ' en bas de la listview
    listviewServices.ListItems.Remove (listviewServices.ListItems.Count)
 
End Sub | 
Partager