Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

437
Visualizações
Deshabilitar la escala de DPI alta

Quiero obtener los datos correctos de mi código cuando la escala de pantalla está habilitada. Así que estoy usando la función SetProcessDPIAware() :

 using namespace System.Windows.Forms Clear-Host Add-Type -AssemblyName System.Windows.Forms Add-Type -Name User32 -Namespace W32 ' [DllImport("user32.dll")] public static extern bool SetProcessDPIAware();' [Cursor]::Position | Write-Host [Screen]::PrimaryScreen.Bounds.Size | Write-Host [SystemInformation]::PrimaryMonitorSize | Write-Host [W32.User32]::SetProcessDPIAware() [Cursor]::Position | Write-Host [Screen]::PrimaryScreen.Bounds.Size | Write-Host [SystemInformation]::PrimaryMonitorSize | Write-Host

Producción:

 {X=630,Y=313} # ❌ incorrect data {Width=2048, Height=864} # ❌ incorrect data {Width=2048, Height=864} # ❌ incorrect data True {X=788,Y=391} # ✔️ data became correct {Width=2048, Height=864} # ❌ still incorrect data {Width=2560, Height=1080} # ✔️ data became correct

La misma situación con el código C#:

 using System; using System.Windows.Forms; using System.Runtime.InteropServices; public class Class1 { [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); public static void Main() { Console.WriteLine("\n"); Console.WriteLine(Cursor.Position); Console.WriteLine(Screen.PrimaryScreen.Bounds.Size); Console.WriteLine(SystemInformation.PrimaryMonitorSize); Console.WriteLine(SetProcessDPIAware()); Console.WriteLine(Cursor.Position); Console.WriteLine(Screen.PrimaryScreen.Bounds.Size); Console.WriteLine(SystemInformation.PrimaryMonitorSize); } }

Entonces, ¿es esto un error de Screen.PrimaryScreen.Bounds o tengo que hacer algo más para obtener los datos correctos de esta propiedad?

Y si la única forma es usar la clase SystemInformation , entonces, ¿cómo obtengo datos sobre el segundo monitor, no solo sobre el principal?

O ¿Hay alguna forma de establecer esta configuración para powershell.exe/powershell_ise.exe?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Esto es lo que uso y funciona para mí:

 Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; using System.Drawing; public class DPI { [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); public enum DeviceCap { VERTRES = 10, DESKTOPVERTRES = 117 } public static float scaling() { Graphics g = Graphics.FromHwnd(IntPtr.Zero); IntPtr desktop = g.GetHdc(); int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES); int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); return (float)PhysicalScreenHeight / (float)LogicalScreenHeight; } } '@ -ReferencedAssemblies System.Drawing -IgnoreWarnings -WarningAction Ignore

Luego puede llamar a esta clase y obtener el DPI como:

 $DPI = [math]::round([dpi]::scaling(), 2) * 100 $bounds = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea $bounds.Width = ($bounds.Width / 100) * $DPI $bounds.Height = ($bounds.Height / 100) * $DPI

Y, por último, siempre use tamaños relativos como:

 $form.Size = [System.Drawing.Size]::new(($bounds.Width / X),($bounds.Height / X))
 $form.Add_Resize({ $txtBox.Size = [System.Drawing.Size]::new(($this.Width - X), XX) })

Y así sucesivamente, donde X puede ser un int o double .

  • Fuente de la clase . No estoy seguro de si este es el OP, ya que he visto el mismo código publicado en diferentes sitios.
  • Ejemplo de formulario DPI Aware: https://stackoverflow.com/a/68240960/15339544

  • Monitor izquierdo: 1920x1080 - Escala 175%
  • Monitor derecho: 1920x1080 - 100 % de escala

2 meses

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda