Jak uzyskać rozmiar bieżącego ekranu w WPF?
Wiem, że mogę uzyskać rozmiar głównego ekranu za pomocą
System.Windows.SystemParameters.PrimaryScreenWidth;
System.Windows.SystemParameters.PrimaryScreenHeight;
Ale jak uzyskać rozmiar bieżącego ekranu? (Użytkownicy wielu ekranów nie zawsze używają ekranu głównego i nie wszystkie ekrany używają tej samej rozdzielczości, prawda?)
Byłoby miło móc uzyskać dostęp do rozmiaru z XAML, ale zrobienie tego z kodu (C#) wystarczyłoby.
9 answers
Z tego co wiem nie ma natywnej funkcji WPF do pobierania wymiarów bieżącego monitora. Zamiast tego możesz przypiąć natywne wiele funkcji monitorów wyświetlania , owinąć je w zarządzaną klasę i ujawnić wszystkie właściwości potrzebne do użycia ich z XAML.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-18 11:17:09
Stworzyłem małe opakowanie wokół ekranu z systemu.Okna.Formularze, obecnie Wszystko działa... Nie jestem jednak pewien "pikseli niezależnych od urządzenia".
public class WpfScreen
{
public static IEnumerable<WpfScreen> AllScreens()
{
foreach (Screen screen in System.Windows.Forms.Screen.AllScreens)
{
yield return new WpfScreen(screen);
}
}
public static WpfScreen GetScreenFrom(Window window)
{
WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);
Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
WpfScreen wpfScreen = new WpfScreen(screen);
return wpfScreen;
}
public static WpfScreen GetScreenFrom(Point point)
{
int x = (int) Math.Round(point.X);
int y = (int) Math.Round(point.Y);
// are x,y device-independent-pixels ??
System.Drawing.Point drawingPoint = new System.Drawing.Point(x, y);
Screen screen = System.Windows.Forms.Screen.FromPoint(drawingPoint);
WpfScreen wpfScreen = new WpfScreen(screen);
return wpfScreen;
}
public static WpfScreen Primary
{
get { return new WpfScreen(System.Windows.Forms.Screen.PrimaryScreen); }
}
private readonly Screen screen;
internal WpfScreen(System.Windows.Forms.Screen screen)
{
this.screen = screen;
}
public Rect DeviceBounds
{
get { return this.GetRect(this.screen.Bounds); }
}
public Rect WorkingArea
{
get { return this.GetRect(this.screen.WorkingArea); }
}
private Rect GetRect(Rectangle value)
{
// should x, y, width, height be device-independent-pixels ??
return new Rect
{
X = value.X,
Y = value.Y,
Width = value.Width,
Height = value.Height
};
}
public bool IsPrimary
{
get { return this.screen.Primary; }
}
public string DeviceName
{
get { return this.screen.DeviceName; }
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-10 05:07:31
Tu budy. To da Ci tylko szerokość i wysokość obszaru roboczego
System.Windows.SystemParameters.WorkArea.Width
System.Windows.SystemParameters.WorkArea.Height
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-09 18:33:47
Poświęć trochę czasu na skanowanie przez członków SystemParameters.
- VirtualScreenWidth
- VirtualScreenHeight
Uwzględniają one nawet względne pozycje ekranów.
Testowane tylko z dwoma monitorami.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-26 15:27:44
To da ci bieżący ekran oparty na lewym górnym rogu okna po prostu zadzwoń.CurrentScreen (), aby uzyskać informacje na bieżącym ekranie.
using System.Windows;
using System.Windows.Forms;
namespace Common.Helpers
{
public static class WindowHelpers
{
public static Screen CurrentScreen(this Window window)
{
return Screen.FromPoint(new System.Drawing.Point((int)window.Left,(int)window.Top));
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-10 20:15:17
Jeśli jesteś zaznajomiony z używaniem Systemu.Okna.Forms class wtedy możesz po prostu dodać referencję Systemu.Okna.Forms class to your project:
Eksplorator rozwiązań -> linki -> Dodaj linki... -> ( Assemblies: Framework) - > przewiń w dół i sprawdź System.Okna.Forms assembly -> OK .
Teraz możesz dodać używając systemu.Okna.Forms; statement and use screen in your wpf project just jak wcześniej.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-24 04:39:41
Dlaczego po prostu tego nie użyć?
var interopHelper = new WindowInteropHelper(System.Windows.Application.Current.MainWindow);
var activeScreen = Screen.FromHandle(interopHelper.Handle);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-15 09:14:12
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenhight= System.Windows.SystemParameters.PrimaryScreenHeight;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-02 18:34:19
Działa z
this.Width = System.Windows.SystemParameters.VirtualScreenWidth;
this.Height = System.Windows.SystemParameters.VirtualScreenHeight;
Testowane na 2 monitorach.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-21 07:27:26