当窗体调整大小时,以固定比例调整子控件的宽度和高度。 [英] Resize child control width and height in a fixed ratio when windows form resize.

查看:105
本文介绍了当窗体调整大小时,以固定比例调整子控件的宽度和高度。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows表单应用程序。我将子控件的Dock Porperty设置为Fill,现在当我调整主对话框时,控件将自动调整大小。但是我希望将我的chld控件调整为固定比例。我如何在Windows窗体应用程序中执行此操作?



(例如:当我调整Docked的大小时,40乘40到30乘30,20乘20,50乘50等Windows窗体应用程序)

I am developing a Windows form application. I set the dock Porperty of the child control to Fill, Now the control will resize automatically when I resize main dialog. But I want to resize my chld control to a fixed ratio. How I can do this in windows form application?

(Eg: 40 by 40 to 30 by 30, 20 by 20,50 by 50 etc when I resize my Docked Windows Form application)

推荐答案

设置 Dock 填充意味着子控件将完全填充父窗口。现在在你的情况下,你正在寻找一个方形面板。为此,在Form的resize事件处理程序中,确保您将面板设为正方形。这样的事情:



Setting Dock to fill would mean that the child control will fill the parent completely. Now in your case, you are looking for a square panel. To achieve this, in the Form's resize event handler, ensure that you are making the panel a square. Something like this:

void Form1_Resize(object sender, EventArgs e)
       {
           panel1.Height = panel1.Width;
       }





您可能想要了解应该保留哪一侧,高度或宽度的逻辑。请注意,这可能会导致一些闪烁。



You might want to put in some logic as to which side should be retained, height or width. Note that this may result in some flickering.


您无法自动执行此操作 - Anchor和Dock属性都旨在独立地在垂直和水平方向上灵活调整控件的大小,并且没有保持宽高比的选项。



你可以这样做 - 但你必须处理Resize事件的形式并设置适当的宽度和高度控制你自己。
You can't do that automatically - the Anchor and Dock properties are both designed to flex the size of controls independently on the vertical and horizontal, and don't have an option for maintaining an aspect ratio.

You can do it - but you will have to handle the form Resize event and set the appropriate Width and Height of your control yourself.


这篇关于当窗体调整大小时,以固定比例调整子控件的宽度和高度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆