-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathYoteTextBox.cs
More file actions
93 lines (82 loc) · 2.93 KB
/
Copy pathYoteTextBox.cs
File metadata and controls
93 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Yotepad
{
public class YoteTextBox : TextBox
{
private const int WM_PASTE = 0x0302;
public bool IsOverwriteMode { get; private set; } = false;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE && Clipboard.ContainsText())
{
string text = Clipboard.GetText();
text = text.Replace("\r\n", "\n").Replace("\n", "\r\n");
this.SelectedText = text;
return;
}
base.WndProc(ref m);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
IsOverwriteMode = !IsOverwriteMode;
UpdateCaretAppearance();
e.Handled = true;
}
base.OnKeyDown(e);
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (IsOverwriteMode &&
this.SelectionLength == 0 &&
this.SelectionStart < this.TextLength &&
!char.IsControl(e.KeyChar))
{
char nextChar = this.Text[this.SelectionStart];
if (nextChar != '\r' && nextChar != '\n')
{
this.SelectionLength = 1;
}
}
base.OnKeyPress(e);
}
// The OS constantly tries to reset the caret to a line.
// We must reassert our block caret after these events.
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
UpdateCaretAppearance();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
UpdateCaretAppearance();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
UpdateCaretAppearance();
}
private void UpdateCaretAppearance()
{
if (IsOverwriteMode)
{
// Measure roughly how wide a character is in the current font
int width = TextRenderer.MeasureText("W", this.Font).Width / 2;
int height = this.Font.Height;
// Passing IntPtr.Zero creates a solid black/white inverted block
NativeMethods.CreateCaret(this.Handle, IntPtr.Zero, width, height);
NativeMethods.ShowCaret(this.Handle);
}
else
{
// Revert to a standard 1-pixel wide line caret
NativeMethods.CreateCaret(this.Handle, IntPtr.Zero, 1, this.Font.Height);
NativeMethods.ShowCaret(this.Handle);
}
}
}
}