How can I get the active screen dimensions?
Screen.FromControl
, Screen.FromPoint
and Screen.FromRectangle
should help you with this. For example in WinForms it would be:
class MyForm : Form
{
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
}
I don't know of an equivalent call for WPF. Therefore, you need to do something like this extension method.
static class ExtensionsForWPF
{
public static System.Windows.Forms.Screen GetScreen(this Window window)
{
return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
}
}
Get screen size in pixels in windows form in C#
Check the Screen class and its property Bounds
Screen.PrimaryScreen.Bounds.Width;
Screen.PrimaryScreen.Bounds.Height;
Screen.PrimaryScreen.Bounds.Size;
How to get the size of the current screen in WPF?
As far as I know there is no native WPF function to get dimensions of the current monitor. Instead you could PInvoke native multiple display monitors functions, wrap them in managed class and expose all properties you need to consume them from XAML.
Get the size of the screen, current web page and browser window
You can get the size of the window or document with jQuery:
// Size of browser viewport.
$(window).height();
$(window).width();
// Size of HTML document (same as pageHeight/pageWidth in screenshot).
$(document).height();
$(document).width();
For screen size you can use the screen
object:
window.screen.height;
window.screen.width;
Get screen width and height in Android
Using this code, you can get the runtime display's width & height:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
In a view you need to do something like this:
((Activity) getContext()).getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);
In some scenarios, where devices have a navigation bar, you have to check at runtime:
public boolean showNavigationBar(Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}
If the device has a navigation bar, then count its height:
private int getNavigationBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
So the final height of the device is:
int height = displayMetrics.heightPixels + getNavigationBarHeight();
In C, how to get the geometry (x, y, width, height) of the active monitor within the screen using X11
You need to use the RandR extension API. Get the source code of the xrandr
utility and steal what you need from there. You need XRRGetMonitors
function and related data structures. You can also use xrandr
to get the information you need on the command line.
I don't know if there's ever such thing as active monitor, because an active window can span several monitors, and a pointer can be in another monitor still. Anyway, the XRRMonitorInfo
structure contains all the information about your monitor geometry, including its size (in pixels and mm) and its position on the logical screen, so you can extract a monitor description from a pair of screen coordinates.
See also this answer.
Related Topics
Why Firefox Requires Geckodriver
Is There a Jquery-Like CSS/HTML Selector That Can Be Used in C#
How to Check for a Network Connection
Casting a Result to Float in Method Returning Float Changes Result
Deserializing Dates with Dd/Mm/Yyyy Format Using JSON.Net
C#: Raising an Inherited Event
Task Sequencing and Re-Entracy
.Net Hashtable VS Dictionary - Can the Dictionary Be as Fast
How to Get the Propertyinfo of a Specific Property
Windows Application Startup Error Exception Code: 0Xe0434352
Cancellation Token in Task Constructor: Why
Authorize by Group in Azure Active Directory B2C
Read from Location on Console C#
Are There Any Fuzzy Search or String Similarity Functions Libraries Written for C#
Does .Net Have a Way to Check If List a Contains All Items in List B