|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
4 | 4 | using System.Drawing;
|
| 5 | +using Windows.Win32.Graphics.Direct2D; |
| 6 | +using Windows.Win32.Graphics.DirectWrite; |
5 | 7 |
|
6 | 8 | namespace Windows;
|
7 | 9 |
|
8 | 10 | public class TextLabelControl : Control
|
9 | 11 | {
|
10 | 12 | private static readonly WindowClass s_textLabelClass = new(className: "TextLabelClass");
|
11 | 13 |
|
12 |
| - private DrawTextFormat _textFormat; |
| 14 | + private DrawTextFormat _drawTextFormat; |
| 15 | + private TextFormat? _textFormat; |
| 16 | + private SolidColorBrush? _textBrush; |
| 17 | + private Color _textColor; |
13 | 18 |
|
14 | 19 | public TextLabelControl(
|
15 | 20 | Rectangle bounds = default,
|
16 |
| - DrawTextFormat textFormat = DrawTextFormat.Center | DrawTextFormat.VerticallyCenter, |
| 21 | + DrawTextFormat textFormat = DrawTextFormat.Center | DrawTextFormat.VerticallyCenter | DrawTextFormat.SingleLine, |
17 | 22 | string? text = default,
|
| 23 | + Color textColor = default, |
18 | 24 | WindowStyles style = WindowStyles.Overlapped | WindowStyles.Visible | WindowStyles.Child,
|
19 | 25 | ExtendedWindowStyles extendedStyle = ExtendedWindowStyles.Default,
|
20 | 26 | Window? parentWindow = default,
|
21 |
| - nint parameters = default) : base( |
| 27 | + nint parameters = default, |
| 28 | + Color backgroundColor = default, |
| 29 | + Features features = default) : base( |
22 | 30 | bounds,
|
23 | 31 | text,
|
24 | 32 | style,
|
25 | 33 | extendedStyle,
|
26 | 34 | parentWindow,
|
27 | 35 | s_textLabelClass,
|
28 |
| - parameters) |
| 36 | + parameters, |
| 37 | + backgroundColor: backgroundColor, |
| 38 | + features: features) |
29 | 39 | {
|
30 |
| - _textFormat = textFormat; |
| 40 | + _drawTextFormat = textFormat; |
| 41 | + TextColor = textColor; |
| 42 | + } |
| 43 | + |
| 44 | + public Color TextColor |
| 45 | + { |
| 46 | + get => _textColor; |
| 47 | + set |
| 48 | + { |
| 49 | + if (value.IsEmpty) |
| 50 | + { |
| 51 | + value = Color.Black; |
| 52 | + } |
| 53 | + |
| 54 | + if (value == _textColor) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + _textColor = value; |
| 60 | + if (_textBrush is { } brush) |
| 61 | + { |
| 62 | + brush.Color = _textColor; |
| 63 | + } |
| 64 | + |
| 65 | + this.Invalidate(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected override void RenderTargetCreated() |
| 70 | + { |
| 71 | + _textBrush?.Dispose(); |
| 72 | + _textBrush = RenderTarget.CreateSolidColorBrush(TextColor); |
| 73 | + base.RenderTargetCreated(); |
| 74 | + } |
| 75 | + |
| 76 | + private TextFormat GetTextFormat() |
| 77 | + { |
| 78 | + if (_textFormat is null) |
| 79 | + { |
| 80 | + HFONT hfont = this.GetFontHandle(); |
| 81 | + _textFormat = new TextFormat(hfont, _drawTextFormat); |
| 82 | + } |
| 83 | + |
| 84 | + return _textFormat; |
| 85 | + } |
| 86 | + |
| 87 | + protected override void OnPaint() |
| 88 | + { |
| 89 | + if (IsDirect2dEnabled()) |
| 90 | + { |
| 91 | + Size size = this.GetClientRectangle().Size; |
| 92 | + using TextLayout textLayout = new(Text, GetTextFormat(), size); |
| 93 | + Debug.Assert(_textBrush is not null); |
| 94 | + RenderTarget.DrawTextLayout(default, textLayout, _textBrush!); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + using var deviceContext = this.BeginPaint(); |
| 99 | + Rectangle bounds = this.GetClientRectangle(); |
| 100 | + deviceContext.DrawText(Text, bounds, _drawTextFormat, this.GetFontHandle(), TextColor); |
| 101 | + } |
| 102 | + |
| 103 | + base.OnPaint(); |
31 | 104 | }
|
32 | 105 |
|
33 | 106 | protected override LRESULT WindowProcedure(HWND window, MessageType message, WPARAM wParam, LPARAM lParam)
|
34 | 107 | {
|
35 | 108 | switch (message)
|
36 | 109 | {
|
37 |
| - case MessageType.Paint: |
| 110 | + case MessageType.SetFont: |
| 111 | + if (_textFormat is not null) |
38 | 112 | {
|
39 |
| - using var deviceContext = window.BeginPaint(out Rectangle paintBounds); |
40 |
| - using var selectionScope = deviceContext.SelectObject(this.GetFontHandle()); |
41 |
| - deviceContext.DrawText(Text, paintBounds, _textFormat); |
42 |
| - break; |
| 113 | + // The font isn't set until we call base, so we need to wait to recreate the text format. |
| 114 | + _textFormat.Dispose(); |
| 115 | + _textFormat = null; |
43 | 116 | }
|
| 117 | + |
| 118 | + break; |
44 | 119 | }
|
45 | 120 |
|
46 | 121 | return base.WindowProcedure(window, message, wParam, lParam);
|
47 | 122 | }
|
48 | 123 |
|
49 | 124 | public DrawTextFormat TextFormat
|
50 | 125 | {
|
51 |
| - get => _textFormat; |
| 126 | + get => _drawTextFormat; |
52 | 127 | set
|
53 | 128 | {
|
54 |
| - if (value == _textFormat) |
| 129 | + if (value == _drawTextFormat) |
55 | 130 | {
|
56 | 131 | return;
|
57 | 132 | }
|
58 | 133 |
|
59 |
| - _textFormat = value; |
| 134 | + _drawTextFormat = value; |
60 | 135 | this.Invalidate();
|
61 | 136 | }
|
62 | 137 | }
|
|
0 commit comments