在调整大小时保持控件的长宽比
我有一个面板控件,里面有一个图片框。 调整面板控件所在窗体的大小时,如何保持面板控件的纵横比?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要以某种方式存储纵横比,无论它是您在设计时已知的东西,还是您只想在
InitializeComponent()
之后在表单的构造函数中计算它。 在表单的Resize
事件中,您只需将 Panel 的大小设置为适合表单 ClientRectangle 的纵横比的最大矩形。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'sResize
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.使用锚点 属性,或将控件放入 DockContainer 中并使用 适当地停靠属性。
编辑:实际上这不是你问的,是吗? 我的建议是在容器的调整大小事件上手动执行此操作。 您需要计算 x 或 y 并在加载图片时应用图片的纵横比。
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.