以下是一个示例代码,其中panel1是我们要进行缩放的panel,panel1中包含了多个控件,我们需要保证这些控件的相对位置在缩放后保持不变。

private void panel1_Resize(object sender, EventArgs e)
{
    // 获取panel的缩放比例
    float ratioWidth = (float)panel1.Width / panel1.PreferredSize.Width;
    float ratioHeight = (float)panel1.Height / panel1.PreferredSize.Height;

    // 遍历panel中的所有控件,并计算它们缩放后的位置
    foreach (Control ctrl in panel1.Controls)
    {
        ctrl.Left = (int)(ctrl.Left * ratioWidth);
        ctrl.Top = (int)(ctrl.Top * ratioHeight);
        ctrl.Width = (int)(ctrl.Width * ratioWidth);
        ctrl.Height = (int)(ctrl.Height * ratioHeight);
    }
}

在这个示例代码中,我们使用了panel的Resize事件来处理缩放逻辑。首先,我们计算出panel的缩放比例,然后遍历panel中的所有控件,并根据缩放比例计算它们缩放后的位置和大小。这样,在panel缩放的同时,所有包含在其中的控件的相对位置都会得到保持。

c#中panel中控件随panel的缩放panel中控件的相对位置改变的代码

原文地址: https://www.cveoy.top/t/topic/MpL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录