#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
Global Const $WM_DROPFILES = 0x0233
Global $aDroppedFiles[1]
Global $iExStyle = ""
GUICreate("支持拖放文件的ListView", 500, 400, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUIRegisterMsg($WM_DROPFILES, "WM_DROPFILES")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUICtrlCreateLabel("把文件拖放到ListView看看", 60, 20)
$hListView = GUICtrlCreateListView("文件名|大小|修改时间", 50, 50, 400, 300, -1)
$iExStyle += $LVS_EX_GRIDLINES ; 项目和子项显示网格.
$iExStyle += $LVS_EX_FULLROWSELECT ; 项目选中时, 该项及其所有子项高亮显示.
_GUICtrlListView_SetExtendedListViewStyle($hListView, $iExStyle)
GUICtrlSendMsg(-1, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_HEADERDRAGDROP, $LVS_EX_HEADERDRAGDROP)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
Exit
Case $GUI_EVENT_DROPPED
For $i = 1 To UBound($aDroppedFiles) - 1
$iSize = _SelectUnit(FileGetSize($aDroppedFiles[$i]))
$aTime = FileGetTime($aDroppedFiles[$i], 0)
_GUICtrlListView_AddItem($hListView, StringRegExpReplace($aDroppedFiles[$i], '^.*\\', ''))
;此处鼠标悬浮在激活的项目上用ToolTip显示完整路径 $aDroppedFiles[$i]
_GUICtrlListView_AddSubItem($hListView, _GUICtrlListView_GetItemCount($hListView) - 1, $iSize, 1)
;此处鼠标悬浮在激活的项目上用ToolTip显示 "文件大小:" & $size
_GUICtrlListView_AddSubItem($hListView, _GUICtrlListView_GetItemCount($hListView) - 1, _
StringFormat("%02i年%02i月%02i日 %02i:%02i:%02i", $aTime[0], $aTime[1], $aTime[2], $aTime[3], $aTime[4], $aTime[5]), 2)
Next
$iColumnCount = _GUICtrlListView_GetColumnCount($hListView)
For $i = 0 To $iColumnCount - 1
GUICtrlSendMsg($hListView, $LVM_SETCOLUMNWIDTH, $i, -1)
Next
EndSwitch
WEnd
Func WM_DROPFILES($hWnd, $msgID, $wParam, $lParam)
Local $nSize, $pFileName
Local $nAmt = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", 0xFFFFFFFF, "ptr", 0, "int", 255)
$aDroppedFiles = 0
Dim $aDroppedFiles[$nAmt[0] + 1]
For $i = 0 To $nAmt[0] - 1
$nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
$nSize = $nSize[0] + 1
$pFileName = DllStructCreate("char[" & $nSize & "]")
DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", _
DllStructGetPtr($pFileName), "int", $nSize)
$aDroppedFiles[0] += 1
$aDroppedFiles[$aDroppedFiles[0]] = DllStructGetData($pFileName, 1)
$pFileName = 0
Next
ReDim $aDroppedFiles[$aDroppedFiles[0] + 1]
EndFunc ;==>WM_DROPFILES
Func _SelectUnit($iSize)
If $iSize <= 1048576 Then
Return StringFormat("%.2f", Round($iSize / 1024, 2)) & 'KB'
ElseIf $iSize > 1048576 And $iSize <= 1073741824 Then
Return StringFormat("%.2f", Round($iSize / 1024 / 1024, 2)) & "MB"
Else
Return StringFormat("%.2f", Round($iSize / 1024 / 1024 / 1024, 2)) & "GB"
EndIf
EndFunc ;==>_SelectUnit
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndListView
Switch $iCode
Case $LVN_HOTTRACK
$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
Local $HotItem = _GUICtrlListView_GetHotItem($hListView)
If $HotItem<>-1 Then
$SubItem=DllStructGetData($tInfo, "SubItem")
ToolTip(_GUICtrlListView_GetItemText ($hListView,$HotItem,$SubItem))
Else
ToolTip("")
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
|