找回密码
 加入
搜索
查看: 423|回复: 4

ListView条目选择问题求助?

  [复制链接]
发表于 2023-9-12 12:31:35 | 显示全部楼层 |阅读模式


以下代码来自本论坛,修改了个选中条目的颜色


#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIex.au3>
#include <StructureConstants.au3>
#include <GDIPlus.au3>

$GUI = GUICreate("Listview Custom Draw", 400, 300)

$cListView = GUICtrlCreateListView("Column 1|Column 2|Column 3", 2, 2, 394, 268, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)

For $i = 1 To 30
        GUICtrlCreateListViewItem("Row" & $i & ": Col 1|Row" & $i & ": Col 2|Row" & $i & ": Col 3", $cListView)
Next
;~ GUICtrlCreateInput("Click here to test focus", 50, 275, 200, 18)


GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Exit

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
        Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

        $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
        $iCode = DllStructGetData($tNMHDR, "Code")

        Switch $hWndFrom
                Case $hListView
                        Switch $iCode
                                Case $NM_CUSTOMDRAW
                                        If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
                                        Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $Color, $Fram
                                        $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                                        $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                                        Switch $iDrawStage
                                                Case $CDDS_PREPAINT
                                                        Return $CDRF_NOTIFYITEMDRAW
                                                Case $CDDS_ITEMPREPAINT
                                                        Return $CDRF_NOTIFYSUBITEMDRAW
                                                Case $CDDS_ITEMPOSTPAINT
                                                Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                                                        $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                                                        $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')

                                                        If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then
                                                                $hDC = _WinAPI_GetDC($hWndFrom)
                                                                $tRect = DllStructCreate($tagRECT)
                                                                If $iSubitem = 0 Then
                                                                        _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
                                                                        DllStructSetData($tRect, "Left", 2)
                                                                        $Color = _WinAPI_GetStockObject($DC_BRUSH)
                                                                        $Fram = _WinAPI_GetStockObject($BLACK_BRUSH)
                                                                        _WinAPI_SetDCBrushColor($hDC, 0x0078D7)
                                                                        _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $Color)
                                                                        _WinAPI_FrameRect($hDC, DllStructGetPtr($tRect), $Fram)
                                                                EndIf

                                                                DllStructSetData($tRect, "Left", 2)
                                                                DllStructSetData($tRect, "Top", $iSubitem)
                                                                _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))

                                                                Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
                                                                _WinAPI_SetBkMode($hDC, $TRANSPARENT)

                                                                _WinAPI_SelectObject($hDC, _SendMessage($hWndFrom, $WM_GETFONT))

                                                                If $iSubitem = 0 Then
                                                                        DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 2)
                                                                Else
                                                                        DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 6)
                                                                EndIf
                                                                _WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))

                                                                _WinAPI_ReleaseDC($hWndFrom, $hDC)

                                                                Return $CDRF_SKIPDEFAULT
                                                        EndIf

                                                        Return $CDRF_NEWFONT
                                                Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
                                        EndSwitch
                        EndSwitch
        EndSwitch

        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

    需要实现:当窗口出现后自动选中索引项目第3项,当选择其它项目时取消索引项目的第3项,在线求助。

 楼主| 发表于 2023-9-12 13:08:59 | 显示全部楼层
本帖最后由 h111666b 于 2023-9-12 13:10 编辑

; 在窗口创建后默认选中索引3
_GUICtrlListView_SetItemState($hListView, 3, $LVIS_SELECTED, $LVIS_SELECTED)

使用此项选中索引3后,当窗口出现鼠标悬停到选择的索引3条目时,文字会丢失不全,重新点击才正常显示

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2023-9-12 22:27:47 | 显示全部楼层
h111666b 发表于 2023-9-12 13:08
; 在窗口创建后默认选中索引3
_GUICtrlListView_SetItemState($hListView, 3, $LVIS_SELECTED, $LVIS_SELE ...
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>


Global $hListView, $iSelectedItem

$GUI = GUICreate("Listview Custom Draw", 400, 300)

$cListView = GUICtrlCreateListView("Column 1|Column 2|Column 3", 2, 2, 394, 268, -1)
$hListView = GUICtrlGetHandle($cListView)

For $i = 1 To 30
    GUICtrlCreateListViewItem("Row" & $i & ": Col 1|Row" & $i & ": Col 2|Row" & $i & ": Col 3", $cListView)
Next

; 设置默认选中索引为3的列表项
$iSelectedItem = 3
_GUICtrlListView_SetItemSelected($hListView, 3, True)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Exit

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR, $hWndFrom, $iIDFrom, $iCode

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $Color
                    $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                    $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                    Select
                        Case $iDrawStage = $CDDS_PREPAINT
                            Return $CDRF_NOTIFYITEMDRAW

                        Case BitOR($iDrawStage, $CDDS_ITEMPREPAINT) = BitOR($CDDS_ITEMPREPAINT, 0x1000)
                            $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                            $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')
                            $hDC = DllStructGetData($tCustDraw, "hdc")
                            $tRect = DllStructCreate($tagRECT, DllStructGetData($tCustDraw, "rcText"))

                            ; 设置填充和边框颜色
                            If $iItem = $iSelectedItem - 1 Then
                                $Color = 0x0078D7
                            Else
                                $Color = 0xFFFFFF
                            EndIf

                            _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), _WinAPI_GetStockObject($BLACK_BRUSH))
                            _WinAPI_FrameRect($hDC, DllStructGetPtr($tRect), _WinAPI_GetStockObject($BLACK_BRUSH))

                            ; 绘制文本
                            DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 6)
                            Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
                            _WinAPI_SetBkMode($hDC, $TRANSPARENT)
                            _WinAPI_SetTextColor($hDC, $Color)
                            _WinAPI_DrawText($hDC, $sText, $tRect, $DT_VCENTER + $DT_END_ELLIPSIS + $DT_SINGLELINE)

                            Return $CDRF_SKIPDEFAULT

                        Case Else
                            ; 绘制完成
                    EndSelect
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
 楼主| 发表于 2023-9-13 10:45:41 | 显示全部楼层
本帖最后由 h111666b 于 2023-9-13 10:53 编辑

感谢回复,但是还是没达到要求,当点击了列表以外按钮焦点高亮消失,而原先的是不会失去高亮显示的
使用_GUICtrlListView_SetItemDropHilited($cListView, 3, True) 此项可以保持高亮,现在有个问题就是,当点击其它项目后怎么取消已经选中的高亮项目3

代码如下


lude <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIex.au3>
#include <StructureConstants.au3>
#include <GDIPlus.au3>

$GUI = GUICreate("Listview Custom Draw", 400, 300)

$cListView = GUICtrlCreateListView("Column 1|Column 2|Column 3", 2, 2, 394, 150, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)

For $i = 1 To 30
        GUICtrlCreateListViewItem("Row" & $i & ": Col 1|Row" & $i & ": Col 2|Row" & $i & ": Col 3", $cListView)
Next
;~ GUICtrlCreateInput("Click here to test focus", 50, 275, 200, 18)

;设置选中项目高亮显示
_GUICtrlListView_SetItemDropHilited($cListView, 3, True)

$Button1 = GUICtrlCreateButton("测试按钮", 100, 154, 60, 27)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Exit

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
        Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

        $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
        $iCode = DllStructGetData($tNMHDR, "Code")

        Switch $hWndFrom
                Case $hListView
                        Switch $iCode
                                Case $NM_CUSTOMDRAW
                                        If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
                                        Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $Color, $Fram
                                        $tCustDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
                                        $iDrawStage = DllStructGetData($tCustDraw, 'dwDrawStage')
                                        Switch $iDrawStage
                                                Case $CDDS_PREPAINT
                                                        Return $CDRF_NOTIFYITEMDRAW
                                                Case $CDDS_ITEMPREPAINT
                                                        Return $CDRF_NOTIFYSUBITEMDRAW
                                                Case $CDDS_ITEMPOSTPAINT
                                                Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                                                        $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                                                        $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')

                                                        If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then
                                                                $hDC = _WinAPI_GetDC($hWndFrom)
                                                                $tRect = DllStructCreate($tagRECT)
                                                                If $iSubitem = 0 Then
                                                                        _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))
                                                                        DllStructSetData($tRect, "Left", 2)
                                                                        $Color = _WinAPI_GetStockObject($DC_BRUSH)
                                                                        $Fram = _WinAPI_GetStockObject($BLACK_BRUSH)
                                                                        _WinAPI_SetDCBrushColor($hDC, 0x0078D7)
                                                                        _WinAPI_FillRect($hDC, DllStructGetPtr($tRect), $Color)
                                                                        _WinAPI_FrameRect($hDC, DllStructGetPtr($tRect), $Fram)
                                                                EndIf

                                                                DllStructSetData($tRect, "Left", 2)
                                                                DllStructSetData($tRect, "Top", $iSubitem)
                                                                _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))

                                                                Local $sText = _GUICtrlListView_GetItemText($hWndFrom, $iItem, $iSubitem)
                                                                _WinAPI_SetBkMode($hDC, $TRANSPARENT)

                                                                _WinAPI_SelectObject($hDC, _SendMessage($hWndFrom, $WM_GETFONT))

                                                                If $iSubitem = 0 Then
                                                                        DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 2)
                                                                Else
                                                                        DllStructSetData($tRect, "Left", DllStructGetData($tRect, "Left") + 6)
                                                                EndIf
                                                                _WinAPI_DrawText($hDC, $sText, $tRect, BitOR($DT_VCENTER, $DT_END_ELLIPSIS, $DT_SINGLELINE))

                                                                _WinAPI_ReleaseDC($hWndFrom, $hDC)

                                                                Return $CDRF_SKIPDEFAULT
                                                        EndIf

                                                        Return $CDRF_NEWFONT
                                                Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
                                        EndSwitch
                        EndSwitch
        EndSwitch

        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2023-9-13 10:54:48 | 显示全部楼层
需要实现以上第二张图片要求,点击按钮或者窗口外其它项目,列表框内选中的条目高亮不会消失
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-5 03:56 , Processed in 0.079559 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表