Maintain a controls Aspect Ratio on Resize

荒凉一梦 提交于 2020-01-15 12:13:02

问题


I have a panel control with a picture box in it. How can I maintain the aspect ratio of the panel control when Resizing the form it's on?


回答1:


You'll need to store off the aspect ratio somehow, whether it's something known to you at design time or if you just want to calculate it in the constructor of the form after InitializeComponent(). In your form's Resize event, you'll just need to set the size of the Panel to be the largest rectangle at that aspect ratio that fits within the ClientRectangle of the form.




回答2:


Use the Anchor property, or put the control in a DockContainer and use the Dock property appropriately.

Edit: Actually that's not what you asked, is it? My recommendation would be to do this manually on the resize event of the container. You'd need to calculate the x or y and apply the aspect ratio of the picture when it was loaded.



来源:https://stackoverflow.com/questions/721621/maintain-a-controls-aspect-ratio-on-resize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!