7

WPF には、ウィンドウをデスクトップの中央に配置するための「CenterScreen」値があることを知っています。ただし、デュアル モニターでは、これはあまり快適ではありません。

プライマリ モニターを中心にするにはどうすればよいですか? プライマリ デスクトップの検出、そのジオメトリの取得などの歌とダンスを実行する必要がありますか、それともより良い方法がありますか?

4

4 に答える 4

6

複数の画面は少し問題であり、それらを処理するための組み込みの適切にラップされた方法はありませんが、いくつかの数学と SystemParameters を使用すると、それを実行できます。

ウィンドウを (0,0) の位置に配置すると、プライマリ画面の左上隅になります。したがって、ウィンドウをその画面の中央に表示するには、次のようにします。

this.Left = (SystemParameters.PrimaryScreenWidth / 2) - (this.ActualWidth / 2);
this.Top = (SystemParameters.PrimaryScreenHeight / 2) - (this.ActualHeight / 2);

基本的な考え方は単純なので、説明する必要はありません。

このコードは C# 用ですが、VB にも同様のものがあると確信しています。

また、NaN 値を保持できるため、Width\Height プロパティではなく、ActualWidth\ActualHeight プロパティを使用する必要があります。

幸運を。

于 2009-09-08T12:55:20.987 に答える
0

同じ問題がありました。残念ながら、TA とクライアントとの多くの話し合いの結果、(お金と時間の点で) プライマリ モニターで最大化する方が価値があると判断されました。

于 2009-09-08T05:28:31.073 に答える
0

これは、ウィンドウをプライマリ モニターの中央に配置し、その周囲に空のスペースの境界線を配置する純粋な WPF ソリューションです (最大化したくないため)。私のセットアップは、左側に正方形のモニター、右側にワイドスクリーンです。これは、各モニターを Windows のプライマリ モニターとして設定してテストされました。

System.Windows.SystemParametersその解決策にたどり着く前に、さまざまな高さを与える3 つの便利なプロパティがあります。与えられた数値は、私の 1920x1080 ワイドスクリーンのものです。

  • PrimaryScreenHeight- 1080。Windows で設定された実際の解像度の高さ。
  • WorkArea.Height- 1040。実際の​​解像度の高さからスタート バーを引いた値
  • FullPrimaryScreenHeight- 1018。実際の解像度からスタート バーとウィンドウ ヘッダーを差し引いたもの。

これが私の解決策であり、私は以下を使用しますWorkArea.Height:

    protected T SetWindowLocation<T>(T window) where T : Window
    {
        //This function will set a window to appear in the center of the user's primary monitor.
        //Size will be set dynamically based on resoulution but will not shrink below a certain size nor grow above a certain size

        //Desired size constraints.  Makes sure window isn't too small if the users monitor doesn't meet the minimum, but also not too big on large monitors
        //Min: 1024w x 768h
        //Max: 1400w x 900h

        const int absoluteMinWidth = 1024;
        const int absoluteMinHeight = 768;
        const int absoluteMaxWidth = 1400;
        const int absoluteMaxHeight = 900;

        var maxHeightForMonitor = System.Windows.SystemParameters.WorkArea.Height - 100;
        var maxWidthForMonitor = System.Windows.SystemParameters.WorkArea.Width - 100;

        var height = Math.Min(Math.Max(maxHeightForMonitor, absoluteMinHeight), absoluteMaxHeight);
        var width = Math.Min(Math.Max(maxWidthForMonitor, absoluteMinWidth), absoluteMaxWidth);

        window.Height = height;
        window.Width = width;
        window.Left = (System.Windows.SystemParameters.FullPrimaryScreenWidth - width) / 2;
        window.Top = (System.Windows.SystemParameters.FullPrimaryScreenHeight - height) / 2;
        window.WindowStartupLocation = WindowStartupLocation.Manual;

        return window;
    }
于 2013-04-17T17:37:19.353 に答える
0

SystemParameters.WorkAreaを使用します。これにより、タスク バーによって占められていないプライマリ モニターの一部が得られるため、中央に配置しやすくなります。

于 2011-08-11T17:54:41.077 に答える