How to determine the screen width/height using C#

前端 未结 6 626
再見小時候
再見小時候 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条回答
  •  半阙折子戏
    2021-02-04 00:25

    You can get the screen height and width:

    int height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;
    int width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
    

    Then set the Window's Height and Width properties to those in the Initialization.

    this.Height = height;
    this.Width = width;
    

    Works to get the screen's height and width in WinForms or in ASP .NET. No muss, no fuss, except you'll need to reference the System.Windows.Forms assembly in your project if it's not a WinForm project.

提交回复
热议问题