Changing colour in RichEdit
Changing colour in RichEdit
I have a read only RichEdit Gadget which is fed with text by program.
How can I manage that all new text from the insertion point appears in a different colour when I click on a button? The text before that point should remain in its colour.
How can I manage that all new text from the insertion point appears in a different colour when I click on a button? The text before that point should remain in its colour.
Well Freak, I have seen that before when I searched the forum, but that seems not to solve my problem. I know how to change the colour of selected text, but I have no selected text. I just want to change the colour of new text from the insertion point at a random time, but leave the colour of the old text unchanged.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Try this :
Code: Select all
Procedure SetColor(gadget,color)
format.CHARFORMAT2
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)
TextGadget(1, 80, 150, 200, 20, "Right-Click for Text Color options")
TextGadget(2, 10, 150, 50, 20, "Text Color")
CreatePopupMenu(0)
MenuItem(#Red, "Red")
MenuItem(#Yellow, "Yellow")
MenuItem(#Green, "Green")
MenuItem(#Black, "Black")
SetActiveGadget(0)
Repeat
EventID = WaitWindowEvent()
Select EventID
Case #WM_RBUTTONUP
DisplayPopupMenu(0, WindowID(0))
Case #PB_Event_Menu
SetColor(0, EventMenu())
SetGadgetColor(2, #PB_Gadget_FrontColor, EventMenu())
EndSelect
Until EventID = #PB_Event_CloseWindow
Well, in my application it doesn't work. The difference between my editor gadget and the one of netmaestro is, that mine is read only as I wrote in my first post. That seems to be the reason for the malfunction.
But before I came to PureBasic, I had the same application realized in Visual Basic, and there it worked. Since both build up on Windows, there must be a solution in PB as well. I will keep on trying.
But before I came to PureBasic, I had the same application realized in Visual Basic, and there it worked. Since both build up on Windows, there must be a solution in PB as well. I will keep on trying.
Something like this ?
Code: Select all
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_EXGETSEL, 0, @sel)
CopyMemory(@sel, @sel_saved, SizeOf(CHARRANGE))
sel\cpMax = -1
SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)
SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel_saved)
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)
AddGadgetItem(0, -1, "This is a stupid Text")
TextGadget(1, 80, 150, 200, 20, "Right-Click for Text Color options")
TextGadget(2, 10, 150, 50, 20, "Text Color")
CreatePopupMenu(0)
MenuItem(#Red, "Red")
MenuItem(#Yellow, "Yellow")
MenuItem(#Green, "Green")
MenuItem(#Black, "Black")
SetActiveGadget(0)
Repeat
Select WaitWindowEvent()
Case #WM_RBUTTONUP
DisplayPopupMenu(0, WindowID(0))
Case #PB_Event_Menu
SetColor(0, EventMenu())
SetGadgetColor(2, #PB_Gadget_FrontColor, EventMenu())
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
{Home}.:|:.{Dialog Design0R}.:|:.{Codes}.:|:.{History Viewer Online}.:|:.{Send a Beer}
Thanks, HeXOR, but this is again the problem, that I don't have selected text. Perhaps it is better to explain what I am doing. My application is a kind of radio teletype. From the receiver I get a string of characters, which are displayed in the RX editor, which by nature is read only. These characters are dark blue. When I transmit, the outgoing characters are monitored in the RX editor as well, and these I want to display in red. When receiving again, the color should change to dark blue again. The problem is to change the color from the position on, where new characters are appended.
With a slight adjustment...
Code: Select all
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)
Repeat
Delay(10)
SetColor(0, #Blue)
AddGadgetItem(0, -1, "Receiving...")
SetColor(0, #Red)
AddGadgetItem(0, -1, "Transmitting...")
Until WindowEvent()=#PB_Event_CloseWindow
I may look like a mule, but I'm not a complete ass.
Thanks srod for ur help. The code is working, but is not apllicable for me, since the received text does not come in linewise, but characterwise.
When I spend each character a line, it works, but makes no sense.
I have been trying many alternatives the last two days but did not come to a final solution.
I have wrote in previous posts that I don't have text to select. That's true for the moment I click on the transmit button, but when I am patient and wait a few milliseconds until the first transmit character appears, I have text to select. That's not the problem. The problem is now, that each time I change the selection, the color changes back to the default.
I experimented with 3 colors: green for received text, red for transmit text and blue as default.
When I start the application I am in receiving mode and the text appears in green. When I start transmitting, the receive text changes to default and the transmit text is red. When changing back to recieve, the transmit text changes to default as well, so that finally all text is blue.
So I give up now. This feature is not very important, since transmitting has leading and trailing lines 'TX-Start' and 'TX-End'.
Thanks for all who have replied.
When I spend each character a line, it works, but makes no sense.
I have been trying many alternatives the last two days but did not come to a final solution.
I have wrote in previous posts that I don't have text to select. That's true for the moment I click on the transmit button, but when I am patient and wait a few milliseconds until the first transmit character appears, I have text to select. That's not the problem. The problem is now, that each time I change the selection, the color changes back to the default.
I experimented with 3 colors: green for received text, red for transmit text and blue as default.
When I start the application I am in receiving mode and the text appears in green. When I start transmitting, the receive text changes to default and the transmit text is red. When changing back to recieve, the transmit text changes to default as well, so that finally all text is blue.
So I give up now. This feature is not very important, since transmitting has leading and trailing lines 'TX-Start' and 'TX-End'.
Thanks for all who have replied.
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
PB 5.21 LTS (x86) - Windows 8.1
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
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.
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)
Now I use the '#EM_REPLACESEL' message. It works fine and I don't need a callback function.
Aye it would do!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 wayThis command caused the loss of previous format settings.Code: Select all
SetGadgetText(Gadget, GetGadgetText(Gadget) + NewText)

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