一个button上两种字体 重写OnPaint方法

 public class XButton : Button
    {
        public XButton()
        {
            UseVisualStyleBackColor = false;
            TextImageRelation = TextImageRelation.ImageAboveText;
        }
        public override string Text
        {
            get { return ""; }
            set { base.Text = value; }
        }
        public string LeftText { get; set; }
        public string RightText { get; set; }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Rectangle rect = ClientRectangle;
            rect.Inflate(-5, -5);
            using (StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far })
            {
                using (Brush brush = new SolidBrush(ForeColor))
                {
                    Font font= new Font(Font,FontStyle.Bold);
                    pevent.Graphics.DrawString(LeftText, font, brush, rect, sf);
                    sf.Alignment = StringAlignment.Far;
                    pevent.Graphics.DrawString(RightText, Font, brush, rect, sf);
                }
            }
        }
    }

private void Form1_Load(object sender, EventArgs e)
        {
            XButton button = new XButton();
            button.LeftText = "hello";
            button.RightText = "test";
            this.Controls.Add(button);
        }

  

posted @ 2019-05-09 10:40  JackJu  阅读(456)  评论(0编辑  收藏  举报