3

I want to restore the form's size and I used the below codes.

I got an idea from this thread.

And I found out on some machines (in particular, Windows10) a form's width keeps doubled when I reopen this specific form.

I'm guessing that it might be scaling issue of screen resolution but I can't reproduce it.

Since I didn't calculate but store the form's size directly, I have no idea where to look.

I'm going to use a remote assistant and try to figure out why it happened. I will share what I find.

private void Form_Load(object sender, EventArgs e)
{
    this.RestoreWindowPosition();
}

private void SaveWindowPosition()
{
    Properties.Settings.Default.ReviewFormState = this.WindowState;

    if (this.WindowState == FormWindowState.Normal)
    {
        Properties.Settings.Default.ReviewFormLocation = this.Location;
        Properties.Settings.Default.ReviewFormSize = this.Size;
    }
    else
    {
        Properties.Settings.Default.ReviewFormLocation = this.RestoreBounds.Location;
        Properties.Settings.Default.ReviewFormSize = this.RestoreBounds.Size;
    }

    Properties.Settings.Default.ReviewFormHasSetDefaults = true;

    Properties.Settings.Default.Save();
}

private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
    this.SaveWindowPosition();
}

private void RestoreWindowPosition()
{
    if (Properties.Settings.Default.ReviewFormHasSetDefaults)
    {
        this.WindowState = Properties.Settings.Default.ReviewFormState;
        this.Location = Properties.Settings.Default.ReviewFormLocation;
        this.Size = Properties.Settings.Default.ReviewFormSize;
    }
}

After a remote session

As I said earlier, I finished a remote session. I found out this is because of a scale issue. But still, it seems pretty strange. I've checked [display settings - scale and layout] menu and it was 125%. Then I open the form and repeat. I found the form size is growing. I think it was 1.25 times on the first try. What I did was I changed the scale on 100%, then did the same thing open and repeat. For this time, I didn't find anything; the form was in the same size.

My middle step conclusion is that I need to save a pure size of the form. I will translate according to the scale. Then I guess, I can get a real size of the form. I'm not really sure I will try and share what I find for letting someone out in the loop.

@Jimi and @Louis Go I hope that one day I can help you too. Thank you for helping me.

Community
  • 1
  • 1
JunhongKim
  • 73
  • 5
  • If you have win10 environment, try to tweaking "Display Settings" -> "Change the size of text, apps". It might reproduce your problem. – Louis Go Feb 07 '20 at 01:53
  • 2
    Make your application DpiAware if haven't. Start from here: [How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?](https://stackoverflow.com/a/13228495/7444103), then take a look at the app.config settings and to [High DPI support in Windows Forms](https://learn.microsoft.com/en-us/dotnet/framework/winforms/high-dpi-support-in-windows-forms#configuring-your-windows-forms-app-for-high-dpi-support) (consider and test the `PerMonitorV2` setup). Take into consideration the Form's `AutoScaleMode`. See the difference between `Dpi` and `Font`. – Jimi Feb 07 '20 at 02:03

1 Answers1

0

Jimi:

Make your application DpiAware if haven't. Start from here: How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?, then take a look at the app.config settings and to High DPI support in Windows Forms (consider and test the PerMonitorV2 setup). Take into consideration the Form's AutoScaleMode. See the difference between Dpi and Font.

yacc
  • 2,915
  • 4
  • 19
  • 33