2016-03-31 8 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); 
    } 

} 
関連する問題