johnnyontheweb
johnnyontheweb

Reputation: 51

Avoid automatic resizing and placement of controls in Winform .NET application

I'm developing in vb.net a Winform .NET 4.0 application with Visual Studio 2013 on Windows 10. Recently I buy a new laptop, and I use it with its maximum screen resolution of 2550x1440.

Visual Studio seems to automatically change file *.designer.vb and *.resx, for each form I modify from the new pc, changing location and anchor of some controls. To be more precise, when I re-open the Winform project on a another (old) pc, I find a real mess on: - the size of the form: in general the form size is decreased, hiding some controls; - the location of some controls: some of them are overlapped with others; - the ImageScaling property of ToolStripMenu icons is automatically changed from 16x16 (the correct one) to 24x24, causing really big menus!

I read a lot of pages over the internet on this topic, but I cannot find any suitable solution. Some of them are from StackOverflow, such as: - How do I keep Visual Studio's Windows Forms Designer from deleting controls? (cannot really understand why VS touches continuosly controls) - Why does Visual Studio automatically changes the layout of my form? - visual studio 2005 designer moves controls and resizes Form (regarding this, it is not a solution to use docking instead of anchoring, some controls do not support it) - How to write WinForms code that auto-scales to system font and dpi settings? (if I set the AutoScale property to DPI, the form displays not correctly, and cannot be resized properly!)

Other solutions I tried, but with no luck: - disabling the AutoSize property of the controls: not a solution, because if you do it for example on ToolStripMenus, you have to change their size manually each time you add a button or whatever; - act on AutoScaling property: I tried every combination of this property, the only one which works fine is "Font".

This is a REALLY annoying problem: is there a way to block VS from modifying locations and dimensions of controls? Possibly without re-ordering manually (or re-doing) the layout of each form? Thanks a lot to anyone

Upvotes: 1

Views: 1940

Answers (1)

Charlie
Charlie

Reputation: 1293

In the proccess of setting form dimensions, the form size is restricted by Screen.GetWorkingArea without taking into account AutoScale.

I resolve this issue storing the original ClientSize setted in InitializeComponent() and reset it autoscaled on HandleCreated event stage.

    private SizeF _autoScaleFactor;
    private Size _originalClientSize;

    protected override void SetClientSizeCore(int x, int y)
    {
        base.SetClientSizeCore(x, y);

        _autoScaleFactor = AutoScaleFactor;
        _originalClientSize = new Size(x, y);
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        AutoScaleClientSize();

        base.OnHandleCreated(e);
    }

    private void AutoScaleClientSize()
    {
        var dx = _autoScaleFactor.Width;
        if (!dx.Equals(1.0F))
        {
            _originalClientSize.Width = (int)Math.Round(_originalClientSize.Width * dx);
        }

        var dy = _autoScaleFactor.Height;
        if (!dy.Equals(1.0F))
        {
            _originalClientSize.Height = (int)Math.Round(_originalClientSize.Height * dy);
        }

        ClientSize = _originalClientSize;
    }

Upvotes: 1

Related Questions