How do I add scaling behavior afterwards in a WinForms application?

I am working on a WinForms Application using Visual Studio 2008 (C #). The user interface of the corresponding form consists of several SplitContainers. When I tested the application after setting the Windows font size to 125%, the form no longer looked the way it should. Obviously there was a scaling issue. So I searched for a solution and found it here here . The next two lines did the job for me:

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

      

As explained in another question (in one of the answers, respectively), they must be included for every container in the constructor file. It works and the scaling is correct. But on the other hand, I need to manually edit the constructor file and shouldn't actually do something. The added lines are lost every time I use the layout constructor. EDIT: just to clarify: these two properties are not shown in the gui design.

Now finally my question is, what can I do? How and where can I add code to scale properly without messy designer file manipulation?

I've already tried to just put these two lines for each container in the constructor after the method call InitializeComponent()

, but at that position they don't have the desired effect.

So, maybe you have an idea how this should be done.

Thanks in advance,
Alex

+3


source to share


1 answer


I finally found a way to solve the problem. This is actually not what I originally assumed, but it has the same effect.

Regarding two properties are not showing in the gui designer, so why not make them show? So I created a custom control and added some attributes to the scaling properties so that they appear in the designer.



public class ScalableSplitContainer : SplitContainer
{
    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(true)]
    public new AutoScaleMode AutoScaleMode
    {
        get { return base.AutoScaleMode; }
        set { base.AutoScaleMode = value; }
    }

    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(true)]
    public new SizeF AutoScaleDimensions
    {
        get { return base.AutoScaleDimensions; }
        set { base.AutoScaleDimensions = value; }
    }
}

      

Using this specialized SplitContainer, the scaling behavior can be easily set in the gui and the lines of code are included in the generated constructor file.

+1


source







All Articles