如题,怎样做到?

解决方案 »

  1.   

    居右简单,修改一下属性RightToLeft为Yes即可。居中不知道,好像没有什么属性可用,可能比较麻烦了
      

  2.   

    谢谢楼上两位!
    居右解决了
    GroupBox标题居中怎么做到?求解答!
      

  3.   

    不过可以,groupbox的text为空,上面放个label
      

  4.   

    加一个自定义GroupBox控件,加TextAlignment属性,重写OnPainting方法,新建Text属性
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace TestTemplate
    {
        public partial class grp : GroupBox
        {
            public grp()
            {
                InitializeComponent();
                base.Text = "";
            }
            private string text;
            public new string Text
            {
                get
                {
                    return this.text;
                }
                set
                {
                    base.Text = "";
                    this.text = value;
                }
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                StringFormat s = new StringFormat();
                s.LineAlignment = StringAlignment.Near;
                s.Alignment = this.textAlignment;
                e.Graphics.DrawString(this.text, this.Font, Brushes.Black, new RectangleF(0, 0, this.Width, this.Height), s);
            }
            private StringAlignment textAlignment = StringAlignment.Center;        public StringAlignment TextAlignment
            {
                get { return textAlignment; }
                set { textAlignment = value; }
            }    }
    }
      

  5.   

    多谢!
    我又用背景色把文字压住的框线重画了一下,但是框线的凹凸感却消不掉,效果如图:GroupBox的框线是怎么画的?如何消除,请高手指点
      

  6.   

      可以这样去处理:
      在onpaint 事件里:
       base.OnPaint(e);
                StringFormat s = new StringFormat();
                s.LineAlignment = StringAlignment.Near;
                s.Alignment = this.textAlignment;
                //新增如下代码段--------------------------------------------------------
                SizeF textarea= e.Graphics.MeasureString(this.Text, this.Font);
                Brush a = new SolidBrush(this.BackColor);
                e.Graphics.FillRectangle(a, new RectangleF(5, 0, (this.Width - 10) * 1.0f, textarea.Height));
                //--------------------------------------------------------------------
                e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, new RectangleF(0, 0, this.Width, this.Height), s);
      

  7.   


    e.Graphics.FillRectangle(a, new RectangleF(5, 0, (this.Width - 10) * 1.0f, textarea.Height));
    把上面这句话改成:
    e.Graphics.FillRectangle(a, new RectangleF((this.Width-textarea.Width)/2, 0, textarea.Width, textarea.Height));
    这样的话文字的长短也没有限制了