任何分辨率的缩放按钮

时间:2014-10-29 10:18:22

标签: c# button size screen scale

我尝试编写一种方法,可以在全屏幕上按任意分辨率缩放按钮,而不会在它们之间留下空间。按钮参数来自Web服务。无论参数是什么,我如何调整它们以填充所有屏幕。或者至少将它们的实际尺寸缩放到我的新尺寸。我的表单尺寸很好,但我的按钮相交。

   private void ResizeButtons()
   {
        pnlDynamicControls.Height = this.ClientSize.Height 
                                  - (pnlDynamicControls.Top + pnlButtons.Height);
        pnlDynamicControls.Width = pnlButtons.Width;
        float widthRatio = 
                    (float)Screen.PrimaryScreen.Bounds.Width / ClientRectangle.Width;
        float heightRatio = 
                    (float)Screen.PrimaryScreen.Bounds.Height / ClientRectangle.Height;
        SizeF scale = new SizeF(widthRatio,heightRatio);
        this.Scale(scale);

        //if ((widthRatio - pnlDynamicControls.Width) < (heightRatio - pnlDynamicControls.Height))
        //    l_zoomFactor = (float)widthRatio / pnlDynamicControls.Width;
        //else
        //    l_zoomFactor = (float)heightRatio / pnlDynamicControls.Height;

        foreach (Control c in pnlDynamicControls.Controls)
        {

            c.Height = Convert.ToInt16(c.Height * heightRatio);
            c.Width = Convert.ToInt16(c.Width * widthRatio);
            c.Margin = new Padding(1, 1, 1, 1);

            //c.Size = new System.Drawing.Size(
            //                                 Convert.ToInt16(c.Width * l_zoomFactor)
            //                               , Convert.ToInt16(c.Height * l_zoomFactor)
            //                                 );
        }
    }

0 个答案:

没有答案