我试图用鼠标调整控件的大小,按住左键Shift,希望宽度和高度都按比例调整(如在Photoshop中).没工作.
我用Google搜索了解如何做到这一点,确定我在一分钟内得到答案.令我惊讶的是我找不到任何东西.
解决方法
您可以随时扩展您想要保持的比例:
public class Panelx : Panel { private int _width; private int _height; private double _proportion; private bool _changingSize; [DefaultValue(false)] public bool MaintainRatio { get; set; } public Panelx() { MaintainRatio = false; _width = this.Width; _height = this.Height; _proportion = (double)_height / (double)_width; _changingSize = false; } protected override void OnResize(EventArgs eventargs) { if (MaintainRatio == true) { if (_changingSize == false) { _changingSize = true; try { if (this.Width != _width) { this.Height = (int)(this.Width * _proportion); _width = this.Width; }; if (this.Height != _height) { this.Width = (int)(this.Height / _proportion); _height = this.Height; }; } finally { _changingSize = false; } } } base.OnResize(eventargs); } }
然后,您需要做的就是将MaintainRatio属性设置为’true’以使其适当调整大小.
但是,如果您需要它来处理许多不同的控件,这个解决方案可能会非常艰巨.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。