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.
How to get real value of Screen Size in C# WPF Application
You can monitor the SystemEvents
for changes.
using Microsoft.Win32;
Add this to your constructor or the like:
SystemEvents.DisplaySettingsChanged +=
new EventHandler(SystemEvents_DisplaySettingsChanged);
Here's the event handler. I have the data bound to 2 TextBoxes so I could see the values change as I messed with the resolution, including telling it to revert.
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
data.ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
data.ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
}
In your case, just change the assignment to point to where you want to store it.
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);
}
}
How to obtain screen size from xaml?
This will work. You can read more here about SystemParameters
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}}" />
<TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}" />
</StackPanel>
</Window>
Related Topics
How to Get the File Size from Http Headers
Dynamically Generate Linq Queries
Check If Number Is Prime Number
Wait Until File Is Completely Written
Serializing Private Member Data
Unrecognized Escape Sequence for Path String Containing Backslashes
How to Read a Large (1 Gb) Txt File in .Net
C# Sort and Orderby Comparison
Can Console.Clear Be Used to Only Clear a Line Instead of Whole Console
How to Get the Size of the Current Screen in Wpf
How to Create Bitmap from Byte Array
Calling Generic Method with a Type Argument Known Only at Execution Time
Execute Multiple Command Lines with the Same Process Using .Net
How Create a New Deep Copy (Clone) of a List<T>
How Do Arrays in C# Partially Implement Ilist<T>
Best Way to Access a Control on Another Form in Windows Forms