WPFで次のコードを使用して解像度を検出します。
double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;
私の画面の現在の解像度は1920*1200ですが、height
960.0width
で1536.0です!!!
どうしたの ?
前もって感謝します。
WPFで次のコードを使用して解像度を検出します。
double height = System.Windows.SystemParameters.PrimaryScreenHeight;
double width = System.Windows.SystemParameters.PrimaryScreenWidth;
私の画面の現在の解像度は1920*1200ですが、height
960.0width
で1536.0です!!!
どうしたの ?
前もって感謝します。
すべてのWPFの場所とサイズは、1/96インチの単位の浮動小数点であることに注意してください。ピクセルではありません。これにより、ウィンドウデザインの解像度に依存しなくなります。計算を行う:高さ= 960/96=10インチ。ビデオアダプタを120DPI(120/96 = 125%)に設定した場合:10 * 120=1200ピクセル。幅も同じ:1536/96 * 120=1920ピクセル。
System.Windows.Formsは、ピクセル単位で機能します。タスクバーの高さを差し引くため、高さは1050未満になります。ただし、WPFの場合は、ピクセルではなく、常に1/96インチで作業する必要があります。
さらに堅牢な実装を行うには、システムのDPI係数を計算し、それらの係数を使用する必要があります。通常のDPI値は96ですが、モニターによっては値が異なる場合があります。コードが96とは異なるDPI値を持つモニターで実行されている可能性があることを考慮してください。次のコードを検討してください。
private static void CalculateDpiFactors()
{
Window MainWindow = Application.Current.MainWindow;
PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
thisDpiWidthFactor = m.M11;
thisDpiHeightFactor = m.M22;
}
次に、これらの比率を使用して、最終的な値を取得できます。
CalculateDpiFactors();
double ScreenHeight = SystemParameters.PrimaryScreenHeight * thisDpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * thisDpiWidthFactor;
ScreenHeightとScreenWidthの値は、モニターの[プロパティ]ウィンドウに表示される値と一致する必要があります。
Windowsがロードされた後、これを呼び出してください。
public static class Ext
{
public static Size GetNativePrimaryScreenSize(this Window window)
{
PresentationSource mainWindowPresentationSource = PresentationSource.FromVisual(window);
Matrix m = mainWindowPresentationSource.CompositionTarget.TransformToDevice;
var dpiWidthFactor = m.M11;
var dpiHeightFactor = m.M22;
double screenHeight = SystemParameters.PrimaryScreenHeight * dpiHeightFactor;
double screenWidth = SystemParameters.PrimaryScreenWidth * dpiWidthFactor;
return new Size(screenWidth, screenHeight);
}
}
SystemParameters.FullPrimaryScreenWidthとFullPrimaryScreenHeightを試してください。PrimaryScreenWidthとHeightは、画面上のタスクバーやその他のデスクバンドを削除した後、使用可能なクライアントウィンドウのサイズを返すと思います。
代わりにこれを使用できます:http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.primaryscreen.aspx
「WindowsXPスタイルのDPIスケーリングを使用する」にチェックを入れると、「Screen.PrimaryScreen.Bounds」の戻り値は正しいです。そうでない場合、戻り値はDPI値(デフォルト)によって比例的に縮小されます。
[Windows XPスタイルのDPIスケーリングを使用する]チェックボックスを見つけるには、次のリンクの[高DPI用に設計されていないプログラムでテキストと画面上のアイテムを明確にする]リンクを展開します: http://windows.microsoft。 com / en-us / windows-vista / Make-the-text-on-your-screen-larger-or-smaller
あなたはそれを使うことができます:
float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
}
double screenX = SystemParameters.PrimaryScreenWidth / 96 * dpiX;
double screenY = SystemParameters.PrimaryScreenHeight / 96 * dpiY;
これらを試してください..私はこれがエラーを修正できると信じています.....
System.Windows.Form1.Screen.PrimaryScreen.Bounds.Height; System.Windows.Form1.Screen.PrimaryScreen.Bounds.Widtht;