[已解决]问个不知是不是简单的问题,如何获取控件的文本颜色?
本帖最后由 志艺风云 于 2018-8-13 18:32 编辑设置控件的文本颜色,GUICtrlSetColor(控件ID, 文本颜色)如何获取控件的文本颜色?什么函数?或API?
本帖最后由 志艺风云 于 2018-7-22 17:36 编辑
实际上我是想设置树视图的一行文本颜色和获取刚设置的颜色
_GUICtrlTreeView_SetTextColor是设置(树视图)全部文本颜色的,不能设置一行,只能用GUICtrlSetColor,所以_GUICtrlTreeView_GetTextColor也检索不到(树视图)那一项的文本颜色。
api GetTextColor GetDC和GetTextColor取色没尝试过.说说另外一个着色的问题.如果使用guictrlsetcolor设置每项的颜色,会严重影响树视图的绘制速度
treeview项目着色原理应与listview一致.论坛上有很多listview的单项着色的例子.学会处理WM_NOTIFY中的NM_CUSTOMDRAW消息即可.
可以用_GUICtrlTreeView_SetItemParam指定相关项目的Param值,然后根据Param值来设置着色.这个Param值请设置大些.如90000等.不要与工程内的控件ID相同.帮助文档中提示;
警告:不要对 GUICtrlCreateTreeViewItem 创建的项目使用 SetItemParam; Param(参数) 是内置函数创建项目的控件 ID经我测试,只要不涉及使用treeview子项目的控件ID操作,以上这个警告可以忽略.
大致代码如下
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
Local $ItemParam = DllStructGetData($tCustDraw, 'ItemParam')
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
Switch $ItemParam
Case $__idItemParam1
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ColorRoot));根颜色
Case $__idItemParam2
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($UDFTextColor));udf颜色
Case $__idItemParam3
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ContrlTextColor));控制颜色
Case $__idItemParam4
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($CommentsTextColor));注释颜色
EndSwitch
Return $CDRF_NEWFONT
; Return $CDRF_DODEFAULT
在开头声明常量
Global Enum $__idItemParam1 = 999900, $__idItemParam2, $__idItemParam3, $__idItemParam4 tubaba 发表于 2018-7-23 15:02
在开头声明常量
Global Enum $__idItemParam1 = 999900, $__idItemParam2, $__idItemParam3, $__idItemPar ...
请教一下,我是那里不对?
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_UseX64 = n
Global $hTreeView0, $hTreeView
Global Const $Crimson = 0x990000
Global Const $ColorRoot = 0x990000
Global Const $COLOR_Turquoise = 0xfbfcfd;0x40e0d0
Global Const $COLOR_Crimson = 0xDC143C
Global $hTvItem0 = 0x990000, $hTvItem1, $hTvItem2, $hTvItem3, $hTvItem02
Global Const $UDFTextColor = 0x0000FF
Global Const $ContrlTextColor = 0x009900
Global Const $CommentsTextColor = 0xFF00FF
Example()
Func Example()
Local $hGUI, $hItem
$hGUI = GUICreate("(UDF 创建) TreeView", 900, 600)
$hTreeView0 = GUICtrlCreateTreeView(4, 4, 228, 540, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE););,$WS_BORDER); BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 9, 600, 0, '微软雅黑')
$hTvItem0 = GUICtrlCreateTreeViewItem("三年段成绩统计", $hTreeView0)
$hTvItem01 = GUICtrlCreateTreeViewItem("三年五班统计", $hTvItem0)
$hTvItem011 = GUICtrlCreateTreeViewItem("语文-数学-英语", $hTvItem01)
$hTvItem012 = GUICtrlCreateTreeViewItem("美术-体育-音乐", $hTvItem01)
$hTvItem02 = GUICtrlCreateTreeViewItem("三年六班统计", $hTvItem0)
$hTvItem03 = GUICtrlCreateTreeViewItem("三年七班统计", $hTvItem0)
$hTvItem1 = GUICtrlCreateTreeViewItem("四年段成绩统计", $hTreeView0)
GUICtrlCreateTreeViewItem("四年一班统计", $hTvItem1)
GUICtrlCreateTreeViewItem("四年二班统计", $hTvItem1)
$hTvItem02 = GUICtrlCreateTreeViewItem("工具", $hTreeView0)
$MenuItemd4 = GUICtrlCreateTreeViewItem("密码修改", $hTvItem02)
$MenuItemd5 = GUICtrlCreateTreeViewItem("备份数据库", $hTvItem02)
$MenuItemd6 = GUICtrlCreateTreeViewItem("桌面快捷方式", $hTvItem02)
$MenuItemd7 = GUICtrlCreateTreeViewItem("计算器", $hTvItem02)
$hTvItem3 = GUICtrlCreateTreeViewItem("系统设置目录", $hTreeView0)
GUICtrlCreateTreeViewItem("系统设置", $hTvItem3)
GUICtrlCreateTreeViewItem("系统优化", $hTvItem3)
GUICtrlCreateTreeViewItem("系统显示", $hTvItem3)
$MenuItem20 = GUICtrlCreateTreeViewItem("注册", $hTvItem3)
$MenuItem21 = GUICtrlCreateTreeViewItem("关于注册", $hTvItem3)
$MenuItem22 = GUICtrlCreateTreeViewItem("关于程序", $hTvItem3)
$MenuItem23 = GUICtrlCreateTreeViewItem("查看属性", $hTvItem3)
$MenuItem24 = GUICtrlCreateTreeViewItem("关闭", $hTvItem3)
Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUICtrlTreeView_SetNormalImageList($hTreeView0, $hImage)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
;_GUICtrlTreeView_BeginUpdate($hTreeView0)
;_GUICtrlTreeView_EndUpdate($hTreeView0)
_GUICtrlTreeView_Expand($hTreeView0)
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView0
If Not IsHWnd($hTreeView0) Then $hWndTreeview = GUICtrlGetHandle($hTreeView0)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
Local $ItemParam = DllStructGetData($tCustDraw, 'ItemParam')
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
Switch $ItemParam
Case $hTvItem0
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ColorRoot));根颜色
Case $hTvItem1
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($UDFTextColor));udf颜色
Case $hTvItem02;$hTvItem2
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ContrlTextColor));控制颜色
Case $hTvItem3
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($CommentsTextColor));注释颜色
EndSwitch
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>_RGB2BGR
本帖最后由 tubaba 于 2018-7-24 12:30 编辑
chzj589 发表于 2018-7-24 11:45
请教一下,我是那里不对?
概念混淆了.Global Enum $hParamTvItem0 = 990000, $hParamTvItem1, $hParamTvItem2, $hParamTvItem3, $hParamTvItem02 ;是指定一个较大的常数值,此值将在后面的代码中赋予treeview子项的param,需要什么颜色就赋予子项相对应的param值.
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_UseX64 = n
Global $hTreeView0, $hTreeView
Global Const $Crimson = 0x990000
Global Const $ColorRoot = 0x990000
Global Const $COLOR_Turquoise = 0xfbfcfd;0x40e0d0
Global Const $COLOR_Crimson = 0xDC143C
Global Enum $hParamTvItem0 = 990000, $hParamTvItem1, $hParamTvItem2, $hParamTvItem3, $hParamTvItem02
Global Const $UDFTextColor = 0x0000FF
Global Const $ContrlTextColor = 0x009900
Global Const $CommentsTextColor = 0xFF00FF
Example()
Func Example()
Local $hGUI, $hItem
$hGUI = GUICreate("(UDF 创建) TreeView", 900, 600)
$hTreeView0 = GUICtrlCreateTreeView(4, 4, 228, 540, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE););,$WS_BORDER); BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 9, 600, 0, '微软雅黑')
$hTvItem0 = GUICtrlCreateTreeViewItem("三年段成绩统计", $hTreeView0)
$hTvItem01 = GUICtrlCreateTreeViewItem("三年五班统计", $hTvItem0)
$hTvItem011 = GUICtrlCreateTreeViewItem("语文-数学-英语", $hTvItem01)
_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem011), $hParamTvItem0)
$hTvItem012 = GUICtrlCreateTreeViewItem("美术-体育-音乐", $hTvItem01)
_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem012), $hParamTvItem1)
$hTvItem02 = GUICtrlCreateTreeViewItem("三年六班统计", $hTvItem0)
_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem02), $hParamTvItem2)
$hTvItem03 = GUICtrlCreateTreeViewItem("三年七班统计", $hTvItem0)
_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem03), $hParamTvItem3)
$hTvItem1 = GUICtrlCreateTreeViewItem("四年段成绩统计", $hTreeView0)
GUICtrlCreateTreeViewItem("四年一班统计", $hTvItem1)
GUICtrlCreateTreeViewItem("四年二班统计", $hTvItem1)
$hTvItem02 = GUICtrlCreateTreeViewItem("工具", $hTreeView0)
$MenuItemd4 = GUICtrlCreateTreeViewItem("密码修改", $hTvItem02)
$MenuItemd5 = GUICtrlCreateTreeViewItem("备份数据库", $hTvItem02)
$MenuItemd6 = GUICtrlCreateTreeViewItem("桌面快捷方式", $hTvItem02)
$MenuItemd7 = GUICtrlCreateTreeViewItem("计算器", $hTvItem02)
$hTvItem3 = GUICtrlCreateTreeViewItem("系统设置目录", $hTreeView0)
GUICtrlCreateTreeViewItem("系统设置", $hTvItem3)
GUICtrlCreateTreeViewItem("系统优化", $hTvItem3)
GUICtrlCreateTreeViewItem("系统显示", $hTvItem3)
$MenuItem20 = GUICtrlCreateTreeViewItem("注册", $hTvItem3)
$MenuItem21 = GUICtrlCreateTreeViewItem("关于注册", $hTvItem3)
$MenuItem22 = GUICtrlCreateTreeViewItem("关于程序", $hTvItem3)
$MenuItem23 = GUICtrlCreateTreeViewItem("查看属性", $hTvItem3)
$MenuItem24 = GUICtrlCreateTreeViewItem("关闭", $hTvItem3)
Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUICtrlTreeView_SetNormalImageList($hTreeView0, $hImage)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
;_GUICtrlTreeView_BeginUpdate($hTreeView0)
;_GUICtrlTreeView_EndUpdate($hTreeView0)
_GUICtrlTreeView_Expand($hTreeView0)
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView0
If Not IsHWnd($hTreeView0) Then $hWndTreeview = GUICtrlGetHandle($hTreeView0)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_CUSTOMDRAW
Local $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
Local $ItemParam = DllStructGetData($tCustDraw, 'ItemParam')
If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
Switch $ItemParam
Case $hParamTvItem0
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ColorRoot));根颜色
Case $hParamTvItem1
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($UDFTextColor));udf颜色
Case $hParamTvItem2;$hTvItem2
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ContrlTextColor));控制颜色
Case $hParamTvItem3
DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($CommentsTextColor));注释颜色
EndSwitch
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>_RGB2BGR
Global Enum $hParamTvItem0 = 990000, $hParamTvItem1, $hParamTvItem2, $hParamTvItem3, $hParamTvItem02
enum是指枚举常量,对于上述表达式来说,$hParamTvItem1 =990001,$hParamTvItem2=990002,$hParamTvItem3=990003...以此类推 本帖最后由 chzj589 于 2018-7-24 13:04 编辑
tubaba 发表于 2018-7-24 12:33
Global Enum $hParamTvItem0 = 990000, $hParamTvItem1, $hParamTvItem2, $hParamTvItem3, $hParamTvItem02 ...
谢谢指教!
双击 工具或密码修改 项,读取出错,设置红色
读取过的正常颜色 绿色
当前读取项 蓝色,加粗,这个不知怎么搞
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#AutoIt3Wrapper_UseX64 = n
Global $hTreeView0, $hTreeView
Global Const $Crimson = 0x990000
Global Const $ColorRoot = 0x990000
Global Const $COLOR_Turquoise = 0xfbfcfd ;0x40e0d0
Global Const $COLOR_Crimson = 0xDC143C
Global Enum $hParamTvItem0 = 990000, $hParamTvItem1, $hParamTvItem2, $hParamTvItem3, $hParamTvItem02
Global Const $UDFTextColor = 0x0000FF
Global Const $ContrlTextColor = 0x009900
Global Const $CommentsTextColor = 0xFF00FF
Example()
Func Example()
Local $hGUI, $hItem
$hGUI = GUICreate("(UDF 创建) TreeView", 900, 500)
$hTreeView0 = GUICtrlCreateTreeView(5, 5, 230, 490, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE) ;);,$WS_BORDER); BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1, 9, 400, 0, '微软雅黑')
$hTvItem0 = GUICtrlCreateTreeViewItem("三年段成绩统计", $hTreeView0)
$hTvItem01 = GUICtrlCreateTreeViewItem("三年五班统计", $hTvItem0)
$hTvItem011 = GUICtrlCreateTreeViewItem("语文-数学-英语", $hTvItem01)
;_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem011), $hParamTvItem0)
$hTvItem012 = GUICtrlCreateTreeViewItem("美术-体育-音乐", $hTvItem01)
;_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem012), $hParamTvItem1)
$hTvItem02 = GUICtrlCreateTreeViewItem("三年六班统计", $hTvItem0)
;_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem02), $hParamTvItem2)
$hTvItem03 = GUICtrlCreateTreeViewItem("三年七班统计", $hTvItem0)
;_GUICtrlTreeView_SetItemParam($hTreeView0, _GUICtrlTreeView_GetItemHandle($hTreeView0, $hTvItem03), $hParamTvItem3)
$hTvItem1 = GUICtrlCreateTreeViewItem("四年段成绩统计", $hTreeView0)
GUICtrlCreateTreeViewItem("四年一班统计", $hTvItem1)
GUICtrlCreateTreeViewItem("四年二班统计", $hTvItem1)
$hTvItem02 = GUICtrlCreateTreeViewItem("工具", $hTreeView0)
$MenuItemd4 = GUICtrlCreateTreeViewItem("密码修改", $hTvItem02)
$MenuItemd5 = GUICtrlCreateTreeViewItem("备份数据库", $hTvItem02)
$MenuItemd6 = GUICtrlCreateTreeViewItem("桌面快捷方式", $hTvItem02)
$MenuItemd7 = GUICtrlCreateTreeViewItem("计算器", $hTvItem02)
$hTvItem3 = GUICtrlCreateTreeViewItem("系统设置目录", $hTreeView0)
GUICtrlCreateTreeViewItem("系统设置", $hTvItem3)
GUICtrlCreateTreeViewItem("系统优化", $hTvItem3)
GUICtrlCreateTreeViewItem("系统显示", $hTvItem3)
$MenuItem20 = GUICtrlCreateTreeViewItem("注册", $hTvItem3)
$MenuItem21 = GUICtrlCreateTreeViewItem("关于注册", $hTvItem3)
$MenuItem22 = GUICtrlCreateTreeViewItem("关于程序", $hTvItem3)
$MenuItem23 = GUICtrlCreateTreeViewItem("查看属性", $hTvItem3)
$MenuItem24 = GUICtrlCreateTreeViewItem("关闭", $hTvItem3)
Local $hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, 'shell32.dll', 137)
_GUICtrlTreeView_SetNormalImageList($hTreeView0, $hImage)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
;_GUICtrlTreeView_BeginUpdate($hTreeView0)
;_GUICtrlTreeView_EndUpdate($hTreeView0)
_GUICtrlTreeView_Expand($hTreeView0)
Local $idListview = GUICtrlCreateListView("col1|col2|col3", 240, 5, 200, 150) ;,$LVS_SORTDESCENDING)
Local $idItem1 = GUICtrlCreateListViewItem("item2|col22|col23", $idListview)
Local $idItem2 = GUICtrlCreateListViewItem("item1|col12|col13", $idListview)
Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|col33", $idListview)
$Edit1 = GUICtrlCreateEdit("", 240, 160, 300, 209)
GUICtrlSetData(-1, StringFormat("双击 工具或密码修改 项,读取出错,设置红色\r\n读取过的正常颜色 绿色\r\n当前读取项 蓝色,加粗,这个不知怎么搞"))
Global $CurrentTreeID = 0 ;当前打开的命名空间ID
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $hTreeView0
If Not IsHWnd($hTreeView0) Then $hWndTreeview = GUICtrlGetHandle($hTreeView0)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_DBLCLK ; 用户在控件中双击了鼠标左键,读取类
ConsoleWrite("上次ID:" & $CurrentTreeID)
GUICtrlSetState($CurrentTreeID, 0) ;取消上次命名空间加粗
;GUICtrlSetColor($CurrentTreeID, 0x000000) ;取消上次命名空间颜色
$CurrentTreeID = GUICtrlRead($hTreeView0) ;设定当前选择项ID
ConsoleWrite(",本次ID:" & $CurrentTreeID & @CRLF)
;$CurrentNamespacesHWnd = _GUICtrlTreeView_GetSelection($hTreeView0) ;设定当前选择项句柄
GUICtrlSetState($CurrentTreeID, 512) ;当前使用的命名空间加粗,成功读取后设置颜色
GUICtrlSetColor($CurrentTreeID, 0x0000C0)
DBLCLK(GUICtrlRead($hTreeView0, 1), $CurrentTreeID)
If @error Then
GUICtrlSetColor($CurrentTreeID, 0xFF0000)
Else
GUICtrlSetColor($CurrentTreeID, 0x008000)
EndIf
#CS Case $NM_CUSTOMDRAW
; Local $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
; Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
; Local $ItemParam = DllStructGetData($tCustDraw, 'ItemParam')
; If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
; Switch $ItemParam
; Case $hParamTvItem0
; DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ColorRoot)) ;根颜色
; Case $hParamTvItem1
; DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($UDFTextColor)) ;udf颜色
; Case $hParamTvItem2 ;$hTvItem2
; DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($ContrlTextColor)) ;控制颜色
; Case $hParamTvItem3
; DllStructSetData($tCustDraw, 'clrText', _RGB2BGR($CommentsTextColor)) ;注释颜色
; EndSwitch
; Return $CDRF_NEWFONT
#CE
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>_RGB2BGR
;双击目录树项
Func DBLCLK($TreeItemTexe, $TreeItemID)
ConsoleWrite("目录树项文本:" & $TreeItemTexe & ",ID:" & $TreeItemID & @CRLF)
If $TreeItemTexe = "工具" Or $TreeItemTexe = "密码修改" Then
SetError(1)
EndIf
EndFunc ;==>DBLCLK
本帖最后由 志艺风云 于 2018-7-24 21:10 编辑
现在我是在读取到错误那设置红色并设置一个全局变量,在下次双击时根据全局变量来判断是否设置绿色。
想不要全局变量,即如果能读取到上次那项是红色就不设置绿色。 你的代码不是已经达到了你的要求?如果不想设置全局变量.可以设置$CurrentTreeID为静态变量
; Global $CurrentTreeID = 0 ;当前打开的命名空间ID,这行不要
.
.
.
Case $NM_DBLCLK ; 用户在控件中双击了鼠标左键,读取类,下面加上一行
Local Static $CurrentTreeID
ConsoleWrite("上次ID:" & $CurrentTreeID)
志艺风云 发表于 2018-7-24 20:41
现在我是在读取到错误那设置红色并设置一个全局变量,在下次双击时根据全局变量来判断是否设置绿色。
想不 ...
设置(树视图)项目的粗体文本样式.
#include <GuiTreeView.au3>
_GUICtrlTreeView_SetBold ( $hWnd, $hItem [, $bFlag = True] )
参 数
$hWnd 控件 ID 或句柄
$hItem 项目句柄
$bFlag [可选] 如为 True, 则使用粗体文本, 否则为 False
_GUICtrlTreeView_SetBold($hTreeView0, $CurrentTreeID);加粗 本帖最后由 志艺风云 于 2018-7-25 14:56 编辑
tubaba 发表于 2018-7-24 21:20
你的代码不是已经达到了你的要求?如果不想设置全局变量.可以设置$CurrentTreeID为静态变量
; Globa ...
谢谢,你再看看这代码
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <WinAPIGdi.au3>
;#include <_DBUG.au3>
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("窗体1", 785, 401, 514, 375)
$TreeView1 = GUICtrlCreateTreeView(5, 5, 330, 305)
$TreeView1_0 = GUICtrlCreateTreeViewItem("设置", $TreeView1)
$TreeView1_1 = GUICtrlCreateTreeViewItem("双击这项1(出错,立即红色)", $TreeView1_0)
$TreeView1_2 = GUICtrlCreateTreeViewItem("双击这项2(出错,立即红色)", $TreeView1_1)
$TreeView1_3 = GUICtrlCreateTreeViewItem("双击这项3(正常,双击其它项后绿色)", $TreeView1_1)
$TreeView1_4 = GUICtrlCreateTreeViewItem("双击这项4(正常,双击其它项后绿色)", $TreeView1_1)
$TreeView1_5 = GUICtrlCreateTreeViewItem("双击这项5(正常,蓝色,加粗,最后双击项)", $TreeView1_1)
$TreeView1_6 = GUICtrlCreateTreeViewItem("其它项6(点击其它项后蓝色正常)", $TreeView1_1)
$TreeView1_7 = GUICtrlCreateTreeViewItem("其它项7(双击后点击文本框等蓝色不正常)", $TreeView1_1)
$ListView1 = GUICtrlCreateListView("列1|列2", 340, 5, 440, 150, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
$ListView1_0 = GUICtrlCreateListViewItem("11|12", $ListView1)
$Edit1 = GUICtrlCreateEdit("", 340, 160, 440, 150)
$Button1 = GUICtrlCreateButton("按钮1", 345, 320, 75, 25)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetTip(-1, "点击按钮取按钮文字颜色")
$Label1 = GUICtrlCreateLabel("按钮文字颜色,取值错误,不知如何获取DC。", 430, 325, 340, 17)
$Label2 = GUICtrlCreateLabel("蓝色不正常应该是需要设置选择项的前景色,这不管它了。" & @CRLF & "如何能做到不要$ComError全局变量。", 10, 320, 330, 27)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlTreeView_Expand($TreeView1)
Global $ComError = False ;设置一个执行错误时的全局变量
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$hDC = _WinAPI_GetWindowDC(GUICtrlGetHandle($Button1))
$TextColor = _WinAPI_GetTextColor($hDC) ;取值错误,不知如何获取DC
GUICtrlSetData($Label1, $TextColor)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $TreeView1
If Not IsHWnd($TreeView1) Then $hWndTreeview = GUICtrlGetHandle($TreeView1)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Local $tCustDraw = DllStructCreate($tagNMTVCUSTOMDRAW, $lParam)
Local $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')
Local $ItemParam = DllStructGetData($tCustDraw, 'ItemParam')
Local $hdc = DllStructGetData($tCustDraw, 'hdc') ;这两项不知是否正常
Local $clrText = DllStructGetData($tCustDraw, 'clrText') ;如果正常这个应该是此次的颜色了,不是上次的。
; If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW ;request custom drawing of items
Switch $iCode
Case $NM_DBLCLK ; 用户在控件中双击了鼠标左键
Local Static $CurrentTreeID ;上次双击的ID
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "上次ID:" & $CurrentTreeID)
GUICtrlSetState($CurrentTreeID, 0) ;取消上次加粗
If Not $ComError Then
GUICtrlSetColor($CurrentTreeID, 0x008000) ;读取正常时设置绿色颜色
Else
$ComError = False ;有错时恢复无错状态
EndIf
$CurrentTreeID = GUICtrlRead($TreeView1) ;设定当前选择项ID
GUICtrlSetState($CurrentTreeID, 512) ;当前双击项加粗
GUICtrlSetColor($CurrentTreeID, 0x0000C0) ;当前双击项蓝色
DBLCLK(GUICtrlRead($TreeView1, 1), $CurrentTreeID)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _RGB2BGR($iColor)
Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc ;==>_RGB2BGR
;双击目录树项
Func DBLCLK($TreeItemTexe, $CurrentTreeID)
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "本次文本:" & $TreeItemTexe & ",ID:" & $CurrentTreeID)
;这里实际为执行其它代码,最终可能会出错。
If $TreeItemTexe = "双击这项1(出错,立即红色)" Or $TreeItemTexe = "双击这项2(出错,立即红色)" Then
GUICtrlSetColor($CurrentTreeID, 0xFF0000) ;读取错误红色
$ComError = True ;设置错误全局变量
;SetError(1)
EndIf
EndFunc ;==>DBLCLK
页:
[1]
2