kemyliu 发表于 2011-9-30 23:27:38

如何获得制定窗口某控件的文本颜色?

如题:如何获得制定窗口某控件的文本颜色?请高人指教!!

gzh888666 发表于 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, 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, 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, 6) = " & Hex($aResult, 6) & @CRLF)
      $Res = "0x" & StringRegExpReplace(Hex($aResult, 6), "(.{2})(.{2})(.{2})", "\3\2\1")
      _WinAPI_ReleaseDC($hWnd, $hDC)
      Return $Res
EndFunc   ;==>_WinAPI_GetBkColor

页: [1]
查看完整版本: 如何获得制定窗口某控件的文本颜色?