toda tamaño resolucion que posicionar pantalla ocupe obtener net maximizar hacer formulario form asp ancho ajustar c# wpf

c# - tamaño - ¿Cómo puedo obtener las dimensiones de la pantalla activa?



screen en c# (10)

Añadiendo una solución que no usa WinForms sino NativeMethods. Primero necesita definir los métodos nativos necesarios.

public static class NativeMethods { public const Int32 MONITOR_DEFAULTTOPRIMERTY = 0x00000001; public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002; [DllImport( "user32.dll" )] public static extern IntPtr MonitorFromWindow( IntPtr handle, Int32 flags ); [DllImport( "user32.dll" )] public static extern Boolean GetMonitorInfo( IntPtr hMonitor, NativeMonitorInfo lpmi ); [Serializable, StructLayout( LayoutKind.Sequential )] public struct NativeRectangle { public Int32 Left; public Int32 Top; public Int32 Right; public Int32 Bottom; public NativeRectangle( Int32 left, Int32 top, Int32 right, Int32 bottom ) { this.Left = left; this.Top = top; this.Right = right; this.Bottom = bottom; } } [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Auto )] public sealed class NativeMonitorInfo { public Int32 Size = Marshal.SizeOf( typeof( NativeMonitorInfo ) ); public NativeRectangle Monitor; public NativeRectangle Work; public Int32 Flags; } }

Y luego obtenga la manija del monitor y la información del monitor como esta.

var hwnd = new WindowInteropHelper( this ).EnsureHandle(); var monitor = NativeMethods.MonitorFromWindow( hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST ); if ( monitor != IntPtr.Zero ) { var monitorInfo = new NativeMonitorInfo(); NativeMethods.GetMonitorInfo( monitor, monitorInfo ); var left = monitorInfo.Monitor.Left; var top = monitorInfo.Monitor.Top; var width = ( monitorInfo.Monitor.Right - monitorInfo.Monitor.Left ); var height = ( monitorInfo.Monitor.Bottom - monitorInfo.Monitor.Top ); }

Lo que estoy buscando es el equivalente de System.Windows.SystemParameters.WorkArea para el monitor en el que está actualmente la ventana.

Aclaración: la ventana en cuestión es WPF , no WinForm .


Agregar a ffpf

Screen.FromControl(this).Bounds


Esta es una " solución Center Screen DotNet 4.5 ", que utiliza SystemParameters lugar de System.Windows.Forms o My.Compuer.Screen : dado que Windows 8 ha cambiado el cálculo de la dimensión de la pantalla, la única forma en que funciona para mí es así (cálculo de la barra de tareas) incluido):

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded Dim BarWidth As Double = SystemParameters.VirtualScreenWidth - SystemParameters.WorkArea.Width Dim BarHeight As Double = SystemParameters.VirtualScreenHeight - SystemParameters.WorkArea.Height Me.Left = (SystemParameters.VirtualScreenWidth - Me.ActualWidth - BarWidth) / 2 Me.Top = (SystemParameters.VirtualScreenHeight - Me.ActualHeight - BarHeight) / 2 End Sub


Necesitaba establecer el tamaño máximo de mi aplicación de ventana. Este podría cambiar en consecuencia, la aplicación se muestra en la pantalla principal o en la secundaria. Para superar este problema, creé un método simple que te mostraré a continuación:

/// <summary> /// Set the max size of the application window taking into account the current monitor /// </summary> public static void SetMaxSizeWindow(ioConnect _receiver) { Point absoluteScreenPos = _receiver.PointToScreen(Mouse.GetPosition(_receiver)); if (System.Windows.SystemParameters.VirtualScreenLeft == System.Windows.SystemParameters.WorkArea.Left) { //Primary Monitor is on the Left if (absoluteScreenPos.X <= System.Windows.SystemParameters.PrimaryScreenWidth) { //Primary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.WorkArea.Height; } else { //Secondary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.VirtualScreenWidth - System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.VirtualScreenHeight; } } if (System.Windows.SystemParameters.VirtualScreenLeft < 0) { //Primary Monitor is on the Right if (absoluteScreenPos.X > 0) { //Primary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.WorkArea.Height; } else { //Secondary monitor _receiver.WindowApplication.MaxWidth = System.Windows.SystemParameters.VirtualScreenWidth - System.Windows.SystemParameters.WorkArea.Width; _receiver.WindowApplication.MaxHeight = System.Windows.SystemParameters.VirtualScreenHeight; } } }



Quería tener la resolución de pantalla antes de abrir la primera de mis ventanas, así que aquí una solución rápida para abrir una ventana invisible antes de medir realmente las dimensiones de la pantalla (debe adaptar los parámetros de la ventana a su ventana para asegurarse de que ambos estén abiertos) la misma pantalla, principalmente WindowStartupLocation es importante)

Window w = new Window(); w.ResizeMode = ResizeMode.NoResize; w.WindowState = WindowState.Normal; w.WindowStyle = WindowStyle.None; w.Background = Brushes.Transparent; w.Width = 0; w.Height = 0; w.AllowsTransparency = true; w.IsHitTestVisible = false; w.WindowStartupLocation = WindowStartupLocation.Manual; w.Show(); Screen scr = Screen.FromHandle(new WindowInteropHelper(w).Handle); w.Close();



Tenga cuidado con el factor de escala de sus ventanas (100% / 125% / 150% / 200%). Puede obtener el tamaño de pantalla real usando el siguiente código:

SystemParameters.FullPrimaryScreenHeight SystemParameters.FullPrimaryScreenWidth


en C # winforms tengo el punto de inicio (para el caso cuando tenemos varios monitores / diplay y un formulario está llamando a otro) con la ayuda del siguiente método:

private Point get_start_point() { return new Point(Screen.GetBounds(parent_class_with_form.ActiveForm).X, Screen.GetBounds(parent_class_with_form.ActiveForm).Y ); }


Screen.FromControl , Screen.FromPoint y Screen.FromRectangle deberían ayudarlo con esto. Por ejemplo, en WinForms sería:

class MyForm : Form { public Rectangle GetScreen() { return Screen.FromControl(this).Bounds; } }

No sé de una llamada equivalente para WPF. Por lo tanto, debe hacer algo como este método de extensión.

static class ExtensionsForWPF { public static System.Windows.Forms.Screen GetScreen(this Window window) { return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle); } }