How to determine the screen width/height using C#

前端 未结 6 531
再見小時候
再見小時候 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:29

    If you want the specific dimensions of the monitor your program is running on (if someone is running more than one monitor) you could also use:

    var helper = new WindowInteropHelper(this); //this being the wpf form 
    var currentScreen = Screen.FromHandle(helper.Handle);
    

    This will return a screen object referencing the monitor the program is running on. From there you can use the currentScreen.Bounds.Width / Height property (for the full size) or the currentScreen.WorkingArea.Width / Height (minus task bar, etc.) depending on what you want.

提交回复
热议问题