Rubrik: System/Windows · Desktop/Bildschirm/Display | VB-Versionen: VB2022 | 27.07.25 |
![]() Diese Lösung berechnet die Zollgrößen aus Millimeter-Größen von Breite und Höhe der Monitore | ||
Autor: ![]() | Bewertung: ![]() ![]() ![]() ![]() ![]() | Views: 259 |
ohne Homepage | System: Win7, Win8, Win10, Win11 | kein Beispielprojekt |
Der betreffende Code wurde von mir in Zusammenarbeit mit "VB-Paradise 2.0 - Die große Visual-Basic- und .NET-Community" erstellt:
Imports System.Runtime.InteropServices Imports System.Runtime.CompilerServices Module dhmodScreenExtensions Private Const HORZSIZE As Integer = 4 Private Const VERTSIZE As Integer = 6 <DllImport("Gdi32.dll", EntryPoint:="CreateDCW")> Private Function CreateDC(<[In], MarshalAs(UnmanagedType.LPWStr)> pwszDriver As String, <[In], MarshalAs(UnmanagedType.LPWStr)> pwszDevice As String, <[In], MarshalAs(UnmanagedType.LPWStr)> pszPort As String, <[In]> pdm As IntPtr) As IntPtr End Function <DllImport("Gdi32.dll", EntryPoint:="DeleteDC")> Private Function DeleteDC(<[In]> hDC As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function <DllImport("Gdi32.dll", EntryPoint:="GetDeviceCaps")> Private Function GetDeviceCaps(<[In]> hDC As IntPtr, <[In]> index As Integer) As Integer End Function <Extension> Public Function HorizontalSizeInMillimeter(scr As Screen) As Integer Dim Ret As Integer Dim hDC As IntPtr = CreateDC(scr.DeviceName, Nothing, Nothing, Nothing) If Not Equals(hDC, IntPtr.Zero) Then Ret = GetDeviceCaps(hDC, HORZSIZE) DeleteDC(hDC) End If Return Ret End Function <Extension> Public Function VerticalSizeInMillimeter(scr As Screen) As Integer Dim Ret As Integer Dim hDC As IntPtr = CreateDC(scr.DeviceName, Nothing, Nothing, Nothing) If Not Equals(hDC, IntPtr.Zero) Then Ret = GetDeviceCaps(hDC, VERTSIZE) DeleteDC(hDC) End If Return Ret End Function <Extension> Public Function DiagonalSizeInZoll(scr As Screen) As Integer Return Math.Sqrt(HorizontalSizeInMillimeter(scr) ^ 2 + VerticalSizeInMillimeter(scr) ^ 2) / 25.4 End Function End Module
und wird so angewendet bspw. in einem Form_Load:
' Zollmaße der Monitore Dim v(Screen.AllScreens.Length) As Short v = {Screen.AllScreens(0).DiagonalSizeInZoll, Screen.AllScreens(1).DiagonalSizeInZoll}
v enthält in dem Fall die Zollmaße von 2 Monitoren im System