Changing colour in RichEdit

Just starting out? Need help? Post your questions and find answers here.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

In theory you should be able to accomplish your task. Without seeing your code it's difficult to see what's going wrong. Here's some streaming text that changes color at each uppercase/lowercase character. Note that nothing is selected at the time of the color change.

Code: Select all

;Streaming code credit goes to El_Choni 
Global *InPtr, InText$

Procedure EditStreamCallback(dwCookie.l, *pbBuff.l, cb.l, *pcb.Long) 
  If dwCookie = 0
    length = MemoryStringLength(*InPtr) 
    If length > cb 
      CopyMemory(*InPtr, *pbBuff, cb) 
      *InPtr + cb 
      *pcb\l = cb 
    ElseIf length < = cb 
      CopyMemory(*InPtr, *pbBuff, length) 
      *InPtr + length 
      *pcb\l = length 
    EndIf 
  EndIf 
  ProcedureReturn #S_OK 
EndProcedure 

Procedure SetColor(Gadget, Color) 
  Protected format.CHARFORMAT2, sel.CHARRANGE, sel_saved.CHARRANGE 
  format\cbSize      = SizeOf(CHARFORMAT2) 
  format\dwMask      = #CFM_COLOR 
  format\crTextColor = Color 
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format) 
EndProcedure 

OpenWindow(0, 0, 0, 322, 170, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
EditorGadget(0, 8, 8, 306, 133, #PB_Editor_ReadOnly) 
SetActiveGadget(0) 
SetColor(0, #Blue) 
eStream.EDITSTREAM 
eStream\dwCookie = 0 
eStream\pfnCallback = @EditStreamCallback() 

Restore myData

For c = 1 To 46
  Read myChar.c
  InText$ =  Chr(myChar)
  *InPtr = @InText$
  Delay(100)
  If Asc(InText$) < 97
    SetColor(0, #Blue)
  Else
    SetColor(0, #Red)
  EndIf
  SendMessage_(GadgetID(0), #EM_STREAMIN, #SF_TEXT | #SFF_SELECTION, @eStream)
  UpdateWindow_(GadgetID(0))
Next c

Repeat 
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow 

End

DataSection
myData:
Data.s "The Lazy Brown Fox ", Chr(13), "Jumped Over Fred's Head"
EndDataSection
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
drahneir
Enthusiast
Enthusiast
Posts: 105
Joined: Tue Jul 18, 2006 4:18 pm
Location: JO42RM

Post by drahneir »

Yes, Sparkie, that's it. Thank you very much for your help. Now the color remains when leaving transmit. Well, that was a long birth, but finally it works. The way via streamin calback looks to me a bit complicated and I wouldn't have gone it on my own :lol: . Thanks again.
drahneir
Enthusiast
Enthusiast
Posts: 105
Joined: Tue Jul 18, 2006 4:18 pm
Location: JO42RM

Post by drahneir »

A final remark on this subject:
after some investigation I found out why it didn't work before. I appended new text the PureBasic way

Code: Select all

SetGadgetText(Gadget, GetGadgetText(Gadget) + NewText)
This command caused the loss of previous format settings.
Now I use the '#EM_REPLACESEL' message. It works fine and I don't need a callback function.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

drahneir wrote:A final remark on this subject:
after some investigation I found out why it didn't work before. I appended new text the PureBasic way

Code: Select all

SetGadgetText(Gadget, GetGadgetText(Gadget) + NewText)
This command caused the loss of previous format settings.
Aye it would do! :)

That explains things.
I may look like a mule, but I'm not a complete ass.
Post Reply