There are various recommendations on the web on how to best handle designing a winforms application for variable DPI. This includes:
- Only using TableLayoutPanels / FlowLayoutPanels (do not set absolute coordinates)
- Setting custom AutoScaleDimensions values to a magic number
- Trying different AutoScaleMode values (dpi/font/none)
- Do nothing.
None of these techniques has worked for every scenario.
Through trial and error, I stumbled across a technique that is simple to implement and doesn’t place constrains on the design layout (can use absolute coordinates). It all comes down to understanding a bug that currently exists in the .Net 4 winforms framework.
If the Form’s ControlBox is set to false in the designer for a fixed single dialogue, it incorrectly lays out the dialogue for a variable DPI scenario. The form’s title bar is not taken into account when determining the size of the form to be shown, so it is drawn slightly too short in height. There is a simple work around to this problem. In the designer, leave the Form’s ControlBox value as true, and in the Form’s Load event, set the ControlBox to false.
/// <summary> /// Event handler for the form's Load event. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">Event arguments.</param> private void Form_Load(object sender, EventArgs e) { // When ControlBox is set to false at design time, in non- // standard DPI environments, the form's title bar is not // taken into account when determining the size of the form // to be shown, so it is drawn slightly too short in height. // So, we keep it enabled in the designer, and set it to false // here in the Load event. this.ControlBox = false; }
To complete the solution so that winform works for variable DPI, you must also set the AutoScaleMode to either Font or DPI