定制化按钮 图片背景+ 文字

C# Forms - Buttons with multiple texts, and image

https://stackoverflow.com/questions/18192556/c-sharp-forms-buttons-with-multiple-texts-and-image

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))
            {
                pevent.Graphics.DrawString(LeftText, Font, brush, rect, sf);
                sf.Alignment = StringAlignment.Far;
                pevent.Graphics.DrawString(RightText, Font, brush, rect, sf);
            }
        }
    }
}
//Use it
xButton1.Image = yourImage;
xButton1.LeftText = "How interesting winforms is";
xButton2.RightText = "F12";
//You can add more properties to this XButton class to control how it looks

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值