缩放Panel,公式问题

时间:2016-06-19 02:57:49

标签: vb.net

我正在尝试扩展面板以适应它的父面板,我已经完成了这样做:

 ValSize = New Size(basePanel.Width - 20, basePanel.Height - 20)

这很好用,但现在我想添加另一个面板(dragItem)到我刚缩小的面板(basePanel),但是我需要在两个面板之间保持相同的缩放比例,因此它们的大小不同但是缩放相同的坐骑。我试图使用这个公式来找出我缩放第一个项目并获得一个百分比并将其用于新面板。

Dim scaleWidth As Double = Math.Abs(basePanel.Width - dragItem.Width) / dragItem.Width

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

如果你想得到这个比例你只能分开。

  • 较小的面板与原始面板的比率:

    Dim scaleWidth As Double = otherPanel.Width / basePanel.Width
    

    返回值< = 1

  • 原始面板与较小面板的比率:

    Dim scaleWidth As Double = basePanel.Width / otherPanel.Width
    

    返回值> = 1

因此,如果您有一个200px宽的面板和一个宽150px的面板,该比率将 0.75 。要根据较小的面板创建一个新面板,您可以这样做:

Dim newWidth As Integer = scaleWidth * otherPanel.Width

由于scaleWidth为0.75且otherPanel.Width为150,您将获得以下结果:0.75 * 150 = 113 (确切地说是112,5,但它已经舍入因为它是一个整数)

现在,您的第三个面板比第二个面板小25%。第二个面板反过来比原始面板小25%。

答案 1 :(得分:0)

尽管可能,但您不需要自己进行此计算。而是将每个面板的停靠属性设置为Fill,并根据需要设置容器的填充。在这种情况下,它似乎希望每个容器都有相同的填充。