Reputation: 3275
I have a button with an included image and a text :
When I resize my application, I resize the label to fit with the new button's height but the image is not resized :
I saw a solution is to set image to BackgroundImage
but it does not match the design of my button :
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);
Upvotes: 0
Views: 202
Reputation: 3275
Finally found a solution, maybe not the best but working fine :
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];
}
}
}
Upvotes: 0
Reputation: 406
if you want to automatically resize the application use this method
create class called resize
and paste this code
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);
}
}
}
}
and call this class in you form
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);
}
}
}
this will automatically resize your all controls in forms
Upvotes: 1