How to determine the screen width/height using C#

前端 未结 6 622
再見小時候
再見小時候 2021-02-03 23:48

I want to set the width & height of a Window dynamically based on the user screens maximum width/height. How can I determine this programmatically?

6条回答
  •  萌比男神i
    2021-02-04 00:20

    You can use the SizeChanged event

    SizeChanged="MyWindow_SizeChanged"
    

    Then in your event handler,

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if (this.MinWidth > 0 && this.MinHeight > 0)
        {
            double heightScaleFactor = e.NewSize.Height / this.MinHeight;
            double widthScaleFactor = e.NewSize.Width / this.MinWidth;            
            mainGrid.LayoutTransform = new ScaleTransform(heightScaleFactor, widthScaleFactor);
        }
    }
    

    where MainGrid is a container for all the contents in MyWindow.

提交回复
热议问题