2

I made a project in C# Form with

FormBorderStyle=FormBorderStyle.None

Resolution = 1920 x 1080.

But, this project is set for 1080p only and takes it offscreen at 720p. How can I code this form (including the components inside) so that it fits on any screen? If it needs to grow, it will grow, if it needs to shrink, it will shrink, but all the components should also move accordingly, so they are always visible on the form.

As I said, I don't use border in my project.

Huntbook
  • 114
  • 9
Manly
  • 61
  • 6
  • The first step would be find out what the size of the monitor is that your form is on. Do you know how to do that? – gunr2171 May 07 '22 at 19:52
  • No, I do not know @gunr2171 – Manly May 07 '22 at 19:55
  • A, I've read it again, but you would like to scale your layout to any resolution. – Jeroen van Langen May 07 '22 at 19:58
  • Does this answer your question? [How to retrieve the Screen Resolution from a C# winform app?](https://stackoverflow.com/questions/2402739/how-to-retrieve-the-screen-resolution-from-a-c-sharp-winform-app) – gunr2171 May 07 '22 at 20:11
  • @Manly Please use the english language.. Did you check the anchor/dock properties of controls? – Jeroen van Langen May 07 '22 at 20:17
  • Yes, I want any screen to be opened according to its resolution. When I use the Dock and Anchor feature, will my resolution set to 1080p automatically downgrade to, for example, a 720p screen? (Sorry for the language problem) @JeroenvanLangen – Manly May 07 '22 at 20:22
  • Override the Form's `OnHandleCreated` method to assign the screen's working area size to the `Form.MaximumSize` property. `this.MaximumSize = Screen.FromControl(this).WorkingArea.Size;`. – dr.null May 07 '22 at 21:16
  • And according to your comments down there, make sure to make your app [DpiAware](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms?view=netframeworkdesktop-4.8). – dr.null May 07 '22 at 21:34

1 Answers1

2

You would first have to find out what the resolution is of your screen. If you have multiple screens, by default the Form will appear on the primary screen, which is Screen.PrimaryScreen.

This has the two properties you will need: Bounds.Width and Bounds.Height. With these you can change the ClientSize to fit your screen.

private double screenWidth = Screen.PrimaryScreen.Bounds.Width;
private double screenHeight = Screen.PrimaryScreen.Bounds.Height;
private void FitWindowFormToScreen() {
     this.ClientSize = new Size
     (   Convert.ToInt32(screenWidth),
         Convert.ToInt32(screenHeight)
     );
}

To also scale the components

You first need to calculate the ratio of the screen size to the original Form size, at which you designed the layout. The denominators may just be expressed as numbers, because you only need to define it once.

double xShift = screenWidth / 816;
double yShift = screenHeight / 489;

These can then be used as the factor by which all controls on the Form are scaled. To rescale both the Location and Size properties in a foreach loop, I recommend defining a seperate method:

private void ScaleLayout(Control container) {
    foreach (Control control in container.Controls) {
        control.Location = new Point
        (   Convert.ToInt32(control.Location.X * xShift),
            Convert.ToInt32(control.Location.Y * yShift)
        );
        control.Size = new Size
        (   Convert.ToInt32(control.Size.Width * xShift),
            Convert.ToInt32(control.Size.Height * yShift)
        );
    }
}

This function takes a parameter for a reason; controls that are inside of container controls like GroupBox or TabControl objects are not affected. So, you can use a recursive call like this:

if (control.HasChildren) {
    ScaleLayout(control);
}

To call this method, simply do:

ScaleLayout(this);
Huntbook
  • 114
  • 9
  • I was able to change the resolution of the project, but the groups in it did not shrink automatically, their size remained the same, my aim was to reduce the groups there as well. @Huntbook – Manly May 07 '22 at 20:40
  • after your codes https://imgyukle.com/i/R1jTWY and this is the original project https://imgyukle.com/i/R1jiXn @Huntbook – Manly May 07 '22 at 20:45
  • @Manly I expanded on my answer. I would put that code in the same method that rescales the window to fit the screen. – Huntbook May 07 '22 at 23:01
  • First of all, thank you for your help. But I couldn't. There are too many variables in the code and I couldn't edit the code you wrote to make the components and groups smaller. @Huntbook – Manly May 08 '22 at 10:01
  • Thank you for your solution. I edited the project and added borders. I adapted it for each screen. But I will try this solution in a backup, I may need it in the future. Thank you so much ! If it works, I'll reply here. – Manly May 14 '22 at 08:12