Hallo,
ist es möglich das komplett Designe einer TextBox zu ändern?
Warum ich das frage? Ich hab jetzt kanpp 4h veruscht die TextBoxBase auf mein Control zu programmieren damit ich die Mechanik des Textschreibens bekomme, aber ohne erfolg.
Nun dachte ich okey, evtl. mit C++ aber dort ist in den Videos das gleiche Spiel. Alle TextBoxen sehen Grau und Viereckig aus -.-
Wie es Momentan aussieht bei mir
Spoiler anzeigen
Mein Code bis Jetzt
ist es möglich das komplett Designe einer TextBox zu ändern?
Warum ich das frage? Ich hab jetzt kanpp 4h veruscht die TextBoxBase auf mein Control zu programmieren damit ich die Mechanik des Textschreibens bekomme, aber ohne erfolg.
Nun dachte ich okey, evtl. mit C++ aber dort ist in den Videos das gleiche Spiel. Alle TextBoxen sehen Grau und Viereckig aus -.-
Wie es Momentan aussieht bei mir
Mein Code bis Jetzt
C#-Quellcode
-
- class CueTextBox : Control {
- /*
- * Text
- * Courser
- * KeyDown Hold
- */
- #region Propertys
- private string _Text;
- public new string Text {
- get => _Text;
- set => PropertyChange(ref _Text, value, () => {
- BeforeTextChanged?.Invoke(this, CursorPosition);
- this.Invalidate();
- AfterTextChanged?.Invoke(this, CursorPosition);
- });
- }
- private Padding _TextPadding;
- public Padding TextPadding { get => _TextPadding; set => PropertyChange(ref _TextPadding, value, Invalidate); }
- private string _CueText;
- public string CueText {
- get => _CueText;
- set => PropertyChange(ref _CueText, value, Invalidate);
- }
- private Color _CueColor;
- public Color CueColor {
- get => _CueColor;
- set => PropertyChange(ref _CueColor, value, Invalidate);
- }
- private ContentAlignment _ContentAlignment;
- public new ContentAlignment ContentAlignment {
- get => _ContentAlignment;
- set => PropertyChange(ref _ContentAlignment, value, Invalidate);
- }
- private Color _LineColor;
- public Color LineColor {
- get => _LineColor;
- set => PropertyChange(ref _LineColor, value, Invalidate);
- }
- private int _LineHeight;
- public int LineHeight {
- get => _LineHeight;
- set => PropertyChange(ref _LineHeight, value, Invalidate);
- }
- private int _LineMarginLeft;
- public int LineMarginLeft {
- get => _LineMarginLeft;
- set => PropertyChange(ref _LineMarginLeft, value, () => {
- LineMarginHorizontal = -1;
- _LineMarginLeft = value;
- });
- }
- private int _LineMarginRight;
- public int LineMarginRight {
- get => _LineMarginRight;
- set => PropertyChange(ref _LineMarginRight, value, () => {
- LineMarginHorizontal = -1;
- _LineMarginRight = value;
- });
- }
- private int _LineMarginHorizontal;
- public int LineMarginHorizontal {
- get => _LineMarginHorizontal;
- set => PropertyChange(ref _LineMarginHorizontal, value, () => {
- if (value != -1) {
- LineMarginLeft = value;
- LineMarginRight = value;
- }
- Invalidate();
- });
- }
- private int _LineMarginBotton;
- public int LineMarginBotton {
- get => _LineMarginBotton;
- set => PropertyChange(ref _LineMarginBotton, value, Invalidate);
- }
- private Color _ForeColor;
- public new Color ForeColor {
- get => _ForeColor;
- set => PropertyChange(ref _ForeColor, value, Invalidate);
- }
- private Color _BackColor;
- public new Color BackColor {
- get => _BackColor;
- set => PropertyChange(ref _BackColor, value, Invalidate);
- }
- private int _CursorSpeed;
- public int CursorSpeed {
- get => _CursorSpeed;
- set => PropertyChange(ref _CursorSpeed, value, Invalidate);
- }
- private int _CourserWidth;
- public int CursorWidth {
- get => _CourserWidth;
- set => PropertyChange(ref _CourserWidth, value, Invalidate);
- }
- private int _CursorPosition;
- public int CursorPosition {
- get => _CursorPosition;
- set => PropertyChange(ref _CursorPosition, value, Invalidate);
- }
- private Font _Font;
- public new Font Font {
- get => _Font;
- set => PropertyChange(ref _Font, value, Invalidate);
- }
- #endregion
- public event Action<Object> MarginChange;
- public event Action<Object, int> BeforeTextChanged;
- public event Action<Object, int> AfterTextChanged;
- public CueTextBox() {
- ControlStyles styles = ControlStyles.UserPaint
- | ControlStyles.OptimizedDoubleBuffer
- | ControlStyles.SupportsTransparentBackColor
- | ControlStyles.ResizeRedraw
- | ControlStyles.UseTextForAccessibility
- | ControlStyles.Selectable;
- SetStyle(styles, true);
- //Font = Parent.Font;
- }
- //Test
- bool select;
- bool usercursorSet;
- //
- protected override void OnClick(EventArgs e) {
- select = true;
- }
- protected override void OnKeyDown(KeyEventArgs e) {
- string letter = e.KeyCode.ToString();
- if (Regex.IsMatch(letter, "^(?:\\w|\\s|\\d){0,1}$")) {
- if (string.IsNullOrEmpty(Text)) {
- Text = letter;
- CursorPosition = Text.Length;
- } else {
- Text = string.Concat(Text, letter);
- CursorPosition = Text.Length;
- }
- Console.WriteLine(Text);
- Invalidate();
- Refresh();
- }
- }
- protected override void OnPaint(PaintEventArgs e) {
- Graphics graphics = e.Graphics;
- Rectangle rect = Bounds;
- #region UnderLine
- Pen linePen = new Pen(LineColor, LineHeight);
- int lineXStart = LineMarginLeft;
- int lineXEnd = rect.Width - LineMarginRight;
- int lineYEnd = rect.Height - (LineMarginBotton + LineHeight);
- Point lineStartLoc = new Point(lineXStart, lineYEnd);
- Point lineEndLoc = new Point(lineXEnd, lineYEnd);
- graphics.DrawLine(linePen, lineStartLoc, lineEndLoc);
- #endregion
- #region Text
- string printText = Text;
- Color textColor = ForeColor;
- if (string.IsNullOrEmpty(Text) && false) {
- printText = CueText;
- textColor = CueColor;
- }
- Size textSize = TextRenderer.MeasureText(printText, Font);
- textSize.Width += TextPadding.Right;
- textSize.Height += TextPadding.Bottom;
- Point textLoc = new Point(TextPadding.Left, TextPadding.Top);
- Rectangle textRect = new Rectangle(textLoc, textSize);
- TextRenderer.DrawText(graphics, printText, Font, textRect, textColor);
- #endregion
- #region Cursor
- if (true && !string.IsNullOrEmpty(Text)) {
- string onCT = Text.Substring(0, CursorPosition);
- Size tcSize = TextRenderer.MeasureText(onCT, Font);
- int cursorX = tcSize.Width + 1;
- int cursorY = textLoc.Y;
- Point cursorLoc = new Point(cursorX, cursorY);
- Size cursorSize = new Size(CursorWidth, tcSize.Height);
- Rectangle cursorRect = new Rectangle(cursorLoc, cursorSize);
- using (SolidBrush cursorBrush = new SolidBrush(Color.Black)) {
- graphics.FillRectangle(cursorBrush, cursorRect);
- }
- }
- #endregion
- }
- protected override Cursor DefaultCursor {
- get {
- return Cursors.IBeam;
- }
- }
- protected override bool IsInputKey(Keys keyData) {
- if ((keyData & Keys.Alt) != Keys.Alt) {
- switch (keyData & Keys.KeyCode) {
- case Keys.Tab:
- case Keys.Escape:
- return false;
- case Keys.Back:
- return true;
- case Keys.PageUp:
- case Keys.PageDown:
- case Keys.Home:
- case Keys.End:
- return true;
- }
- }
- return base.IsInputKey(keyData);
- }
- }
- }