Skip to content

Commit 95e9df1

Browse files
committed
RichEdit: append random colored text demo
1 parent 8c80864 commit 95e9df1

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

examples/basic/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"log"
6+
"math/rand"
67
"os"
78
"strconv"
89
"sync/atomic"
@@ -14,6 +15,7 @@ import (
1415

1516
func init() {
1617
log.SetOutput(os.Stdout)
18+
rand.Seed(time.Now().UnixNano())
1719
}
1820

1921
var dlg *wingui.Dialog
@@ -349,7 +351,11 @@ func bindWidgets(dlg *wingui.Dialog) {
349351

350352
appendBtn, _ := wingui.BindNewButton(IDC_RICH_APPEND, richPage)
351353
appendBtn.OnClicked = func() {
352-
rich.AppendText(time.Now().Format("15:04:05") + " hello rich edit\r\n")
354+
r := uint32(60 + rand.Intn(160))
355+
g := uint32(60 + rand.Intn(160))
356+
b := uint32(60 + rand.Intn(160))
357+
color := win.COLORREF(r | (g << 8) | (b << 16))
358+
rich.AppendTextColor(time.Now().Format("15:04:05")+" hello rich edit\r\n", color)
353359
updateRichLabel()
354360
}
355361
clearBtn, _ := wingui.BindNewButton(IDC_RICH_CLEAR, richPage)

richedit.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
1243
type 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.
79130
func (re *RichEdit) TextLength() int {
80131
return int(re.SendMessage(win.WM_GETTEXTLENGTH, 0, 0))

0 commit comments

Comments
 (0)