2016-03-31 15 views
0

我有一個自定義的圓角文本框。但是我無法添加文本框的行爲,如文本編輯,文本選擇等。如果我決定自己做,那些屬性需要很長時間。我如何將這些屬性添加到我的文本框?如何將文本框控件的行爲添加到我的自定義控件中?

我的TextBox類:

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

回答

0

我已經找到了解決方案,從Hazeldev's custom controls

在這個解決方案中,我們添加一個文本框控件作爲我們的子控件。

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

} 
相關問題