2017-08-03 222 views
0

我有一个包含的图像和文本的按钮:调整图像大小时调整

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); 
+1

[制作背景图像规模与按钮的大小(HTTPS的可能重复:// stackoverflow.com/questions/13357032/making-a-background-image-scale-with-button-size) – fuchs777

+0

@ fuchs777正如我所说,我不想使用'BackgroundImage'来同时显示文字和图片 –

+1

_I don不想使用BackgroundImage同时具有文本和图像_但是您__可以同时拥有BackgrondImage和!加上Stretch和Zoom的BackgroundImageLayout选项! – TaW

回答

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]; 
     } 
    } 
} 
1
如果要自动调整应用程序中使用这种方法

创建类调用调整大小

并粘贴此代码

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); 


     } 

    } 
} 

thi s会自动调整所有控件的形式

+0

他已经可以做到这一点。他问的是Button.Image。 – TaW

+0

是的,我知道,但这种方法来调整大小问题 – Zaza

+0

那么,他没有一个。只有图像有,我不明白你的代码将如何帮助.. – TaW