07. Get screen resolution on WINDOWS10

Source: Internet
Author: User

Because on Win10, the app is not fully occupied by default when it is running, and Windows Runtime does not currently provide access

The screen size of the API. In the desktop Win32 API, you can get the screen size, but because it is a limited API, it cannot be used directly.

Screen size of my current PC:

Method One: Get the screen resolution by invoking JavaScript in the WebView control:

Put a WebView control in the page:

<x:name= "WebView"  loadcompleted= "webview_loadcompleted"  />

  

In the page load completion event, add the JS code:

  Private voidWebview_test_loaded (Objectsender, RoutedEventArgs e) {      //webview.navigatetostring ("<script>" +//"window.onload = function loaded () {" +//"document.write (' screen resolution: ' + Window.screen.width + ' X ' + window.screen.height);}" +      //"</script>");
//in the WebView control, add a method to get the screen resolutionWebview.navigatetostring (""); } Private Async voidWebview_loadcompleted (Objectsender, NavigationEventArgs e) { //call the Javascript method in C # to get the return value varstr =awaitWebview.invokescriptasync ("getscreensize",NULL); Debug.WriteLine (str); }


To run the app:

Mode two: By default when the app first launches, the window is in full screen mode, and then the window size is obtained.

When full screen, the window size is the size of the screen, you can store the obtained size in the Settings, for

Used later. The disadvantage is that the default first full screen boot, may not be the user wants the behavior, cannot force the user.

Call the Applicationview API:

  Windows.UI.ViewManagement.ApplicationView view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView ( );   // there is a "try" word in the method name, that is, the "Show full screen" failure occurs   view. Tryenterfullscreenmode ();   // If the screen size is read immediately, it is not full-screen. After a full screen at a later time, get the screen size  "" + Window.Current.Bounds.Height);            

Other Applicationview operations (not related to this article):

Windows.UI.ViewManagement.ApplicationView view =Windows.UI.ViewManagement.ApplicationView.GetForCurrentView (); Private voidButton_full_click (Objectsender, RoutedEventArgs e) {Debug.WriteLine ("Full screen operation:"+view.            Tryenterfullscreenmode ()); BOOLIsfull =view.        IsFullScreen; }        Private voidButton_exitfull_click (Objectsender, RoutedEventArgs e) {view.            Exitfullscreenmode (); Debug.WriteLine ("Exit Full Screen"); }        Private voidButton_resize_click (Objectsender, RoutedEventArgs e) {Debug.WriteLine ("To change the view:"+ View. Tryresizeview (NewSize ( -, the))); }        Private voidButton_boundsmode_click (Objectsender, RoutedEventArgs e) {            if(View. Desiredboundsmode = =Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible) {view.                Setdesiredboundsmode (Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseCoreWindow); Debug.WriteLine ("set to: Applicationviewboundsmode.usecorewindow"); }            Else{view.                Setdesiredboundsmode (Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseVisible); Debug.WriteLine ("set to: Applicationviewboundsmode.usevisible"); }        }        //Set Window Size        Private voidButton_mini_size_click (Objectsender, RoutedEventArgs e) {view. Setpreferredminsize (NewSize ( -, -)); }        Private voidButton_standard_overlays_click (Objectsender, RoutedEventArgs e) {view.        Showstandardsystemoverlays (); }        //get the dimensions and position of a view        Private voidButton_visiblebounds_click (Objectsender, RoutedEventArgs e) {Rect rect=view.            Visiblebounds; Debug.WriteLine (string. Format ("visiblebounds----left:{0}, Right:{1}, width:{2}, height:{3}", Rect. Left, rect. Right, rect. Width, Rect.        Height)); }        Private voidButton_preferredlaunchviewsize_click (Objectsender, RoutedEventArgs e) {Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize=NewSize ( -, -); }        //it's expired.        Private voidButton_applicationviewstate_click (Objectsender, RoutedEventArgs e) {Debug.WriteLine ("applicationviewstate:"+Windows.UI.ViewManagement.ApplicationView.Value.ToString ()); }        Private voidButton_global_back_click (Objectsender, RoutedEventArgs e) {          varCurview =Windows.UI.Core.SystemNavigationManager.GetForCurrentView (); if(Curview.appviewbackbuttonvisibility = =Windows.UI.Core.AppViewBackButtonVisibility.Visible) {curview.appviewbackbuttonvisibility =Windows.UI.Core.AppViewBackButtonVisibility.Collapsed; }            Else{curview.appviewbackbuttonvisibility=Windows.UI.Core.AppViewBackButtonVisibility.Visible; }        }
View Code

07. Get screen resolution on WINDOWS10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.