curly
Full Member
Posts: 161
|
Post by curly on Apr 7, 2021 9:32:06 GMT -5
Hi All, me again! I want to change the color of some text in an open window?
I want to make the colour of text in a textbox red initially, and when something is satisfied, change it to green. I was going to ask if I could do that, but cleary I can't, but hopefully someone else can?
|
|
|
Post by Rod on Apr 7, 2021 10:52:55 GMT -5
Change in a window or change in a text box? Text box is easy just create an extra text box of the correct color then show or hide the correctly coloured one. You will need to make sure the contents are identical.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 7, 2021 13:31:17 GMT -5
Thanks Rod. To save me lots of try and see, can I set one group of textboxes to one colour, and another group to the other colour, or do I need to set the colour for each textbox individually?
|
|
|
Post by tenochtitlanuk on Apr 7, 2021 16:44:50 GMT -5
The code shown here flips visibility of two boxes at the same position- may help you see what to do..
nomainwin
WindowWidth =300 WindowHeight =170
texteditor #w.te1, 10, 10, 240, 80 texteditor #w.te2, 10, 10, 240, 80
global state: stete =0
open "Demo" for window as #w
#w "trapclose quit" #w "font Arial 24 bold"
#w.te1 "!forecolor red" #w.te1 "!backcolor cyan" #w.te1 " Text 1"
#w.te2 "!forecolor darkblue" #w.te2 "!backcolor yellow" #w.te2 " Text 2"
timer 100, [swapEm]
wait
[swapEm] state =state +1 #w.te1 "!cls" #w.te1 using( "#####.", state); " swaps" #w.te2 "!cls" #w.te2 using( "#####.", state); " swaps" if state mod 2 =0 then #w.te1 "!show": #w.te2 "!hide" if state mod 2 =1 then #w.te1 "!hide": #w.te2 "!show" wait
sub quit h$ close #h$ end end sub
|
|
|
Post by Rod on Apr 8, 2021 3:24:23 GMT -5
This sets out five textboxes and hides five more. The background changes if they have content.
nomainwin WindowWidth =300 WindowHeight =300 TextboxColor$="yellow" textbox #w.tb1, 10, 10, 240, 25 textbox #w.tb2, 10, 40, 240, 25 textbox #w.tb3, 10, 70, 240, 25 textbox #w.tb4, 10, 100, 240, 25 textbox #w.tb5, 10, 130, 240, 25 TextboxColor$="green" textbox #w.tb1r, 10, 10, 240, 25 textbox #w.tb2r, 10, 40, 240, 25 textbox #w.tb3r, 10, 70, 240, 25 textbox #w.tb4r, 10, 100, 240, 25 textbox #w.tb5r, 10, 130, 240, 25
button #w.b1,"Enter",[enter],UL,10,160,240,25 open "Demo" for window as #w #w "trapclose [quit]" #w.tb1r "!hide" #w.tb2r "!hide" #w.tb3r "!hide" #w.tb4r "!hide" #w.tb5r "!hide" wait
[enter] #w.tb1 "!contents? c$" if c$<>"" then #w.tb1 "!hide" : #w.tb1r "!show" : #w.tb1r c$ #w.tb2 "!contents? c$" if c$<>"" then #w.tb2 "!hide" : #w.tb2r "!show" : #w.tb2r c$ #w.tb3 "!contents? c$" if c$<>"" then #w.tb3 "!hide" : #w.tb3r "!show" : #w.tb3r c$ #w.tb4 "!contents? c$" if c$<>"" then #w.tb4 "!hide" : #w.tb4r "!show" : #w.tb4r c$ #w.tb5 "!contents? c$" if c$<>"" then #w.tb5 "!hide" : #w.tb5r "!show" : #w.tb5r c$ wait
[quit] close #w end
|
|
|
Post by Rod on Apr 8, 2021 3:27:24 GMT -5
To take full control needs some API tricks and is perhaps too complex for the job in hand.
Credit to John Davidson, a master at tweaking Liberty in his day.
nomainwin
'|---------- Red Text Controls ----------| ForegroundColor$="red" TextboxColor$="darkblue"
statictext #red.st1,"Red on darkblue", 10, 10, 100, 17 textbox #red.tb1,10, 30, 100, 25
stylebits #red,0,_WS_VISIBLE,0,0 open "Red window" for window_popup as #red
'|-------- green Text Controls -------| ForegroundColor$="darkgreen" TextboxColor$="green"
statictext #green.st1,"Darkgreen on green", 120, 10, 100, 17 textbox #green.tb1,120, 30, 100, 25
stylebits #green,0,_WS_VISIBLE,0,0 open "Green window" for window_popup as #green
'|-------- Normal Text Controls ---------|
'Open the mainGUI WindowWidth = 250 WindowHeight = 160 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
TextboxColor$="" 'default ForegroundColor$="" 'default
statictext #1.st1,"Normal", 10, 70, 50, 17 textbox #1.tb1,10, 90, 100, 25
open "Main GUI" for window as #1 print #1, "font ms_sans_serif 8" print #1, "trapclose [quit.1]"
'Move the controls from the 'color' windows to the main GUI. ' Parent Child call setParent hWnd(#1) , hWnd(#red.st1) call setParent hWnd(#1) , hWnd(#red.tb1) call setParent hWnd(#1) , hWnd(#green.st1) call setParent hWnd(#1) , hWnd(#green.tb1)
#red.tb1 "123" #green.tb1 "123" #1.tb1 "123" wait
[quit.1] close #green close #red close #1 END
SUB setParent parent,child calldll #user32, "SetParent",_ child as ulong,_ parent as ulong,_ r as long END SUB
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 8, 2021 3:35:30 GMT -5
Hi Rod and tenochtitlanuk, I've tried copying the code you propose, but it doesn't work. Maybe I'm getting too old? It all works without the last line, #home.tb101R, "!forecolor red"
TextboxColor$ = "Buttonface" Stylebits #home.tb101R, 0, _WS_BORDER, 0, 0 textbox #home.tb101R, 120,row(2), 80,20 #home.tb101R, "!forecolor red"
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 8, 2021 3:40:28 GMT -5
Hi Rod, Very sorry, your replies hadn't appeared before my last reply. I'll now try and understand what you've provided. (I wish I wasn't so old!)
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 8, 2021 13:11:23 GMT -5
Hi Rod, We've had the Grandchildren here today, so not much done software wise. However, I don't think I explained myself very well. What I am trying to achieve is a textbox that is the same colour of the background (buttonface) wth red text, with the ability to 'hide that textbox and replace it with another in the same place containing the same text, but in a different color. It is the color of the text only that I am trying to change?
|
|
|
Post by Rod on Apr 8, 2021 14:44:48 GMT -5
Well that would need the second set of API code. I would pick a different strategy as the API code is complex.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 9, 2021 3:25:26 GMT -5
Hi Rod, thank you very much. I have so far created small square coloured textboxes below the text that works, but circles would look better? Maybe that's beyond me? Kind regards, David
|
|
|
Post by Brandon Parker on Apr 9, 2021 8:48:06 GMT -5
Are you just trying to have some type of indicator for display purposes?
If so and you do not want to just use a GraphicBox, an easy way would be to use a BMPButton (so you can get a circular object) that has been Disabled. You could have text on the bitmap or simply use a StaticText off to the side.
{:0)
Brandon Parker
|
|
curly
Full Member
Posts: 161
|
Post by curly on Apr 9, 2021 9:16:14 GMT -5
Hi Brandon, Indicator is precisely what I'm looking for. The usercollects various lots of information, Client, Location of Job, Description of job, etc, using a menue bar in a gbox. Theindicator for each section starts at red, changes to amber when it has been started, and green when complete and validated. When all indicators are green, then the job can be signed off. The ideal would have been for each item of text to change color, but that appears somewhat beyond my expertise, Coloured textboxes hide the text, so as I have premised, I am currently looking at small disabled coloured square textboxes as indicators, but would prefer circles if possible?
I will do a search on Liberty Basic for BMPButton to see if I can make them work. Remember, my most recent programming experience was with Turbobasic in DOS! I am very aware I have learnt so much from you guys, and am very grateful. Than you all.
|
|
|
Post by Brandon Parker on Apr 9, 2021 20:20:38 GMT -5
If you are using a GraphicBox to do things already, you might want to extend that to draw your indicators as you see fit.
As for API stuff, here is an old example from Brent Thorn utilizing WMLiberty.dll to paint TextBoxes with custom colors which I edited some time ago for...someone...
It's not too terribly difficult, but it is involved for sure.
' Toggle TextBox Color Demo ' By Brent D. Thorn, 05/2008
' REQUIRES WINDOWS 2000 OR LATER. 'Edited by Brandon Parker for individual colors for each TextBox NoMainWin Global g.LastControl : g.LastControl = 3 Dim g.hwndAlerts(g.LastControl)
Open "WMLiberty" For DLL As #wmlib
WindowWidth = 256 WindowHeight = 130 StyleBits #demo, _DS_CENTER, 0, 0, 0
TextBox #demo.txt.1, 10, 10, 200, 20 Checkbox #demo.chk.1, "", demo.chk.Set, demo.chk.Reset, 220, 10, 20, 20 TextBox #demo.txt.2, 10, 40, 200, 20 Checkbox #demo.chk.2, "", demo.chk.Set, demo.chk.Reset, 220, 40, 20, 20 TextBox #demo.txt.3, 10, 70, 200, 20 Checkbox #demo.chk.3, "", demo.chk.Set, demo.chk.Reset, 220, 70, 20, 20
Open "Toggle Color Demo" For Dialog As #demo
#demo "TrapClose demo.Close" #demo "Font Microsoft_Sans_Serif 8" #demo.txt.1 "!SetFocus"
Callback lpfnCB, OnCtlColorEdit( ULong, ULong, ULong, ULong ), Long ret = SetWMHandler(HWnd(#demo), _WM_CTLCOLOREDIT, lpfnCB, GetStockObject(18))
Call DoEvents
Sub demo.Close wnd$ Close #wmlib Close #wnd$ End End Sub
Sub DoEvents [DoEvents] Scan CallDLL #kernel32, "Sleep", _ 50 As ULong, ret As Void GoTo [DoEvents] End Sub
Sub demo.chk.Set chk$ n = g.hwndAlerts(0) + 1 g.hwndAlerts(0) = n txt$ = Word$(chk$, 1, ".")+".txt."+Word$(chk$, 3, ".") hwnd = HWnd(#txt$) g.hwndAlerts(n) = hwnd Call EraseWindow hwnd End Sub
Sub demo.chk.Reset chk$ txt$ = Word$(chk$, 1, ".")+".txt."+Word$(chk$, 3, ".") hwnd = HWnd(#txt$) n = g.hwndAlerts(0) For i = 1 To n If hwnd = g.hwndAlerts(i) Then g.hwndAlerts(i) = 0 Sort g.hwndAlerts(), n, 1 Exit For End If Next g.hwndAlerts(0) = n - 1 Call EraseWindow hwnd End Sub
Function OnCtlColorEdit( hWnd, uMsg, hdcEdit, hwndEdit ) 'Give each Textbox some differing colors 'Should've seen the first iteration; had it flashing 'random colors on each key stroke and when checked/ unchecked..... Select Case hwndEdit Case Hwnd(#demo.txt.1) crFore = RGB(255, 122, 255) crBack = RGB(192, 168, 0) Case Hwnd(#demo.txt.2) crFore = RGB(102, 250, 124) crBack = RGB(185, 100, 25) Case Hwnd(#demo.txt.3) crFore = RGB(25, 95, 235) crBack = RGB(155, 115, 0) End Select
For i = 1 To g.hwndAlerts(0) If hwndEdit = g.hwndAlerts(i) Then found = 1 : Exit For Next
If Not( found ) Then Exit Function
CallDLL #gdi32, "SetTextColor", _ hdcEdit As ULong, _ crFore As ULong, _ ret As ULong
CallDLL #gdi32, "SetBkColor", _ hdcEdit As ULong, _ crBack As ULong, _ ret As ULong
hbrDCBrush = GetStockObject(18) 'DC_BRUSH
CallDLL #gdi32, "SelectObject", _ hdcEdit As ULong, _ hbrDCBrush As ULong, _ ret As ULong
CallDLL #gdi32, "SetDCBrushColor", _ hdcEdit As ULong, _ crBack As ULong, _ ret As ULong
OnCtlColorEdit = hbrDCBrush End Function
Function SetWMHandler( hWnd, uMsg, lpfnCB, lSuccess ) CallDLL #wmlib, "SetWMHandler", _ hWnd As ULong, _ uMsg As ULong, _ lpfnCB As ULong, _ lSuccess As Long, _ SetWMHandler As Long End Function
Function GetStockObject( nObject ) calldll #gdi32, "GetStockObject", _ nObject As Long, _ GetStockObject As ULong End Function
function RGB( R, G, B ) RGB = R + 256 * G + 65536 * B end function
Sub EraseWindow hWnd flags = _RDW_ERASE Or _RDW_INVALIDATE CallDLL #user32, "RedrawWindow", _ hWnd as ulong, _ _NULL as ulong, _ _NULL as ulong, _ flags as ulong, _ ret as long End Sub
{:0)
Brandon Parker
|
|
|
Post by Chris Iverson on Apr 11, 2021 4:48:03 GMT -5
Wanted to learn a bit more about playing around with window messages and subclassing, so I built a DLL to handle changing textbox colors in LB. The way the DLL is designed, it should work for any number of textboxes on any number of windows in your program. I've attached the DLL to this post, and full demo code is below. The DLL's source code is available at github.com/iversc/LBTBColor/. open "LBTBColor.dll" for DLL as #lbtbc
'Excel Good/Bad/Neutral cell colors goodTextClr = RGB( 0, 97, 0) goodBGClr = RGB(198, 239, 206)
badTextClr = RGB(156, 0, 6) badBGClr = RGB(255, 199, 206)
neutralTextClr = RGB(156, 87, 0) neutralBGClr = RGB(255, 235, 156)
nomainwin
[setup.m.Window] '-----Begin code for #m
WindowWidth = 450 WindowHeight = 415 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
'-----Begin GUI objects code
groupbox #m.groupbox6, "Custom Colors", 10, 142, 415, 220 TextboxColor$ = "white" textbox #m.tbGood, 10, 12, 100, 25 textbox #m.tbNeutral, 160, 12, 100, 25 textbox #m.tbBad, 315, 12, 100, 25 textbox #m.tbCustom, 10, 107, 415, 25 statictext #m.statictext8, "Text Red:", 20, 172, 58, 20 textbox #m.tbCusTextRed, 100, 167, 100, 25 statictext #m.statictext10, "Text Green:", 20, 207, 69, 20 statictext #m.statictext11, "Text Blue:", 20, 242, 59, 20 textbox #m.tbCusTextGreen, 100, 202, 100, 25 textbox #m.tbCusTextBlue, 100, 237, 100, 25 statictext #m.statictext14, "BG Red:", 215, 172, 51, 20 statictext #m.statictext15, "BG Green:", 215, 207, 62, 20 statictext #m.statictext16, "BG Blue:", 215, 242, 52, 20 textbox #m.tbCusBGRed, 285, 167, 100, 25 textbox #m.tbCusBGGreen, 285, 202, 100, 25 textbox #m.tbCusBGBlue, 285, 237, 100, 25 button #m.button20,"Set Custom Textbox Colors",[setColors], UL, 30, 287, 180, 25 button #m.button21,"Reset Custom Textbox",[resetColors], UL, 230, 287, 170, 25 button #m.button22,"Set Custom Text Color Only",[setTextColor], UL, 30, 322, 179, 25 button #m.button23,"Set Custom BG Color Only",[setBGColor], UL, 230, 322, 172, 25
'-----End GUI objects code
open "Test Textbox Colors" for window as #m print #m, "font ms_sans_serif 10" print #m, "trapclose [quit.m]"
#m.tbCusTextRed, "0" #m.tbCusTextGreen, "0" #m.tbCusTextBlue, "0" #m.tbCusBGRed, "255" #m.tbCusBGGreen, "255" #m.tbCusBGBlue, "255"
#m.tbGood, "Good" #m.tbNeutral, "Neutral" #m.tbBad, "Bad" #m.tbCustom, "Custom"
hTBGood = hwnd(#m.tbGood) hTBNeutral = hwnd(#m.tbNeutral) hTBBad = hwnd(#m.tbBad) hTBCustom = hwnd(#m.tbCustom)
a = SetTextboxColors(hTBGood, goodTextClr, goodBGClr) a = SetTextboxColors(hTBNeutral, neutralTextClr, neutralBGClr) a = SetTextboxTextColor(hTBBad, badTextClr) a = SetTextboxBGColor(hTBBad, badBGClr)
[m.inputLoop] 'wait here for input event wait
[setColors] #m.tbCusTextRed, "!contents? cusTextRed" #m.tbCusTextGreen, "!contents? cusTextGreen" #m.tbCusTextBlue, "!contents? cusTextBlue" #m.tbCusBGRed, "!contents? cusBGRed" #m.tbCusBGGreen, "!contents? cusBGGreen" #m.tbCusBGBlue, "!contents? cusBGBlue"
a = SetTextboxColorsRGB(hTBCustom, cusTextRed, cusTextGreen, cusTextBlue,_ cusBGRed, cusBGGreen, cusBGBlue) wait
[resetColors] a = ResetTextboxColors(hTBCustom) wait
[setTextColor] #m.tbCusTextRed, "!contents? cusTextRed" #m.tbCusTextGreen, "!contents? cusTextGreen" #m.tbCusTextBlue, "!contents? cusTextBlue"
a = SetTextboxTextColorRGB(hTBCustom, cusTextRed, cusTextGreen, cusTextBlue) wait
[setBGColor] #m.tbCusBGRed, "!contents? cusBGRed" #m.tbCusBGGreen, "!contents? cusBGGreen" #m.tbCusBGBlue, "!contents? cusBGBlue"
a = SetTextboxBGColorRGB(hTBCustom, cusBGRed, cusBGGreen, cusBGBlue) wait
[quit.m] 'End the program close #m close #lbtbc end
Function RGB(r, g, b) RGB = ((b AND 255) * 65536) + ((g AND 255) * 256) + (r AND 255) End Function
Function SetTextboxColors(hTB, textColor, bgColor) CallDLL #lbtbc, "SetTextboxColors",_ hTB as ulong, textColor as long,_ bgColor as long, SetTextboxColors as long End Function
Function SetTextboxColorsRGB(hTB, textR, textG, textB, bgR, bgG, bgB) CallDLL #lbtbc, "SetTextboxColorsRGB",_ hTB as ulong, textR as long, textG as long,_ textB as long, bgR as long, bgG as long,_ bgB as long, SetTextboxColorsRGB as long End Function
Function SetTextboxTextColor(hTB, textColor) CallDLL #lbtbc, "SetTextboxTextColor",_ hTB as ulong, textColor as long,_ SetTextboxTextColor as long End Function
Function SetTextboxTextColorRGB(hTB, textR, textG, textB) CallDLL #lbtbc, "SetTextboxTextColorRGB",_ hTB as ulong, textR as long, textG as long,_ textB as long, SetTextboxTextColorRGB as long End Function
Function SetTextboxBGColor(hTB, bgColor) CallDLL #lbtbc, "SetTextboxBGColor",_ hTB as ulong, bgColor as long,_ SetTextboxBGColor as long End Function
Function SetTextboxBGColorRGB(hTB, bgR, bgG, bgB) CallDLL #lbtbc, "SetTextboxBGColorRGB",_ hTB as ulong, bgR as long, bgG as long,_ bgB as long, SetTextboxBGColorRGB as long End Function
Function ResetTextboxColors(hTB) CallDLL #lbtbc, "ResetTextboxColors",_ hTB as ulong, ResetTextboxColors as long End Function Attachments:LBTBColor.dll (12 KB)
|
|