How to get the screen size of Console Application?

删除回忆录丶 提交于 2020-01-23 07:10:54

问题


How do you get the screen size in a console application?


回答1:


var w = Console.WindowWidth;
var h = Console.WindowHeight;

--EDIT--

Screen.PrimaryScreen.Bounds 
Screen.PrimaryScreen.WorkingArea



回答2:


If you want get display size you can use WMI(Windows Management Instrumentation)

        var scope = new ManagementScope();
        scope.Connect();

        var query = new ObjectQuery("SELECT * FROM Win32_VideoController");

        using (var searcher = new ManagementObjectSearcher(scope, query))
        {
            var results = searcher.Get();
            foreach (var result in results)
            {
                Console.WriteLine("Horizontal resolution: " + result.GetPropertyValue("CurrentHorizontalResolution"));
                Console.WriteLine("Vertical resolution: " + result.GetPropertyValue("CurrentVerticalResolution"));
            }               
        }



回答3:


If you are looking to change the size of a console application with the size of the screen we first have to get the largest width and height of the window.

var largestWindowX = Console.LargestWindowWidth;
var largestWindowY = Console.LargestWindowHeight;

For a screen that is 1920x1080 the largest X and Y values are X: 240 and Y: 66. By writing the values out to the screen, we can then change the screen size manually to get the respective X and Y coordinates of that screen size.

Now we take the largest value of the last screen size in this case 1600x1024 which is X: 200 and Y: 62 and check if the screen is bigger then or equal to the display and make the console window this big.

if(largestWindowX >= 200 && largestWindowY >= 62)
{
    //The values 200 and 40 can be changed depending on what you are making
    Console.SetWindowSize(200, 40);
}
//add more if else statments depending how you wish to scale it as.
else
{
    //Otherwise any screen size smaller than the sizes added make it this...
    Console.SetWindowSize(/*Your X value*/, /*Your Y value*/);
}

If you are looking for a way to find the Display size from the OS, that I have no clue as I am not an advanced programmer, just know some of this vast language.

Though, you could technically use the values of the variables above. for example:

if(largestWindowX == 240 && largestWindowY == 66)
{
     Console.WriteLine("This is a 1920x1080 screen");
}

I am not exactly sure that would be an accurate way of telling the screen size. Otherwise if full screen is necessary Alt+Enter works...



来源:https://stackoverflow.com/questions/10675732/how-to-get-the-screen-size-of-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!