一个button上两种字体 重写OnPaint方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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); } |
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步