找回密码
 加入
搜索
查看: 509|回复: 5

ListView 条目焦点高亮和按钮执行问题求助!

[复制链接]
发表于 2023-8-18 15:35:40 | 显示全部楼层 |阅读模式


代码如下:


#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

GUICreate("测试焦点高亮", 400, 300)
$idListview = GUICtrlCreateListView("", 2, 2, 394, 200, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT))
;按钮1
$Button1 = GUICtrlCreateButton("按钮", 160, 220, 120, 38)
GUISetState(@SW_SHOW)

;添加列
_GUICtrlListView_AddColumn($idListview, "第一列", 100)
_GUICtrlListView_AddItem($idListview, "测试焦点高亮1")
_GUICtrlListView_AddItem($idListview, "测试焦点高亮2")
_GUICtrlListView_SetItemSelected($idListview, 1)

While 1
        Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                        ExitLoop
                       
                        ;测试按钮
                Case $Button1
                        MsgBox(0, "测试", "执行了按钮")
                       
                        ;恢复高亮显示
                        ;_GUICtrlListView_ClickItem($idListview, 1)
        EndSwitch
WEnd

    以上代码当点击了按钮后,之前选中的带焦点和高亮条目自动变为了灰色,
       
_GUICtrlListView_ClickItem($idListview, 1)能恢复正常焦点高亮,但是按钮按下时还是灰色的。
       
有什么方法能点击按钮,按下按钮时不能变灰色, 能实时保持窗口内条目焦点和高亮,在线求助!

发表于 2023-8-18 17:57:32 来自手机 | 显示全部楼层
能想到的办法就是拦截鼠标点击消息,然后程序接管点击后的操作。
发表于 2023-8-21 00:44:24 | 显示全部楼层
目前找不到  $iColor  這全域變數是哪個UDF 提供的,說不定可以從這下手。
 楼主| 发表于 2023-8-21 09:09:54 | 显示全部楼层
好的,我找一下看看,感谢
 楼主| 发表于 2023-8-21 19:29:54 | 显示全部楼层
#include <GDIPlus.au3>
#include <WinAPIEx.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

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

$ListView = GUICtrlCreateListView("Column 1|Column 2|Column 3|Column 4", 10, 50, 380, 220, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($ListView)
;创建单选框1
$Radio1 = GUICtrlCreateRadio("测试按钮", 40, 20, 85, 20)
; Add items
For $i = 1 To 30
        GUICtrlCreateListViewItem("Row" & $i & ": Col 1|Row" & $i & ": Col 2|Row" & $i & ": Col 3|Row" & $i & ": Col 4", $ListView)
Next
;选中项目和焦点
_GUICtrlListView_SetItemState($ListView, 1, $LVIS_FOCUSED + $LVIS_SELECTED, $LVIS_FOCUSED + $LVIS_SELECTED)
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 ; Not in details mode

                                        Local $tCustDraw, $iDrawStage, $iItem, $iSubitem, $hDC, $tRect, $iColor1, $iColor2, $iColor3

                                        $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
                                                        ; Not handled
                                                Case BitOR($CDDS_ITEMPREPAINT, $CDDS_SUBITEM)
                                                        $iItem = DllStructGetData($tCustDraw, 'dwItemSpec')
                                                        $iSubitem = DllStructGetData($tCustDraw, 'iSubItem')

                                                        If _GUICtrlListView_GetItemSelected($hWndFrom, $iItem) Then ; Item to draw is selected
                                                                $hDC = _WinAPI_GetDC($hWndFrom)
                                                                $tRect = DllStructCreate($tagRECT)

                                                                ; We draw the background when we draw the first item.
                                                                If $iSubitem = 0 Then
                                                                        ; We must send the message as we want to use the struct. _GUICtrlListView_GetSubItemRect returns an array.
                                                                        _SendMessage($hWndFrom, $LVM_GETSUBITEMRECT, $iItem, DllStructGetPtr($tRect))

                                                                        DllStructSetData($tRect, "Left", 2)
                                                                        ;===>
                                                                        _GDIPlus_Startup()
                                                                        $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWndFrom)
                                                                        $hbrush = _GDIPlus_BrushCreateSolid("0xFF" & "0078D7")
                                                                       
                                                                        _GDIPlus_GraphicsFillRect($hGraphic, DllStructGetData($tRect, 'Left'), DllStructGetData($tRect, 'Top'), _
                                                                                        DllStructGetData($tRect, 'Right') - DllStructGetData($tRect, 'Left'), DllStructGetData($tRect, 'Bottom') - DllStructGetData($tRect, 'Top'), $hbrush)
                                                                       
                                                                        _GDIPlus_BrushDispose($hbrush)
                                                                        _GDIPlus_GraphicsDispose($hGraphic)
                                                                        _GDIPlus_Shutdown()
                                                                        ;===>
                                                                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) ; It uses the background drawn for the first item.
                                                                _WinAPI_SetTextColor($hDC, 0xFFFFFF)
                                                                ; Select the font we want to use
                                                                _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 ; Don't do default processing
                                                        EndIf
                                                       
                                                        Return $CDRF_NEWFONT ; Let the system do the drawing for non-selected items
                                                Case BitOR($CDDS_ITEMPOSTPAINT, $CDDS_SUBITEM)
                                                        ; Not handled
                                        EndSwitch
                        EndSwitch
        EndSwitch

        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
        Return BitAND(BitShift(String(Binary($iColor)), 8), 0x000000)
EndFunc   ;==>RGB2BGR

;在本论坛找到了一个函数,如果没有选择默认,是可以高亮的,但是选择了默认后,当鼠标悬停在选择条目上就会遮住文字显示
发表于 2023-8-29 06:38:09 | 显示全部楼层
太高深了,看了也不懂
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-2 03:06 , Processed in 0.076168 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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