@@ -7,6 +7,37 @@ import (
77 "github.com/lxn/win"
88)
99
10+ const (
11+ emSetCharFormat = win .WM_USER + 68
12+
13+ scfSelection = 0x0001
14+
15+ cfmColor = 0x40000000
16+ )
17+
18+ type charformat2W struct {
19+ cbSize uint32
20+ dwMask uint32
21+ dwEffects uint32
22+ yHeight int32
23+ yOffset int32
24+ crTextColor win.COLORREF
25+ bCharSet byte
26+ bPitchAndFam byte
27+ szFaceName [32 ]uint16
28+ wWeight uint16
29+ sSpacing int16
30+ crBackColor win.COLORREF
31+ lcid uint32
32+ dwReserved uint32
33+ sStyle int16
34+ wKerning uint16
35+ bUnderlineTyp byte
36+ bAnimation byte
37+ bRevAuthor byte
38+ bReserved1 byte
39+ }
40+
1041// RichEdit is a wrapper for the Win32 Rich Edit control.
1142// The class is provided by Msftedit.dll (RICHEDIT50W) or Riched20.dll (RichEdit20W).
1243type RichEdit struct {
@@ -75,6 +106,26 @@ func (re *RichEdit) AppendText(value string) {
75106 re .SetTextSelection (s , e )
76107}
77108
109+ // SetSelectionTextColor sets the selected text (and typing attributes) color.
110+ func (re * RichEdit ) SetSelectionTextColor (color win.COLORREF ) bool {
111+ cf := charformat2W {
112+ cbSize : uint32 (unsafe .Sizeof (charformat2W {})),
113+ dwMask : cfmColor ,
114+ crTextColor : color ,
115+ }
116+ ret := re .SendMessage (emSetCharFormat , scfSelection , uintptr (unsafe .Pointer (& cf )))
117+ return ret != 0
118+ }
119+
120+ // AppendTextColor appends text with a specific color.
121+ func (re * RichEdit ) AppendTextColor (value string , color win.COLORREF ) {
122+ l := re .TextLength ()
123+ re .SetTextSelection (l , l )
124+ _ = re .SetSelectionTextColor (color )
125+ re .ReplaceSelectedText (value , false )
126+ _ = re .SetSelectionTextColor (win .COLORREF (0 ))
127+ }
128+
78129// TextLength returns current text length.
79130func (re * RichEdit ) TextLength () int {
80131 return int (re .SendMessage (win .WM_GETTEXTLENGTH , 0 , 0 ))
0 commit comments