【问题标题】:How to add Control over GraphicsPath如何添加对 GraphicsPath 的控制
【发布时间】:2015-03-10 11:22:45
【问题描述】:

我已经使用 OnPaint 事件绘制了一个圆形 Rectangle:

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

            GraphicsPath path = RoundedRectangle.Create(5, 5, 20, 20);
            e.Graphics.DrawPath(Pens.Black, path);
        }

我想在那个矩形上画一个控件。例如:TextBox

问题:有没有办法知道 GraphicsPath 的 LocationPoint 来设置我对它的控制?

【问题讨论】:

    标签: c# winforms drawing


    【解决方案1】:

    所以你想要看起来像这样的东西:

    我的设置为自定义类,继承自控件并具有自定义OnPaint 以获得圆角边缘:

    class RoundedText : Control
    {
        //Code
        protected override void OnPaint(PaintEventArgs e)
        {
            //Code for rounded edges
        }
    }
    

    然后为了显示文本框,我将 TextBox 添加到控件中,就像添加表单一样:

    class RoundedText : Control
    {
        public RoundedText()
        {
            TextBox t = new TextBox();
            t.Left = 10;
            t.Top = 1;
            this.Controls.Add(t);
        }
    }
    

    这样,TextBox 始终是相对于矩形放置的,因此您无需担心获取路径的位置。

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2012-08-10
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多