如何让鼠标悬浮在选中的listview项目上用ToolTip显示提示【已解决】
本帖最后由 xwt620 于 2011-11-3 00:14 编辑如图,就是鼠标选中某一项,然后鼠标悬浮在文件名那一列时则用ToolTip显示完整路径,如果悬浮在其他列又可以提示其他相关信息,如悬浮在文件大小上则显示文件大小。
顺便问一下,如何在WM_DROPFILES() 直接返回文件的大小,创建时间,修改时间,访问时间,和文件的相关属性?有没有这方面的文章?
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
Global Const $WM_DROPFILES = 0x0233
Global $aDroppedFiles
Global $iExStyle = ""
GUICreate("支持拖放文件的ListView", 500, 400, -1, -1, -1, $WS_EX_ACCEPTFILES)
GUIRegisterMsg($WM_DROPFILES, "WM_DROPFILES")
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, $aTime, $aTime, $aTime, $aTime, $aTime), 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 + 1]
For $i = 0 To $nAmt - 1
$nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
$nSize = $nSize + 1
$pFileName = DllStructCreate("char[" & $nSize & "]")
DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", _
DllStructGetPtr($pFileName), "int", $nSize)
$aDroppedFiles += 1
$aDroppedFiles[$aDroppedFiles] = DllStructGetData($pFileName, 1)
$pFileName = 0
Next
ReDim $aDroppedFiles[$aDroppedFiles + 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
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
Global Const $WM_DROPFILES = 0x0233
Global $aDroppedFiles
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, $aTime, $aTime, $aTime, $aTime, $aTime), 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 + 1]
For $i = 0 To $nAmt - 1
$nSize = DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", 0, "int", 0)
$nSize = $nSize + 1
$pFileName = DllStructCreate("char[" & $nSize & "]")
DllCall("shell32.dll", "int", "DragQueryFile", "hwnd", $wParam, "int", $i, "ptr", _
DllStructGetPtr($pFileName), "int", $nSize)
$aDroppedFiles += 1
$aDroppedFiles[$aDroppedFiles] = DllStructGetData($pFileName, 1)
$pFileName = 0
Next
ReDim $aDroppedFiles[$aDroppedFiles + 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 本帖最后由 xwt620 于 2011-11-2 23:52 编辑
回复 2# 3mile
用消息通知函数得到item中的text后,想得到文件完整路径这还真有点难啊,难道要增加一列专门显示完整路径吗,得像个好一点法子才行。
我觉得能不能在注释的地方增加一个相关的变量提示,然后就用这个变量提示来显示Tooltip呢? OK 搞定了,增加以下代码就OK了
_GUICtrlListView_AddItem($hListView, StringRegExpReplace($aDroppedFiles[$i], '^.*\\', ''))
_ArrayAdd($aFileFullpath, $aDroppedFiles[$i])
;此处鼠标悬浮在激活的项目上用ToolTip显示完整路径 $aDroppedFiles[$i]
修改消息函数的显示方式,加个判断。 $SubItem = DllStructGetData($tInfo, "SubItem")
If $SubItem = 0 Then
ToolTip($aFileFullpath[$HotItem + 1])
Else
ToolTip(_GUICtrlListView_GetItemText($hListView, $HotItem, $SubItem))
EndIf 好贴,值得收藏。 挺有意思的哈哈 学习 感谢分享,源码收藏了 真是不错的思路 收藏 收藏 这个真不错的。。。 这个帖子很好很强大。! $aFileFullpath需要补充 写了个简单版的,也能实现楼主要的功能#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>
Opt("MouseCoordMode", 2) ;鼠标坐标相对于窗口有效区------------------------------,很重要
Local $iExStyle
$GUI = GUICreate("支持拖放文件的ListView", 500, 400, -1, -1, -1, $WS_EX_ACCEPTFILES)
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)
Global $LVArray = [] ;文件名,大小,创建日期,完整路径
Global $OldHotItem = -1;,
AdlibRegister("_ShowToolTip")
GUISetState()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
Exit
Case $GUI_EVENT_DROPPED
$FileName = StringMid(@GUI_DRAGFILE, StringInStr(@GUI_DRAGFILE, "\", 0, -1)+1)
$FileSize = _SelectUnit(FileGetSize(@GUI_DRAGFILE))
$FileTimeArray = FileGetTime(@GUI_DRAGFILE, 0)
$FileTime = $FileTimeArray & "年" & $FileTimeArray & "月" & $FileTimeArray & "日 " & $FileTimeArray & ":" & $FileTimeArray & ":" & $FileTimeArray
GUICtrlCreateListViewItem($FileName & "|" & $FileSize & "|" & $FileTime, $hListView)
ReDim $LVArray
$LVArray = $FileName ;文件名
$LVArray = $FileSize ;文件尺寸
$LVArray = $FileTime ;创建日期
$LVArray = @GUI_DRAGFILE ;完整路径,供后续调用
$LVArray = UBound($LVArray)-1
EndSwitch
WEnd
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 _ShowToolTip()
If $LVArray = 0 Then Return 0
$Pos = GUIGetCursorInfo($GUI)
$HotItem = _GUICtrlListView_GetHotItem($hListView)
If $HotItem = -1 Or $Pos <> $hListView Then
ToolTip("")
ElseIf $HotItem <> $OldHotItem Then
$MPos = MouseGetPos() ;取得鼠标当前坐标
$CPos = ControlGetPos($GUI, "", $hListView) ;取处LV控件坐标,以确定控件X坐标在窗口的位置 ,即窗口边缘到控件的宽度
$ItemXY = _GUICtrlListView_GetItemRect($hListView, $HotItem) ;读取所点击行,第0个单元格的框坐标
$Sub1XY = _GUICtrlListView_GetSubItemRect($hListView, $HotItem, 1) ;读取所点击行,第1个单元格的框坐标
$Sub2XY = _GUICtrlListView_GetSubItemRect($hListView, $HotItem, 2) ;读取所点击行,第2个单元格的框坐标
If $MPos < $CPos + $Sub1XY Then ;如果鼠标坐标小于第二列左边坐标则说明鼠标在第1列上
$SubItem = 3
ElseIf $MPos > $CPos + $Sub1XY And $MPos < $CPos + $Sub2XY Then ;鼠标坐标介于第一列和第三列之间
$SubItem = 1
ElseIf $MPos > $CPos + $Sub2XY Then ;鼠标点在第3列内
$SubItem = 2
Else
ToolTip("")
Return 0
EndIf
ToolTip($LVArray[$HotItem+1][$SubItem])
$HotItem = $OldHotItem
EndIf
EndFunc 什么用途的、? 学习了,很不错的基础知识! 很不错,学习了
收藏先
页:
[1]
2