谢谢,你再看看这代码
#include
#include
没什么问题啊?
chzj589 发表于 2018-7-26 13:39
没什么问题啊?
如果不使用 $ComError 要怎么处理呢?
如果得到按钮文件的颜色红色呢? 本帖最后由 chzj589 于 2018-7-27 09:26 编辑
志艺风云 发表于 2018-7-26 18:40
如果不使用 $ComError 要怎么处理呢?
如果得到按钮文件的颜色红色呢?
1:你10楼的代码就没有使用 $ComError
2:按钮文件的颜色红色是设置的,不是随机获取的,为何? 志艺风云 发表于 2018-7-26 18:40
如果不使用 $ComError 要怎么处理呢?
如果得到按钮文件的颜色红色呢?
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
#include <GUIEdit.au3>
#include <ListViewConstants.au3>
#AutoIt3Wrapper_UseX64 = n
Global $TreeView1, $Edit1, $Button1, $Label1, $Label2, $CurrentTreeID;, $TreeItemTexe
#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, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL));, $ES_MULTILINE)
GUICtrlSetFont(-1, 11, 600, 0, '微软雅黑')
$Button1 = GUICtrlCreateButton("按钮1", 355, 320, 75, 25)
GUICtrlSetColor(-1, $COLOR_RED)
GUICtrlSetTip(-1, "点击按钮取按钮文字颜色")
$Label1 = GUICtrlCreateLabel("按钮文字颜色,取值错误,不知如何获取DC。", 435, 320, 340, 25)
GUICtrlSetFont($Label1, 9, 400, 0, '微软雅黑')
$Label2 = GUICtrlCreateLabel("蓝色不正常应该是需要设置选择项的前景色,这不管它了。" & @CRLF & "如何能做到不要$ComError全局变量。", 5, 320, 340, 47)
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
GUICtrlSetData($Label1, GUICtrlRead($Button1)& "--"&"红色")
GUICtrlSetColor($Label1, $COLOR_RED)
GUICtrlSetFont($Label1, 11, 600, 0, '微软雅黑')
GUICtrlSetFont($Label2, 10, 400, 0, '微软雅黑')
GUICtrlSetColor($Label2, 0x008000) ;$COLOR_RED)
DBLCLK(GUICtrlRead($TreeView1, 1), $CurrentTreeID)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $ilParam);$lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $TreeView1
If Not IsHWnd($TreeView1) Then $hWndTreeview = GUICtrlGetHandle($TreeView1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam);$lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $NM_DBLCLK ; 用户在控件中双击了鼠标左键
;Local Static $CurrentTreeID
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "上次ID:" & $CurrentTreeID)
$CurrentTreeID = GUICtrlRead($TreeView1) ;设定当前选择项ID
GUICtrlSetColor($CurrentTreeID, 0x0000C0)
_GUICtrlTreeView_SetBold($TreeView1, $CurrentTreeID);加粗
DBLCLK(GUICtrlRead($TreeView1, 1), $CurrentTreeID)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
;双击目录树项
Func DBLCLK($TreeItemTexe, $TreeItemID)
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "本次文本:" & $TreeItemTexe & ",ID:" & $CurrentTreeID)
;这里实际为执行其它代码,最终可能会出错。
If $TreeItemTexe = "双击这项1(出错,立即红色)" Or $TreeItemTexe = "双击这项2(出错,立即红色)" Or $TreeItemTexe = "双击这项3(正常,双击其它项后绿色)" Then
GUICtrlSetColor($CurrentTreeID, 0xFF0000)
$Ed1 = 0xFF0000;文本颜色
GUICtrlSetColor($Edit1, $Ed1);文本颜色
;SetError(1)
Else
GUICtrlSetColor($Edit1, 0x008000)
GUICtrlSetColor($CurrentTreeID, 0x008000)
EndIf
EndFunc ;==>DBLCLK
志艺风云 发表于 2018-7-26 18:40
如果不使用 $ComError 要怎么处理呢?
如果得到按钮文件的颜色红色呢?
使用 $ComError 设置错误全局变量,鼠标左键单击显示蓝色,鼠标左键双击显示绿色
#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, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL));, $ES_MULTILINE)
GUICtrlSetFont(-1, 11, 600, 0, '微软雅黑')
$Button1 = GUICtrlCreateButton("按钮1", 355, 320, 75, 25)
GUICtrlSetColor(-1, $COLOR_RED)
GUICtrlSetTip(-1, "点击按钮取按钮文字颜色")
$Label1 = GUICtrlCreateLabel("按钮文字颜色,取值错误,不知如何获取DC。", 435, 320, 340, 25)
GUICtrlSetFont($Label1, 9, 400, 0, '微软雅黑')
$Label2 = GUICtrlCreateLabel("蓝色不正常应该是需要设置选择项的前景色,这不管它了。" & @CRLF & "如何能做到不要$ComError全局变量。", 5, 320, 340, 47)
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
GUICtrlSetData($Label1, GUICtrlRead($Button1)& "--"&"红色")
GUICtrlSetColor($Label1, $COLOR_RED)
GUICtrlSetFont($Label1, 11, 600, 0, '微软雅黑')
GUICtrlSetFont($Label2, 10, 400, 0, '微软雅黑')
GUICtrlSetColor($Label2, 0x008000) ;$COLOR_RED)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $ilParam);$lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $TreeView1
If Not IsHWnd($TreeView1) Then $hWndTreeview = GUICtrlGetHandle($TreeView1)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam);$lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
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
_GUICtrlTreeView_SetBold($TreeView1, $CurrentTreeID);加粗
GUICtrlSetColor($CurrentTreeID, 0x0000C0) ;当前双击项蓝色
GUICtrlSetColor($Edit1, 0x0000FF)
DBLCLK(GUICtrlRead($TreeView1, 1), $CurrentTreeID)
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
;双击目录树项
Func DBLCLK($TreeItemTexe, $CurrentTreeID)
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "本次文本:" & $TreeItemTexe & ",ID:" & $CurrentTreeID)
;这里实际为执行其它代码,最终可能会出错。
If $TreeItemTexe = "双击这项1(出错,立即红色)" Or $TreeItemTexe = "双击这项2(出错,立即红色)" Or $TreeItemTexe = "双击这项3(正常,双击其它项后绿色)" Then
GUICtrlSetColor($CurrentTreeID, 0xFF0000) ;读取错误红色
GUICtrlSetColor($Edit1, 0xFF0000)
$ComError = True ;设置错误全局变量
Else
GUICtrlSetColor($Edit1, 0x008000)
EndIf
EndFunc ;==>DBLCLK
本帖最后由 志艺风云 于 2018-7-27 17:58 编辑
chzj589 发表于 2018-7-27 09:25
1:你10楼的代码就没有使用 $ComError
2:按钮文件的颜色红色是设置的,不是随机获取的,为何?
第10楼的代码不完善的,你没有理解我说的和我问的问题。
设置控件的文本颜色,GUICtrlSetColor(控件ID, 文本颜色)。设置控件的背景颜色,GUICtrlSetBkColor(控件ID, 背景色)
正常情况下应该有GUICtrlGetColor(控件ID)和GUICtrlGetBkColor(控件ID)来获取控件的文本颜色和背景颜色,可是没有这两函数。那么应该怎么才能获取到控件的这两颜色呢?
下面代码我把全局变量改成静态变量了,需然不满意,但也接受了。
你看代码 获取标签颜色 功能如何获取 $Label1 的文本颜色和背景颜色
#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", 786, 402, 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)
$Label1 = GUICtrlCreateLabel("如何取这标签文字背景和前景颜色?", 430, 325, 340, 17)
$Label2 = GUICtrlCreateLabel("蓝色不正常应该是需要设置选择项的前景色,这不管它了。" & @CRLF & "唔,实际是想管也不会,如更改可能是更改系统的选择项目时的颜色。", 10, 320, 325, 37)
$Button1 = GUICtrlCreateButton("更换标签颜色", 345, 320, 80, 25)
GUICtrlSetTip(-1, "更换颜色标签文字背景和前景颜色")
$Button2 = GUICtrlCreateButton("获取标签颜色", 345, 350, 80, 25)
$Input1 = GUICtrlCreateInput("标签文字背景颜色", 430, 350, 121, 21)
$Input2 = GUICtrlCreateInput("标签文字前景颜色", 560, 350, 121, 21)
$Input3 = GUICtrlCreateInput("标签文字背景颜色", 430, 375, 121, 21)
$Input4 = GUICtrlCreateInput("标签文字前景颜色", 560, 375, 121, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlTreeView_Expand($TreeView1)
Local $aColorCode =
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$TC = Random(0, 15, 1)
$BC = Random(0, 15, 1)
GUICtrlSetColor($Label1, $aColorCode[$TC])
GUICtrlSetBkColor($Label1, $aColorCode[$BC])
Case $Button2
$hWnd = GUICtrlGetHandle($Label1)
$hdc = _WinAPI_GetDC($hWnd)
$TextColor = _WinAPI_GetTextColor($hdc) ;取值错误,不知如何获取DC
$TextBKColor = _WinAPI_GetBkColor($hdc) ;取值错误,不知如何获取DC
GUICtrlSetData($Input1, $TextColor)
GUICtrlSetData($Input2, $TextBKColor)
_WinAPI_ReleaseDC($hWnd, $hdc)
$TextColor1 = GUICtrlRecvMsg($Label1, $COLOR_INFOTEXT) ;取值错误,不知消息代码是什么
;ConsoleWrite($TextColor1)
$TextBKColor1 = GUICtrlRecvMsg($Label1, $COLOR_INFOBK) ;取值错误,不知消息代码是什么
GUICtrlSetData($Input3, $TextColor1)
GUICtrlSetData($Input4, $TextBKColor1)
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
Local Static $NoError = True
GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & @CRLF & "上次ID:" & $CurrentTreeID)
GUICtrlSetState($CurrentTreeID, 0) ;取消上次加粗
If $NoError Then
GUICtrlSetColor($CurrentTreeID, 0x008000) ;读取正常时设置绿色颜色
EndIf
$CurrentTreeID = GUICtrlRead($TreeView1) ;设定当前选择项ID
GUICtrlSetState($CurrentTreeID, 512) ;当前双击项加粗
GUICtrlSetColor($CurrentTreeID, 0x0000C0) ;当前双击项蓝色
DBLCLK(GUICtrlRead($TreeView1, 1), $CurrentTreeID)
If @error Then
GUICtrlSetColor($CurrentTreeID, 0xFF0000) ;读取错误红色
$NoError = False ;有错时设置状态
Else
$NoError = True ;元错时设置状态
EndIf
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, "本次文本:" & $TreeItemTexe & ",ID:" & $CurrentTreeID, 1)
;这里实际为执行其它代码,最终可能会出错。
If $TreeItemTexe = "双击这项1(出错,立即红色)" Or $TreeItemTexe = "双击这项2(出错,立即红色)" Then
;GUICtrlSetColor($CurrentTreeID, 0xFF0000) ;读取错误红色
SetError(1) ;执行错误时设置错误码
EndIf
EndFunc ;==>DBLCLK
本帖最后由 chzj589 于 2018-7-28 15:05 编辑
志艺风云 发表于 2018-7-27 17:37
第10楼的代码不完善的,你没有理解我说的和我问的问题。
设置控件的文本颜色,GUICtrlSetColor(控件ID,...
是这样子?
chzj589 发表于 2018-7-28 14:49
是这样子?
对,就是如何获取控件的前背景色。 本帖最后由 chzj589 于 2019-6-28 21:50 编辑
志艺风云 发表于 2018-7-29 13:07
对,就是如何获取控件的前背景色。
http://www.autoitx.com/forum.php?mod=viewthread&tid=30775 志艺风云 发表于 2018-7-29 13:07
对,就是如何获取控件的前背景色。
问题还没解决?
那个例子的颜色是反的,修改一下就行了。
页:
1
[2]