找回密码
 加入
搜索
楼主: yuantian

[AU3基础] 请教ListView颜色变更的方式

[复制链接]
发表于 2020-8-10 23:34:50 | 显示全部楼层
yuantian 发表于 2020-8-10 22:31
A版大大,再请教我之前求助的问题2,如何获取列表序列中某一行的文字的背景颜色???

...........代碼來自官網

#Include <WinAPIEx.au3>
_Main()
Func _Main()
    Local $GUI, $Label, $Color_Array[6] = [5, 0x8AE5DC, 0x8FFF9F, 0xEC4841, 0xB0E35D, 0x440BFD] ; Random Color Array.
    $GUI = GUICreate("GUI")
    $Label = GUICtrlCreateLabel("", 0, 0, 500, 350)
    GUISetState(@SW_SHOW)
    For $A = 1 To $Color_Array[0]
        GUICtrlSetBkColor($Label, $Color_Array[$A])
        Sleep(20)
        MsgBox(0, "GUICtrlGetBkColor()", "The background color is >> 0x" & Hex($Color_Array[$A], 6) & @CRLF & _
                "and GUICtrlGetBkColor() says it's >> 0x" & Hex(GUICtrlGetBkColor($GUI, $Label), 6)) ; Pass the GUI Handle & Label ID to the Function.
    Next
    Exit
EndFunc   ;==>_Main
; #FUNCTION# =========================================================================================================
; Name...........: GUICtrlGetBkColor
; Description ...: Retrieves the hex value of the control background.
; Syntax.........: GUICtrlGetBkColor($hHandle, $iControl)
; Parameters ....: $hHandle - A valid GUI handle.
;                  $iControl - A valid control ID.
; Requirement(s).: v3.3.2.0 or higher
; Return values .: Success - Returns hex value of the control background.
;                  Failure - Returns 0 & sets @error = 1
; Author ........: guinness & additional information from Yashied.
; Example........; Yes
;=====================================================================================================================
Func GUICtrlGetBkColor($hHandle, $iControl)
;   Local $aControlGetPos = ControlGetPos($hHandle, "", $iControl)
;   Local $bReturn = PixelGetColor($aControlGetPos[2], $aControlGetPos[3], $hHandle)
;   If Not @error Then Return "0x" & Hex($bReturn, 6)
;   Return SetError(1, 0, 0)
    Local $hWnd = GUICtrlGetHandle($iControl)
    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $Rgb = _WinAPI_GetPixel($hDC, 0, 0)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    Return $Rgb
EndFunc   ;==>GUICtrlGetBkColor

评分

参与人数 1金钱 +20 收起 理由
yuantian + 20 很给力!

查看全部评分

发表于 2020-8-10 23:38:33 | 显示全部楼层
yuantian 发表于 2020-8-10 22:30
多设置一个空白,没问题。
自适应宽度也没问题。
这个第三个 禁止最后一列拉宽度?这个操作我没见到过 ...

效果一


#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
                                ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"
;                        Title    TextCol   BkCol         - colours in BGR
Global $aHdrData[][] = [["Tom",   0x000000, 0x00FFFF], _
                        ["Dick",  0x00FFFF, 0x0000FF], _
                        ["Harry", 0xFF0000, 0xFFCCCC] _
                        ]
Global $colCount = UBound($aHdrData)

$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
$cListView = GUICtrlCreateListView(_ArrayToString(_ArrayExtract($aHdrData, 0, $colCount-1, 0, 0)), 10, 10, 480, 280)
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
;Get header handle
Global $hHeader = _GUICtrlListView_GetHeader($cListView)
; Get the font of the Header control (credit KaFu)
Local $hDC = _WinAPI_GetDC($hHeader)
Local $hFont = _SendMessage($hHeader, $WM_GETFONT)
Local $hObject = _WinAPI_SelectObject($hDC, $hFont)
Local $tLogFont = DllStructCreate($tagLOGFONT)
_WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hHeader, $hDC)
Local $iWeight = DllStructGetData( $tLogFont, "Weight" )               ; Bold
DllStructSetData( $tLogFont, "Weight", BitOR( $iWeight, $FW_BOLD ) )
$hHdrFont = _WinAPI_CreateFontIndirect( $tLogFont )
For $i = 1 To 15
    _GUICtrlListView_AddItem($cListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($cListView, $i - 1, "SubItem" & $i, 1)
    _GUICtrlListView_AddSubItem($cListView, $i - 1, "SubItem" & $i, 2)
Next
For $i = 0 To  $colCount - 1
    _GUICtrlListView_SetColumnWidth($cListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return


    Local $tNMHEADER = DllStructCreate($tagNMHEADER, $lParam)
    If HWnd( DllStructGetData($tNMHEADER, "hWndFrom")) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Local Static $lastRect = 0
        Switch $iCode
            Case $HDN_TRACKW,$HDN_ENDTRACKW
    ConsoleWrite("C"&@CRLF)
                Local $pos = ControlGetPos($hGUI, "", $cListView)
                Local $rectLast = _GUICtrlListView_GetSubItemRect($hListView, _GUICtrlListView_GetItemCount($cListView) - 1, $colCount - 1, 0)
                If $pos[2] > $rectLast[2] Then
                    Local Static $scrollbarWidth = _WinAPI_GetSystemMetrics($SM_CXVSCROLL)
                    Local $rectFirst = _GUICtrlListView_GetItemRect($hListView, 0)
                    Local $sw = ($rectFirst[1] < 0 Or $rectLast[3] > $pos[3]) ? $scrollbarWidth : 0
                    GUICtrlSendMsg( $cListView, $LVM_SETCOLUMNWIDTH, $colCount-1, $pos[2] - $rectLast[0] - $sw - 4);resize last column
                    Return True
                EndIf
    

   
            Case $NM_CUSTOMDRAW
                Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam)
                Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage")
                Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                    Case $CDDS_PREPAINT ; Before the paint cycle begins
                        Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations
                    Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background)
                        Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations
                    Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts)
                        Local $tRECT = DllStructCreate($tagRECT)
                        Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index
                        Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context
                        _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font
                        _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background
                        _WinAPI_SetTextColor( $hDC, $aHdrData[$iIndex][1]) ; Set text colour
                        ; Get header section size
                        DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1)
                        DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7) + 1)
                        DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - 2)
                        DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2)
                        ; Set and draw back colour
                        Local $hBrush = _WinAPI_CreateSolidBrush($aHdrData[$iIndex][2])
                        _WinAPI_FillRect($hDC, $tRECT, $hBrush)
                        ; Write text
                        If $iIndex < _GUICtrlListView_GetColumnCount($cListView) Then
                            _WinAPI_DrawText ( $hDC, $aHdrData[$iIndex][0], $tRECT, $DT_CENTER + $DT_VCENTER )
                        EndIf
                        Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                EndSwitch
        EndSwitch
    EndIf
EndFunc   ;==>_WM_NOTIFY


评分

参与人数 1金钱 +30 收起 理由
yuantian + 30 很给力!

查看全部评分

发表于 2020-8-10 23:42:11 | 显示全部楼层
yuantian 发表于 2020-8-10 22:30
多设置一个空白,没问题。
自适应宽度也没问题。
这个第三个 禁止最后一列拉宽度?这个操作我没见到过 ...

效果二


#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FontConstants.au3>
#include <GuiListView.au3>
Global Const $tagNMCUSTOMDRAW = "struct;" & $tagNMHDR & ";dword dwDrawStage;handle hdc;" & $tagRECT & _
                                ";dword_ptr dwItemSpec;uint uItemState;lparam lItemlParam;endstruct"
;                        Title    TextCol   BkCol         - colours in BGR
Global $aHdrData[][] = [["Tom",   0x000000, 0x00FFFF], _
                        ["Dick",  0x00FFFF, 0x0000FF], _
                        ["Harry", 0xFF0000, 0xFFCCCC] _
                        ]
Global $colCount = UBound($aHdrData)

$hGUI = GUICreate("Set Listview Header Colour ", 500, 300)
$cListView = GUICtrlCreateListView(_ArrayToString(_ArrayExtract($aHdrData, 0, $colCount-1, 0, 0)), 10, 10, 480, 280)
$hListView = GUICtrlGetHandle($cListView)
_GUICtrlListView_SetExtendedListViewStyle($cListView, $LVS_EX_FULLROWSELECT) ; BitOR($LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
;Get header handle
Global $hHeader = _GUICtrlListView_GetHeader($cListView)
; Get the font of the Header control (credit KaFu)
Local $hDC = _WinAPI_GetDC($hHeader)
Local $hFont = _SendMessage($hHeader, $WM_GETFONT)
Local $hObject = _WinAPI_SelectObject($hDC, $hFont)
Local $tLogFont = DllStructCreate($tagLOGFONT)
_WinAPI_GetObject($hFont, DllStructGetSize($tLogFont), DllStructGetPtr($tLogFont))
_WinAPI_SelectObject($hDC, $hObject)
_WinAPI_ReleaseDC($hHeader, $hDC)
Local $iWeight = DllStructGetData( $tLogFont, "Weight" )               ; Bold
DllStructSetData( $tLogFont, "Weight", BitOR( $iWeight, $FW_BOLD ) )
$hHdrFont = _WinAPI_CreateFontIndirect( $tLogFont )
For $i = 1 To 15
    _GUICtrlListView_AddItem($cListView, "Item" & $i)
    _GUICtrlListView_AddSubItem($cListView, $i - 1, "SubItem" & $i, 1)
    _GUICtrlListView_AddSubItem($cListView, $i - 1, "SubItem" & $i, 2)
Next
For $i = 0 To  $colCount - 1
    _GUICtrlListView_SetColumnWidth($cListView, $i, $LVSCW_AUTOSIZE_USEHEADER)
Next
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return


    Local $tNMHEADER = DllStructCreate($tagNMHEADER, $lParam)
    If HWnd( DllStructGetData($tNMHEADER, "hWndFrom")) = $hHeader Then
        Local $iCode = BitAND(DllStructGetData($tStruct, 3), 0xFFFFFFFF)
        Local Static $lastRect = 0
        Switch $iCode

   Case  $HDN_BEGINTRACKW
    ; Now get column being resized
    Local $iCol = DllStructGetData($tNMHEADER, "Item")
    If $iCol = 2 Then  ;0 為第0列
     ; Prevent resizing
     Return True
    Else
     ; Allow resizing
     Return False
    EndIf
  
   
            Case $NM_CUSTOMDRAW
                Local $tNMCustomDraw = DllStructCreate($tagNMCUSTOMDRAW, $lParam)
                Local $dwDrawStage = DllStructGetData($tNMCustomDraw, "dwDrawStage")
                Switch $dwDrawStage ; Holds a value that specifies the drawing stage
                    Case $CDDS_PREPAINT ; Before the paint cycle begins
                        Return $CDRF_NOTIFYITEMDRAW ; Notify parent window of any item related drawing operations
                    Case $CDDS_ITEMPREPAINT ; Before an item is drawn: Default painting (frames and background)
                        Return $CDRF_NOTIFYPOSTPAINT ; Notify parent window of any post item related drawing operations
                    Case $CDDS_ITEMPOSTPAINT ; After an item is drawn: Custom painting (item texts)
                        Local $tRECT = DllStructCreate($tagRECT)
                        Local $iIndex = DllStructGetData($tNMCustomDraw, "dwItemSpec") ; Item index
                        Local $hDC = DllStructGetData($tNMCustomDraw, "hdc") ; Device context
                        _WinAPI_SelectObject($hDC, $hHdrFont) ; Set text font
                        _WinAPI_SetBkMode($hDC, $TRANSPARENT) ; Transparent background
                        _WinAPI_SetTextColor( $hDC, $aHdrData[$iIndex][1]) ; Set text colour
                        ; Get header section size
                        DllStructSetData($tRECT, 1, DllStructGetData($tNMCustomDraw, 6) + 1)
                        DllStructSetData($tRECT, 2, DllStructGetData($tNMCustomDraw, 7) + 1)
                        DllStructSetData($tRECT, 3, DllStructGetData($tNMCustomDraw, 8) - 2)
                        DllStructSetData($tRECT, 4, DllStructGetData($tNMCustomDraw, 9) - 2)
                        ; Set and draw back colour
                        Local $hBrush = _WinAPI_CreateSolidBrush($aHdrData[$iIndex][2])
                        _WinAPI_FillRect($hDC, $tRECT, $hBrush)
                        ; Write text
                        If $iIndex < _GUICtrlListView_GetColumnCount($cListView) Then
                            _WinAPI_DrawText ( $hDC, $aHdrData[$iIndex][0], $tRECT, $DT_CENTER + $DT_VCENTER )
                        EndIf
                        Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors
                EndSwitch
        EndSwitch
    EndIf
EndFunc   ;==>_WM_NOTIFY


评分

参与人数 1金钱 +30 收起 理由
yuantian + 30 很给力!

查看全部评分

 楼主| 发表于 2020-8-11 21:17:20 | 显示全部楼层
afan 发表于 2020-8-10 22:38
这个没试过。但可以个人经验建议,此类获取数据问题,除非是系统自动控制,或是第三方程序无法控制,其它 ...

还是指定靠谱。。。好的,谢谢
:)
 楼主| 发表于 2020-8-11 21:18:04 | 显示全部楼层

特别靠谱,万分感谢。
发表于 2020-8-11 21:23:13 | 显示全部楼层
本帖最后由 afan 于 2020-8-11 21:30 编辑
yuantian 发表于 2020-8-11 21:18
特别靠谱,万分感谢。

往左拖动红蓝交界看看是不是“特别靠谱”
如果你想使用该法,禁用一列是不行的,全禁才行。
 楼主| 发表于 2020-8-11 23:20:47 | 显示全部楼层
afan 发表于 2020-8-11 21:23
往左拖动红蓝交界看看是不是“特别靠谱”
如果你想使用该法,禁用一列是不行的,全禁才行。

是的,样式二,改成全禁止调整,很舒服..
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-4 06:07 , Processed in 0.072084 second(s), 15 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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