在运行时调整控件的大小(无边框形式)

时间:2019-10-11 02:20:24

标签: c#

也许有人可以提供帮助:)我不知道如何使此代码适用于无边界表单。

  

Form1

public partial class Form1 : Form
{
clsResize _form_resize;

public Form1()
{
    InitializeComponent();

    FormBorderStyle = FormBorderStyle.None;
   _form_resize = new clsResize(this);
    this.Load += _Load;
    this.Resize += _Resize;
}
private void _Load(object sender, EventArgs e)
{
    _form_resize._get_initial_size();
}

private void _Resize(object sender, EventArgs e)
{
    _form_resize._resize();
}
//
//This part im using for resize form
//
private const int cGrip = 16;

protected override void OnPaint(PaintEventArgs e)
{
    Rectangle rc = new Rectangle(ClientSize.Width - cGrip, ClientSize.Height - cGrip, cGrip, cGrip);
    ControlPaint.DrawSizeGrip(e.Graphics, BackColor, rc);
}

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x84)
    {
        Point pos = new Point(m.LParam.ToInt32());
        pos = PointToClient(pos);
        if (pos.X >= ClientSize.Width - cGrip && pos.Y >= ClientSize.Height - cGrip)
        {
            m.Result = (IntPtr)17;
            return;
        }
    }
    base.WndProc(ref m);
}
  

clsResize.cs

public class clsResize
{
List<System.Drawing.Rectangle> _arr_control_storage = new List<System.Drawing.Rectangle>();
private bool showRowHeader = false;
public clsResize(Form _form_)
{
    form = _form_;
    _formSize = _form_.ClientSize;
    _fontsize = _form_.Font.Size;
}

private float _fontsize  { get; set; }

private System.Drawing.SizeF _formSize {get;set; }

private Form form { get; set; }

public void _get_initial_size()
{
    var _controls = _get_all_controls(form);
    foreach (Control control in _controls)
    {
        _arr_control_storage.Add(control.Bounds);          
    }
}

public void _resize()
{
    double _form_ratio_width = (double)form.ClientSize.Width /(double)_formSize.Width;
    double _form_ratio_height = (double)form.ClientSize.Height / (double)_formSize.Height;
    var _controls = _get_all_controls(form);
    int _pos = -1;
    foreach (Control control in _controls)
    {
        _pos += 1;
        System.Drawing.Size _controlSize = new System.Drawing.Size((int)(_arr_control_storage[_pos].Width * _form_ratio_width),
            (int)(_arr_control_storage[_pos].Height * _form_ratio_height));

        System.Drawing.Point _controlposition = new System.Drawing.Point((int)
        (_arr_control_storage[_pos].X * _form_ratio_width),(int) (_arr_control_storage[_pos].Y * _form_ratio_height));

        control.Bounds = new System.Drawing.Rectangle(_controlposition, _controlSize);

        control.Font = new System.Drawing.Font(form.Font.FontFamily,
         (float)(((Convert.ToDouble(_fontsize) * _form_ratio_width) / 2) +
          ((Convert.ToDouble(_fontsize) * _form_ratio_height) / 2)));

    }
}
private static IEnumerable<Control> _get_all_controls(Control c)
{
    return c.Controls.Cast<Control>().SelectMany(item =>
        _get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control => 
        control.Name != string.Empty);
}

因此对于较大的Form来说,它是完美的作品,但是如果FormBorderStyle.None,请在此位置获取“ System ArgumentOutOfRangeException”:

            System.Drawing.Size _controlSize = new System.Drawing.Size((int)(_arr_control_storage[_pos].Width * _form_ratio_width),
            (int)(_arr_control_storage[_pos].Height * _form_ratio_height));

有什么想法吗?至少我可以从哪里开始?抱歉,如果我做错了,我是新手)

0 个答案:

没有答案