获取控件的最大大小而不最大化它,窗口窗体

本文关键字:最大化 窗体 窗口 控件 获取 | 更新日期: 2023-09-27 18:35:48

我有一个包含图片框(锚点设置为左,右,上和下)和其他一些控件的表单,我想知道无论如何都可以在不最大化表单的情况下最大化表单时获取图片框的大小,

[我必须根据图片框的最大大小裁剪图像]

获取控件的最大大小而不最大化它,窗口窗体

有几种方法可以解决这个问题。1. 迁移到 WPF。(Windows 窗体即将被弃用。2. 使用反射在运行时获取图片框的属性。尝试这样的事情:

using System.Reflection;
private PictureBox DoSomethingWithProperties(PictureBox picturebox)
{
    Type pictureboxType = picturebox.GetType();
    PictureBox instance = (PictureBox)Activator.CreateInstance(pictureboxType)
    // The above code is only used to create an instance of the picturebox type. 
    // This will enable modification/changes to the picturebox property values during runtime via late-binding.
    PropertyInfo DefaultSize= instance.GetType().GetProperty("DefaultSize", BindingFlags.Public | BindingFlags.Instance);
    PropertyInfo ClientSize= instance.GetType().GetProperty("ClientSize", BindingFlags.Public | BindingFlags.Instance);
    PropertyInfo DefaultMaximumSize= instance.GetType().GetProperty("DefaultMaximumSize", BindingFlags.Public | BindingFlags.Instance);
    return instance;
}