调整大小时调整按钮中包含的图像大小

时间:2017-08-03 09:00:38

标签: c# winforms button

我有一个包含图片和文字的按钮:

Button with image OK

当我调整应用程序的大小时,我会调整标签大小以适应新按钮的高度,但图像不会调整大小:

BUtton with image KO

我看到一个解决方案是将图片设置为BackgroundImage,但它与我的按钮的设计不符:

this.buttonClose.Dock = System.Windows.Forms.DockStyle.Fill;
this.buttonClose.Image = ((System.Drawing.Image)(resources.GetObject("buttonClose.Image")));
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(482, 28);
this.buttonClose.Text = "Close";
this.buttonClose.TextAlign = ContentAlignment.MiddleRight;
this.buttonClose.TextImageRelation = TextImageRelation.ImageBeforeText;
this.buttonClose.UseVisualStyleBackColor = true;
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);

2 个答案:

答案 0 :(得分:1)

如果要自动调整应用程序的大小,请使用此方法

创建名为resize的类

并粘贴此代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YOUR APPLICATION NAMESPACE
{

   public class ClassResize
    {
       List<System.Drawing.Rectangle> AryControlsStorage = new List<System.Drawing.Rectangle>();
       private bool ShowRowHeader=false;

       private Form form { get; set; }
       private float FontSize { get; set; }
       private System.Drawing.SizeF FormSize { get; set; }

       public ClassResize(Form FForm)
       {
           form = FForm;
           FormSize = FForm.ClientSize;
           FontSize = FForm.Font.Size;
       }
       private static IEnumerable<Control> GetAllControls(Control c)
       {
           return c.Controls.Cast<Control>().SelectMany(item=>           
           GetAllControls(item)).Concat(c.Controls.Cast<Control>()).Where(control=>
                control.Name !=string.Empty);
       }
       public void GetInitialSize()
       {
           var _Controls = GetAllControls(form);
           foreach (Control control in _Controls)
           {
               AryControlsStorage.Add(control.Bounds);
               if (control.GetType() == typeof(DataGridView))
                   DGColumnAdjust(((DataGridView)control), ShowRowHeader);

           }

       }
       public void Resize()
       {
           double FormRatioWidth = (double)form.ClientSize.Width / (double)FormSize.Width;
           double FormRatioHeight=(double)form.ClientSize.Height / (double)FormSize.Height;
           var _Controls = GetAllControls(form);
           int Postion = -1;
           foreach(Control control in _Controls)
           {
               Postion += 1;
               System.Drawing.Size _ControlsSize = new System.Drawing.Size((int)(AryControlsStorage[Postion].Width * FormRatioWidth),
                   (int)(AryControlsStorage[Postion].Height * FormRatioHeight));
               System.Drawing.Point _ControlsPoint = new System.Drawing.Point((int)(AryControlsStorage[Postion].X * FormRatioWidth),
    (int)(AryControlsStorage[Postion].Y * FormRatioHeight));
               control.Bounds = new System.Drawing.Rectangle(_ControlsPoint, _ControlsSize);
               if (control.GetType() == typeof(DataGridView))
                   DGColumnAdjust(((DataGridView)control),ShowRowHeader);
               //control.Font = new System.Drawing.Font(form.Font.FontFamily,
               //(float)(((Convert.ToDouble(FontSize) * FormRatioWidth) / 1.5) + ((Convert.ToDouble(FontSize) * FormRatioHeight) / 1.5)));



           }

       }

       private void DGColumnAdjust(DataGridView dgv, bool _showRowHeader)
       {
           int intRowHeader = 0;
           const int Hscrolbarwidth = 5;
           if (_showRowHeader)
           {
               intRowHeader = dgv.RowHeadersWidth;
           }
           else
           {
               dgv.RowHeadersVisible = false;
           }
               for (int i = 0; i < dgv.ColumnCount; i++)
               {
                   if (dgv.Dock == DockStyle.Fill)
                       dgv.Columns[i].Width = ((dgv.Width - intRowHeader) / dgv.ColumnCount);
                   else
                       dgv.Columns[i].Width = ((dgv.Width - intRowHeader - Hscrolbarwidth) / dgv.ColumnCount);

               }
           }


    }
}

并以您的形式调用此类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Data.SqlClient;


namespace YOUR NAMESPACE
{
    public partial class FORMNAME: Form
    {
             ClassResize Form;



        public FORMNAME()
        {

            InitializeComponent();
           Form = new ClassResize(this);


        }

    }
}

这会自动调整表单中所有控件的大小

答案 1 :(得分:0)

终于找到了一个解决方案,也许不是最好的但工作得很好:

private Dictionary<Button, Image> dicButtonsBaseImage = new Dictionary<Button, Image>();
private void SetImageButton(Button btn, Image img)
{
    // Initiate image for button
    btn.Image = img;

    // set method dor sizeChanged
    btn.SizeChanged += Control_SizeChanged;

    // Save image associated for this button
    if (!dicButtonsBaseImage.ContainsKey(btn))
        dicButtonsBaseImage.Add(btn, img);
    else
        dicButtonsBaseImage[btn] = img;

    // Init image size
    resizeImageSize(btn);
}

private void Control_SizeChanged(object sender, EventArgs e)
{
    Control c = sender as Control;
    if (c != null)
    {
        Button b = sender as Button;
        if (b != null)
        {
            resizeImageSize(b);
        }
    }
}

private static void resizeImageSize(Button b)
{
    if (b.Image != null && dicButtonsBaseImage.ContainsKey(b))
    {
        // Set a margin (top/bot) to 8px
        if (b.Height - 8 < dicButtonsBaseImage[b].Height)
        {
            int newHeight = b.Height - 8;
            if (newHeight <= 0)
                newHeight = 1;
            Image img = new Bitmap(dicButtonsBaseImage[b], new Size(dicButtonsBaseImage[b].Width, newHeight));
            b.Image = img;
        }
        else
        {
            b.Image = dicButtonsBaseImage[b];
        }
    }
}