找回密码
 加入
搜索
查看: 1683|回复: 1

[GUI管理] 如何获得制定窗口某控件的文本颜色?

[复制链接]
发表于 2011-9-30 23:27:38 | 显示全部楼层 |阅读模式
如题:如何获得制定窗口某控件的文本颜色?请高人指教!!
发表于 2011-9-30 23:34:56 | 显示全部楼层
本帖最后由 gzh888666 于 2011-9-30 23:44 编辑

找了一段3笑的,自己修改吧!这个是背景,你修改成文本颜色就可以了!
#include <GUIConstantsEx.au3>

#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $WINDOW_TO_QUERY, $___RGBTC, $___RGBBGC
GUIRegisterMsg($WM_CTLCOLORSTATIC, "MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLOREDIT, "MY_CTLCOLOR")
GUIRegisterMsg($WM_CTLCOLORBTN, "MY_CTLCOLOR")

$hGui = GUICreate('Test', 200, 200)
GUISetBkColor(0xABCDEF)

$hButton = GUICtrlCreateButton("change", 20, 160, 40, 20)
$hButton_1 = GUICtrlCreateButton("msg", 120, 160, 40, 20)
$Input = GUICtrlCreateInput('Simple text', 20, 20, 160, 20)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0x0000FF)
GUISetState()

$Z = 1
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        ExitLoop
                Case $msg = $hButton
                        ;$color=0xABCDEF
                        If $Z = 1 Then
                                GUICtrlSetBkColor($Input, 0xABCDEF)
                                $Z = 0
                        Else
                                GUICtrlSetBkColor($Input, 0xADEFAC)
                                $Z = 1
                        EndIf
                Case $msg = $hButton_1
                        MsgBox(0, 'Colours in RGB format', 'Text colour input    = : 0x' & Hex(_WinAPI_GetTextColor($Input, 1), 6) & @CRLF & _
                                        'BackGnd colour input = : 0x' & Hex(_WinAPI_GetTextColor($Input), 6) & @CRLF & _
                                        'BackGnd colour GUI   = : 0x' & Hex(_WinAPI_GetBkColor($hGui), 6) & @CRLF)
        EndSelect
WEnd


; $BGC_TC = 0 for background colour,  $BGC_TC <> 0 for text colour
Func _WinAPI_GetTextColor($hWnd, $BGC_TC = 0)
        ; Prog@ndy
        Global $___RGBTC = -1, $___RGBBGC = -1
        If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
        $WINDOW_TO_QUERY = $hWnd
        _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_UPDATENOW)
        Do
                Sleep(10)
        Until $___RGBTC <> -1 And $___RGBBGC <> -1
        If $BGC_TC = 0 Then
                Return $___RGBBGC
        Else
                Return $___RGBTC
        EndIf
EndFunc   ;==>_WinAPI_GetTextColor

Func MY_CTLCOLOR($hWnd, $uMsg, $wParam, $lParam)
        ; Prog@ndy
        If $lParam = $WINDOW_TO_QUERY Then
                Local $RGB = DllCall('gdi32.dll', 'int', 'GetTextColor', 'ptr', $wParam)
                If Not @error Then
                        Global $___RGBTC = "0x" & StringRegExpReplace(Hex($RGB[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
                Else
                        Global $___RGBTC = -2
                EndIf
                Local $___RGBBGC = DllCall('gdi32.dll', 'int', 'GetBkColor', 'ptr', $wParam)
                If Not @error Then
                        Global $___RGBBGC = "0x" & StringRegExpReplace(Hex($___RGBBGC[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
                Else
                        Global $___RGBBGC = -2
                EndIf
        EndIf
        Return 'GUI_RUNDEFMSG'
EndFunc   ;==>MY_CTLCOLOR

; Get background colour of GUI window, NOT a control.
Func _WinAPI_GetBkColor($hWnd)
        ; Not Prog@ndy
        Local $aResult, $hDC, $Res
        If Not IsHWnd($hWnd) Then $hWnd = ControlGetHandle("", "", $hWnd)
        $hDC = _WinAPI_GetDC($hWnd)
        $aResult = DllCall("GDI32.dll", "int", "GetBkColor", "hwnd", $hDC)
        ConsoleWrite("Hex($aResult[0], 6) = " & Hex($aResult[0], 6) & @CRLF)
        $Res = "0x" & StringRegExpReplace(Hex($aResult[0], 6), "(.{2})(.{2})(.{2})", "\3\2\1")
        _WinAPI_ReleaseDC($hWnd, $hDC)
        Return $Res
EndFunc   ;==>_WinAPI_GetBkColor
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-9 18:39 , Processed in 0.143087 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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