.NET WPF Remember window size between sessions

后端 未结 12 911
悲哀的现实
悲哀的现实 2020-11-30 20:33

Basically when user resizes my application\'s window I want application to be same size when application is re-opened again.

At first I though of handling SizeChange

12条回答
  •  春和景丽
    2020-11-30 20:52

    I'm using the answer from Lance Cleveland and bind the Setting. But i'm using some more Code to avoid that my Window is out of Screen.

    private void SetWindowSettingsIntoScreenArea()
    {
        // first detect Screen, where we will display the Window
        // second correct bottom and right position
        // then the top and left position.
        // If Size is bigger than current Screen, it's still possible to move and size the Window
    
        // get the screen to display the window
        var screen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point((int)Default.Left, (int)Default.Top));
    
        // is bottom position out of screen for more than 1/3 Height of Window?
        if (Default.Top + (Default.Height / 3) > screen.WorkingArea.Height)
            Default.Top = screen.WorkingArea.Height - Default.Height;
    
        // is right position out of screen for more than 1/2 Width of Window?
        if (Default.Left + (Default.Width / 2) > screen.WorkingArea.Width)
            Default.Left = screen.WorkingArea.Width - Default.Width;
    
        // is top position out of screen?
        if (Default.Top < screen.WorkingArea.Top)
            Default.Top = screen.WorkingArea.Top;
    
        // is left position out of screen?
        if (Default.Left < screen.WorkingArea.Left)
            Default.Left = screen.WorkingArea.Left;
    }
    

提交回复
热议问题