2016-03-31 31 views
0

Özel yuvarlak metin kutum var. Ancak metin düzenleme, metin seçimi vb. Gibi metin kutusu davranışlarını ekleyemedim. Kendimi yapmaya karar verirsem bu özellikler çok zaman alır. Bu özellikleri metin kutusuna nasıl ekleyebilirim?Metin kutusu denetiminin davranışlarını özel denetimime nasıl ekleyebilirim?

Benim TextBox sınıfı: Ben Hazeldev's custom controls bir çözüm bulduk

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; 
     } 
    } 
} 

cevap

0

.

Bu çözümde çocuk kontrolümüz olarak bir metin kutusu kontrolü ekliyoruz.

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); 
    } 

}