Jump to content

Can't change text color in a label


klpc
 Share

Recommended Posts

Hi

I have several windows (let's say 3) with 13 labels each.

I can change text of a label knowing the window handle and it's instance number in the window, but I can't change text label color.

Does anybody have an idea on how to do that ?

Local $hWnd = WinGetHandle("Title of window 2")
ControlSetText($hWnd, "", "Static5", "Text for label 5") ; <=== This works

To change text color I suppose I have to use GUICtrlSetColor.

GUICtrlSetColor works with a ControlId.

How to get the controlId of label 5 in window 2 ?

All examples work if label has just been created. This is not the case for me.

Best regards

Link to comment
Share on other sites

  • Developers
10 minutes ago, klpc said:

To change text color I suppose I have to use GUICtrlSetColor.

No, not really, this is to change the color of your own generated gui in the script, nother for a control of another App's control.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The 3 windows are created with GUICreate in the same app. It's not another app.

$hGUI = GUICreate("Title of window 1", 100, 100, 100, 100)

... then I create 13 labels

$hGUI = GUICreate("Title of window 2", 100, 100, 250, 100)

... then I create 13 labels

$hGUI = GUICreate("Title of window 3", 100, 100, 500, 100)

... then I create 13 labels

Then I want to change text and color of a label of my choice among all these labels

Link to comment
Share on other sites

  • Developers

Why use ControlSetText() when it is your own window with controls?

Anyway: show us the script that has the issue so we can check what is happening. :)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <FontConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

Example()
;==========================================================================================
Func Example()
   For $iNum=1 To 3
      CreateNewGui($iNum)
   Next
   Sleep(5000)
   DisplayStats(3, CalculateStats())
   DisplayStats(1, CalculateStats())
   DisplayStats(2, CalculateStats())
   MsgBox($MB_OK,"", "CR to continue ...")
EndFunc
;==========================================================================================
Func CreateNewGui($iNum)
   Local $hGUI = GUICreate("Title of window " & $iNum, 200, 100, 250 * $iNum, 100)
   CreateNewLabels($hGui)
   GUISetState(@SW_SHOW)
EndFunc
;==========================================================================================
Func CreateNewLabels($hGui)
   For $i = 0 To 3
      Local $iInt = Int($i/2)
      Local $iMod = Mod($i,2)
      $hLbl = GUICtrlCreateLabel($i, 15 + $iInt * 90, 15 + $iMod * 30, 80, 20)
      GUICtrlSetFont($hLbl, 12, $FW_BOLD, $GUI_FONTNORMAL, "Arial")
      GUICtrlSetColor($hLbl, $COLOR_WHITE)
      GUICtrlSetBkColor($hLbl, 0x000000)
   Next
EndFunc
;==========================================================================================
Func CalculateStats()
   Local $aInfos[4]
   $aInfos[0] = Random(0, 1000, 1)
   $aInfos[1] = Random(0, 1000, 1)
   $aInfos[2] = Random(0, 1000, 1)
   $aInfos[3] = Random(0, 1000, 1)
   Return $aInfos
EndFunc
;==========================================================================================
Func DisplayStats($iNum, $aInfos)
   Local $hGui = WinGetHandle("Title of window " & $iNum)
   For $i = 0 To 3
      ControlSetText($hGui, "", "Static" & ($i+1), StringFormat("%8s",$aInfos[$i]))
      Select
      Case $aInfos[$i] < 333
         GUICtrlSetColor("Static" & ($i+1), $COLOR_RED)    ; <=== Doesn't work !
      Case $aInfos[$i] < 666
         GUICtrlSetColor("Static" & ($i+1), $COLOR_MAROON) ; <=== Doesn't work !
      Case Else
         GUICtrlSetColor("Static" & ($i+1), $COLOR_GREEN)  ; <=== Doesn't work !
      EndSelect
   Next
EndFunc

Here is an example

Link to comment
Share on other sites

Ah I see your issue you need a controlID 

I've altered your script to give you the controlID but this is definitely NOT  the way to do this as far as speed is concerned

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <FontConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

#include <WinAPI.au3>

Example()
;==========================================================================================
Func Example()
    For $iNum = 1 To 3
        CreateNewGui($iNum)
    Next
    Sleep(5000)
    DisplayStats(3, CalculateStats())
    DisplayStats(1, CalculateStats())
    DisplayStats(2, CalculateStats())
    MsgBox($MB_OK, "", "CR to continue ...")
EndFunc   ;==>Example
;==========================================================================================
Func CreateNewGui($iNum)
    Local $hGUI = GUICreate("Title of window " & $iNum, 200, 100, 250 * $iNum, 100)
    CreateNewLabels($hGUI)
    GUISetState(@SW_SHOW)
EndFunc   ;==>CreateNewGui
;==========================================================================================
Func CreateNewLabels($hGUI)
    For $i = 0 To 3
        Local $iInt = Int($i / 2)
        Local $iMod = Mod($i, 2)
        $hLbl = GUICtrlCreateLabel($i, 15 + $iInt * 90, 15 + $iMod * 30, 80, 20)
        GUICtrlSetFont($hLbl, 12, $FW_BOLD, $GUI_FONTNORMAL, "Arial")
        GUICtrlSetColor($hLbl, $COLOR_WHITE)
        GUICtrlSetBkColor($hLbl, 0x000000)
    Next
EndFunc   ;==>CreateNewLabels
;==========================================================================================
Func CalculateStats()
    Local $aInfos[4]
    $aInfos[0] = Random(0, 1000, 1)
    $aInfos[1] = Random(0, 1000, 1)
    $aInfos[2] = Random(0, 1000, 1)
    $aInfos[3] = Random(0, 1000, 1)
    Return $aInfos
EndFunc   ;==>CalculateStats
;==========================================================================================
Func DisplayStats($iNum, $aInfos)
    Local $hGUI = WinGetHandle("Title of window " & $iNum)
    Local $hCtrl, $idCtrl
    For $i = 0 To 3
        $hCtrl = ControlGetHandle($hGUI, "", "[ClassNN:Static" & ($i + 1) & "]")
        If $hCtrl Then
            $idCtrl = _WinAPI_GetDlgCtrlID($hCtrl)
            ConsoleWrite("$hCtrl = " & $hCtrl & @CRLF)
            ControlSetText($hGUI, "", $idCtrl, StringFormat("%8s", $aInfos[$i]))
            Select
                Case $aInfos[$i] < 333
                    GUICtrlSetColor($idCtrl, $COLOR_RED) ; <=== Doesn't work !
                Case $aInfos[$i] < 666
                    GUICtrlSetColor($idCtrl, $COLOR_MAROON) ; <=== Doesn't work !
                Case Else
                    GUICtrlSetColor($idCtrl, $COLOR_GREEN) ; <=== Doesn't work !
            EndSelect
        Else
            MsgBox(0, "Error", "Error getting handle " & @error)
        EndIf
    Next
EndFunc   ;==>DisplayStats

Instead you could record the ID as they are created to a Global Array Like This..

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <FontConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

#include <WinAPI.au3>

Global $g_Label[32] = [0]; You can have an exact number here or just a resonable amount and Record the count as you add them

Example()
;==========================================================================================
Func Example()
    For $iNum = 1 To 3
        CreateNewGui($iNum)
    Next
    Sleep(5000)
    DisplayStats(3, CalculateStats())
    DisplayStats(1, CalculateStats())
    DisplayStats(2, CalculateStats())
    MsgBox($MB_OK, "", "CR to continue ...")
EndFunc   ;==>Example
;==========================================================================================
Func CreateNewGui($iNum)
    Local $hGUI = GUICreate("Title of window " & $iNum, 200, 100, 250 * $iNum, 100)
    CreateNewLabels($hGUI)
    GUISetState(@SW_SHOW)
EndFunc   ;==>CreateNewGui
;==========================================================================================
Func CreateNewLabels($hGUI)
    For $i = 0 To 3
        Local $iInt = Int($i / 2)
        Local $iMod = Mod($i, 2)
        $hLbl = GUICtrlCreateLabel($i, 15 + $iInt * 90, 15 + $iMod * 30, 80, 20)
        GUICtrlSetFont($hLbl, 12, $FW_BOLD, $GUI_FONTNORMAL, "Arial")
        GUICtrlSetColor($hLbl, $COLOR_WHITE)
        GUICtrlSetBkColor($hLbl, 0x000000)
        $g_Label[0] += 1
        $g_Label[$g_Label[0]] = $hLbl
    Next
EndFunc   ;==>CreateNewLabels
;==========================================================================================
Func CalculateStats()
    Local $aInfos[4]
    $aInfos[0] = Random(0, 1000, 1)
    $aInfos[1] = Random(0, 1000, 1)
    $aInfos[2] = Random(0, 1000, 1)
    $aInfos[3] = Random(0, 1000, 1)
    Return $aInfos
EndFunc   ;==>CalculateStats
;==========================================================================================
Func DisplayStats($iNum, $aInfos)
    Local $hGUI = WinGetHandle("Title of window " & $iNum)
    Local $j = ($iNum - 1) * 4 + 1, $idCtrl
    ConsoleWrite("#" & $iNum & " Starting Label = " & $j & @CRLF)
    For $i = 0 To 3
        If $j <= $g_Label[0] Then
            ConsoleWrite("#" & $iNum & " Current Label = " & $j & @CRLF)
            $idCtrl = $g_Label[$j]

            ControlSetText($hGUI, "", $idCtrl, StringFormat("%8s", $aInfos[$i]))
            Select
                Case $aInfos[$i] < 333
                    GUICtrlSetColor($idCtrl, $COLOR_RED) ; <=== Doesn't work !
                Case $aInfos[$i] < 666
                    GUICtrlSetColor($idCtrl, $COLOR_MAROON) ; <=== Doesn't work !
                Case Else
                    GUICtrlSetColor($idCtrl, $COLOR_GREEN) ; <=== Doesn't work !
            EndSelect
        Else
            MsgBox(0, "Error", "Error getting handle " & @error)
        EndIf
        $j += 1
    Next
EndFunc   ;==>DisplayStats

You could also hold the CtrlIds just like you did $aInfos[] or even as another dimension of $aInfos[1][$i] I just didn't because frankly I didn't feel like all that typing when some math would suffice

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...