如果想让c#panel中的控件相对于panel位置随panel的缩放控件也相应改变,可以使用以下代码:

private void panel1_Resize(object sender, EventArgs e) { foreach (Control c in panel1.Controls) { c.Left = (int)(c.Left * (float)panel1.Width / (float)panel1.PreviousWidth); c.Top = (int)(c.Top * (float)panel1.Height / (float)panel1.PreviousHeight); c.Width = (int)(c.Width * (float)panel1.Width / (float)panel1.PreviousWidth); c.Height = (int)(c.Height * (float)panel1.Height / (float)panel1.PreviousHeight); } panel1.PreviousWidth = panel1.Width; panel1.PreviousHeight = panel1.Height; }

在这个代码中,panel1_Resize事件会在Panel大小改变时触发。在控件循环中,我们根据控件的先前位置和大小计算出新的位置和大小,以便使它们与Panel大小的比例保持一致。最后,我们更新Panel的先前大小,以便在下一次调整大小时使用。

请注意,这个代码只能在Panel控件的子控件中使用。如果您想要在其他类型的控件中使用这个代码,请修改变量名和事件名称以适应您的控件。


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

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