Win Forms按钮图像有不需要的边框

时间:2014-09-05 11:25:28

标签: image winforms button

设置Button属性时Button.Image周围的不需要的边框。第二个图像是光标在黄色框上时。

enter image description here enter image description here

button.ImageAlign设置为ContentAlingment.MiddleCenter时,边框仍然存在但只有1像素宽。

红色框应为100x100,但为95x95(在屏幕截图上测量)。

预期结果为四个方格,两者之间没有间隙。

当我使用BackgroundImage属性时,问题不存在,但按钮在禁用时不会自动灰显。

完整代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        Shown += OnShown;
    }

    private Bitmap DrawFilledRectangle(int x, int y, Brush brush)
    {
        var bmp = new Bitmap(x, y);
        using (var graph = Graphics.FromImage(bmp))
        {
            var imageSize = new Rectangle(0, 0, x, y);
            graph.FillRectangle(brush, imageSize);
        }
        return bmp;
    }

    private void OnShown(object sender, EventArgs eventArgs)
    {
        BackColor = Color.Wheat;

        var leftTop = new Button();
        var rightTop = new Button();
        var leftBottom = new Button();
        var rightBottom = new Button();

        var bmp1 = DrawFilledRectangle(100, 100, Brushes.Firebrick);
        var bmp2 = DrawFilledRectangle(100, 100, Brushes.ForestGreen);
        var bmp3 = DrawFilledRectangle(100, 100, Brushes.CornflowerBlue);
        var bmp4 = DrawFilledRectangle(100, 100, Brushes.Yellow);

        SetButton(leftTop, bmp1);
        SetButton(rightTop, bmp2);
        SetButton(leftBottom, bmp3);
        SetButton(rightBottom, bmp4);

        leftTop.Left = leftTop.Top = 10;

        rightTop.Left = leftTop.Left+ bmp1.Width;
        rightTop.Top = 10;

        leftBottom.Left = 10;
        leftBottom.Top = leftTop.Top + bmp1.Height;

        rightBottom.Left = leftBottom.Left + bmp1.Width;
        rightBottom.Top = rightTop.Top + bmp1.Height;

        Controls.AddRange(new Control[] { leftTop, rightTop, leftBottom,rightBottom });
    }

    private void SetButton(Button button, Bitmap bmp)
    {
        button.FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
        button.FlatAppearance.BorderSize = 0;
        button.FlatStyle = FlatStyle.Flat;
        button.Image = bmp;
        button.ImageAlign = ContentAlignment.TopLeft;
        button.Margin = new Padding(0);
        button.Padding = new Padding(0);
        button.Size = bmp.Size;
    }
}

修改

我在实际应用中有图片而不是填充框。

2 个答案:

答案 0 :(得分:0)

即使你设置FlatAppearance.BorderSize = 0,它仍然不能正确拉伸它。

所以基本上你必须这样做:

SetButton(leftTop, bmp1, Color.Firebrick);
SetButton(rightTop, bmp2, Color.ForestGreen);
SetButton(leftBottom, bmp3, Color.CornflowerBlue);
SetButton(rightBottom, bmp4, Color.Yellow);

private void SetButton(Button button, Bitmap bmp, Color borderColor)
{
    button.FlatAppearance.BorderColor = borderColor;
    button.FlatAppearance.BorderSize = 1;
    button.FlatStyle = FlatStyle.Flat;
    button.Image = bmp;
    button.ImageAlign = ContentAlignment.MiddleCenter;
    button.Margin = new Padding(0);
    button.Padding = new Padding(0);
    button.Size = bmp.Size;
}

enter image description here

答案 1 :(得分:0)

解决方案:

public class AppButton : Button
{
    protected override void OnPaint(PaintEventArgs pevent)
    {
        if (Enabled)
        {
            pevent.Graphics.DrawImageUnscaled(Image, 0, 0);
        }
        else
        {
            ControlPaint.DrawImageDisabled(pevent.Graphics, Image, 0, 0, Color.Transparent);
        }
    }
}