LoginSignup
0
1

More than 3 years have passed since last update.

C# - マルチディスプレイ(マルチスクリーン)における座標の取得について調べてみた - Windows10

Last updated at Posted at 2019-12-14

調査内容

ウィンドウ(C#のForm)をスクリーンをまたぐ位置に置いてみて、クリックしたときに得られる座標を調べた。

調査結果

実験してみたところ、Formはどちらか一方のScreenに所属する扱いのようである。
「左端をクリックしたらPrimaryスクリーン、右端をクリックしたらSecondaryスクリーン」というわけではない

image.png

image.png

座標については、スクリーンをまたいで連続しているようである。

調査に使ったプログラムのコード


using System;
using System.Drawing;
using System.Windows.Forms;

class MultiScreenTest : Form
{
    Button btn;

    MultiScreenTest()
    {
        Text = "Multi Screen Test";
        ClientSize = new Size(500,400);

        btn = new Button();
        btn.Text = "Show X,Y";
        btn.Size = new Size(500,200);
        btn.MouseClick += Btn_MouseClick;
        Controls.Add(btn);
    }

    void Btn_MouseClick(object sender, MouseEventArgs e)
    {
        Point screenP = btn.PointToScreen(e.Location);

        Console.Write("Click: ");
        Console.Write(screenP.X);
        Console.Write(", ");
        Console.Write(screenP.Y);
        Screen screen = Screen.FromPoint(screenP);
        Console.Write("  Primary:");
        Console.WriteLine(screen.Primary);
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new MultiScreenTest());
    }
}

プログラムの実行結果


Click: 457, 251  Primary:True
Click: 119, 122  Primary:True
Click: 114, 114  Primary:True
Click: 31, 49  Primary:True
Click: 1878, 387  Primary:False
Click: 1921, 390  Primary:False
Click: 1958, 391  Primary:False
Click: 1916, 410  Primary:False
Click: 1881, 413  Primary:False
Click: 1838, 401  Primary:False
Click: 1153, 283  Primary:True
Click: 1800, 378  Primary:False
Click: 1730, 382  Primary:False
Click: 1074, 237  Primary:True
Click: 1099, 242  Primary:True
Click: 1529, 341  Primary:True
Click: 2239, 274  Primary:False
Click: 2231, 322  Primary:False

参考サイト

  1. .NET TIPS スクリーン座標←→クライアント座標の変換を行うには?
  2. Screenクラス - Microsoft Docs

おまけ

2つ目のスクリーンのタスクバーのWindowClass名はShell_SecondaryTrayWndになるようである。

下記は https://qiita.com/kob58im/items/3587d8e595e655e9391d で作ったツールを使用して確認した。

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1