2016-03-31 11 views
0

Ich habe eine benutzerdefinierte abgerundete Textbox. Aber ich konnte die Textbox-Verhaltensweisen wie Textbearbeitung, Textauswahl usw. nicht hinzufügen. Diese Eigenschaften brauchen viel Zeit, wenn ich mich entscheide. Wie kann ich diese Eigenschaften in mein Textfeld einfügen?Wie kann ich das Verhalten des Textfeldsteuerelements in mein benutzerdefiniertes Steuerelement einfügen?

Meine TextBox Klasse:

public class AltoTextBox : Control 
{ 
    public AltoTextBox() 
    { 
     SetStyle(ControlStyles.AllPaintingInWmPaint | 
       ControlStyles.SupportsTransparentBackColor | 
       ControlStyles.OptimizedDoubleBuffer | 
       ControlStyles.UserPaint, true); 

     BackColor = Color.Transparent; 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

     RoundedRectangleF strokeRect = new RoundedRectangleF(Width, Height, 10); 
     RoundedRectangleF innerRect = new RoundedRectangleF(Width - 0.5f, Height - 0.5f, 10f, 0.5f, 0.5f); 


     e.Graphics.DrawPath(Pens.Black, strokeRect.Path); 
     e.Graphics.FillPath(Brushes.White, innerRect.Path); 

    } 
} 
public class RoundedRectangleF 
{ 

    Point location; 
    float radius; 
    GraphicsPath grPath; 
    float x, y; 
    float width, height; 
    public RoundedRectangleF(float width, float height, float radius,float x = 0,float y = 0) 
    { 
     location = new Point(0, 0); 
     this.radius = radius; 

     RectangleF upperLeftRect = new RectangleF(x, y, 2 * radius, 2 * radius); 
     RectangleF upperRightRect = new RectangleF(width - 2 * radius - 1, x, 2 * radius, 2 * radius); 
     RectangleF lowerLeftRect = new RectangleF(x, height - 2 * radius - 1, 2 * radius, 2 * radius); 
     RectangleF lowerRightRect = new RectangleF(width - 2 * radius - 1, height - 2 * radius - 1, 2 * radius, 2 * radius); 

     grPath = new GraphicsPath(); 
     grPath.AddArc(upperLeftRect, 180, 90); 
     grPath.AddArc(upperRightRect, 270, 90); 
     grPath.AddArc(lowerRightRect, 0, 90); 
     grPath.AddArc(lowerLeftRect, 90, 90); 
     grPath.CloseAllFigures(); 
     this.x = x; 
     this.y = y; 
     this.width = width; 
     this.height = height; 
    } 
    public RoundedRectangleF() 
    { 
    } 
    public GraphicsPath Path 
    { 
     get 
     { 
      return grPath; 
     } 
    } 
    public RectangleF Rect 
    { 
     get 
     { 
      return new RectangleF(x, y, width, height); 
     } 
    } 
    public float Radius 
    { 
     get 
     { 
      return radius; 
     } 
     set 
     { 
      radius = value; 
     } 
    } 
} 

Antwort

0

ich eine Lösung aus Hazeldev's custom controls gefunden haben.

In dieser Lösung fügen wir ein Textbox-Steuerelement als unser Kind Steuerelement hinzu.

public class AltoTextBox : Control 
{ 
    int radius = 15; 
    public TextBox box = new TextBox(); 
    GraphicsPath Shape; 
    public AltoTextBox() 
    { 
     SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
     SetStyle(ControlStyles.UserPaint, true); 
     SetStyle(ControlStyles.ResizeRedraw, true); 

     AddTextBox(); 
     Controls.Add(box); 

     BackColor = Color.Transparent; 
     ForeColor = Color.DimGray; 

     Text = null; 
     Font = new Font("Comic Sans MS", 11); 
     Size = new Size(135, 33); 
     DoubleBuffered = true; 
    } 
    void AddTextBox() 
    { 
     box.Size = new Size(Width - 2*radius, Height - 6); 
     box.Location = new Point(radius, 3); 
     box.BorderStyle = BorderStyle.None; 
     box.TextAlign = HorizontalAlignment.Left; 
     box.Multiline = true; 
     box.Font = Font; 
    } 
    protected override void OnBackColorChanged(EventArgs e) 
    { 
     base.OnBackColorChanged(e); 
    } 
    protected override void OnTextChanged(EventArgs e) 
    { 
     base.OnTextChanged(e); 
     box.Text = Text; 
    } 
    GraphicsPath innerRect; 
    protected override void OnFontChanged(EventArgs e) 
    { 
     base.OnFontChanged(e); 
     box.Font = Font; 
    } 
    protected override void OnResize(System.EventArgs e) 
    { 
     base.OnResize(e); 
     Shape = new RoundedRectangleF(Width, Height, radius).Path; 
     innerRect = new RoundedRectangleF(Width - 0.5f, Height - 0.5f, radius, 0.5f, 0.5f).Path; 

     AddTextBox(); 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     Bitmap bmp = new Bitmap(Width, Height); 
     Graphics grp = Graphics.FromImage(bmp); 
     grp.SmoothingMode = SmoothingMode.HighQuality; 
     grp.DrawPath(Pens.Gray, Shape); 
     grp.FillPath(Brushes.White, innerRect); 
     e.Graphics.DrawImage((Image)bmp.Clone(), 0, 0); 

     base.OnPaint(e); 
    } 

}