簡體   English   中英

如何在WinForm的控件設計器中使用鼠標按比例調整控件的大小?

[英]How to proportionally resize a control with the mouse within WinForm's control designer?

我試圖用鼠標調整控件的大小,按住左鍵+ Shift鍵,希望寬度和高度都按比例調整(就像在Photoshop中一樣)。 沒工作。

我用Google搜索了解如何做到這一點,確定我在一分鍾內得到答案。 令我驚訝的是我找不到任何東西。

我必須明白Visual Studio,即使是2013版本,也缺乏這個非常基本的設計功能嗎?! 或者我只是一直想念它?

請注意,這不僅適用於特定控件; 它是一個設計工具,我想用於任何可以在表單/用戶控件上“繪制”的東西。

您可以隨時擴展您想要保持的比例:

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'以使其適當調整大小。

但是,如果您需要它來處理許多不同的控件,這個解決方案可能會非常艱巨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM