C#での画面(ディスプレイ)サイズ情報の取得方法を解説
- SE
- C#では画面サイズを自動調整できると聞いたのですが、詳しく教えてください。
- PM
- では、実際のソースコードを見ながら理解を深めていきましょう。
目次
C#での画面サイズについて
今回は、C#での画面サイズについてご説明します。C#では画面(ディスプレイ)について、サイズなどの情報を取得でき、画面サイズに合わせて、Formサイズなどを自動調整することもできます。
C#での画面サイズに興味のある方はぜひご覧ください。
プライマリ画面の取得
C#では、プライマリ画面の画面サイズ、ワーキングエリアを取得できます。ワーキングエリアとは、デスクトップ領域からタスクバーなどを除いた部分の領域です。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { Console.WriteLine(""PrimaryScreen.DeviceName:"" + Screen.PrimaryScreen.DeviceName); // デバイス名 Console.WriteLine(""PrimaryScreen.Bounds.X:"" + Screen.PrimaryScreen.Bounds.X); // 画面の左上X座標 Console.WriteLine(""PrimaryScreen.Bounds.Y:"" + Screen.PrimaryScreen.Bounds.Y); // 画面の左上Y座標 Console.WriteLine(""PrimaryScreen.Bounds.Height:"" + Screen.PrimaryScreen.Bounds.Height); // 画面の高さ Console.WriteLine(""PrimaryScreen.Bounds.Width:"" + Screen.PrimaryScreen.Bounds.Width); // 画面の横幅 Console.WriteLine(""PrimaryScreen.WorkingArea.Height:"" + Screen.PrimaryScreen.WorkingArea.Height); // ワーキングエリアの高さ Console.WriteLine(""PrimaryScreen.WorkingArea.Width:"" + Screen.PrimaryScreen.WorkingArea.Width); // ワーキングエリアの横幅 } } } |
実行すると、プライマリ画面の画面サイズやワーキングエリアが取得できていることが分かります。
このように、C#では、プライマリ画面の画面サイズ、ワーキングエリアを取得できます。
すべての画面情報の取得
画面が複数ある場合、すべての画面情報を取得できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { foreach (var screen in Screen.AllScreens) { Console.WriteLine(""Screen.DeviceName:"" + screen.DeviceName); // デバイス名 Console.WriteLine(""Screen.Bounds.X:"" + screen.Bounds.X); // 画面の左上X座標 Console.WriteLine(""Screen.Bounds.Y:"" + screen.Bounds.Y); // 画面の左上Y座標 Console.WriteLine(""Screen.Bounds.Height:"" + screen.Bounds.Height); // 画面の高さ Console.WriteLine(""Screen.Bounds.Width:"" + screen.Bounds.Width); // 画面の横幅 Console.WriteLine(""Screen.WorkingArea.Height:"" + screen.WorkingArea.Height); // ワーキングエリアの高さ Console.WriteLine(""Screen.WorkingArea.Width:"" + screen.WorkingArea.Width); // ワーキングエリアの横幅 } } } } |
実行すると、すべての画面情報が取得できることが分かります。
Formのある画面を取得
C#では、Formのある画面を取得できます。
画面が複数ある場合、どちらにFormがあるのかが分かるということです。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { this.Load += Form1_Load; this.ResizeEnd += Form1_ResizeEnd; } private void Form1_ResizeEnd(object sender, EventArgs e) { // Formのある画面を取得 Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); } private void Form1_Load(object sender, EventArgs e) { // Formのある画面を取得 Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); } } } |
Formの移動完了をResizeEndイベントで検出します。
Formを移動すると、どちらにFormがあるのかを取得しています。
このように、C#ではFormのある画面を取得できます。
画面の大きさにあわせてFromの調整
先ほどの応用として、画面サイズに合わせてFormのサイズを自動調整してみます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { this.Load += Form1_Load; this.ResizeEnd += Form1_ResizeEnd; } private void Form1_ResizeEnd(object sender, EventArgs e) { // Formのある画面を取得 Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); // Formのサイズを画面サイズの1/5に設定 this.Width = screen.Bounds.Width / 5; this.Height = screen.Bounds.Height / 5; } private void Form1_Load(object sender, EventArgs e) { // Formのある画面を取得 Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); // Formのサイズを画面サイズの1/5に設定 this.Width = screen.Bounds.Width / 5; this.Height = screen.Bounds.Height / 5; } } } |
画面をまたいでFormを移動すると、Formのサイズが自動調整されることが分かります。
Formの位置や大きさの取得
C#でのFormの位置や大きさは、LocationやSizeプロパティで取得できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { this.Load += Form1_Load; this.ResizeEnd += Form1_ResizeEnd; } private void Form1_ResizeEnd(object sender, EventArgs e) { Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); this.Width = screen.Bounds.Width / 5; this.Height = screen.Bounds.Height / 5; // Formの位置や大きさの取得 Console.WriteLine(""FormのX座標:"" + this.Location.X); Console.WriteLine(""FormのY座標:"" + this.Location.Y); Console.WriteLine(""Formの横幅:"" + this.Size.Width); Console.WriteLine(""Formの高さ:"" + this.Size.Height); } private void Form1_Load(object sender, EventArgs e) { Screen screen = Screen.FromControl(this); Console.WriteLine(""FormがあるScreen.DeviceName:"" + screen.DeviceName); this.Width = screen.Bounds.Width / 5; this.Height = screen.Bounds.Height / 5; // Formの位置や大きさの取得 Console.WriteLine(""FormのX座標:"" + this.Location.X); Console.WriteLine(""FormのY座標:"" + this.Location.Y); Console.WriteLine(""Formの横幅:"" + this.Size.Width); Console.WriteLine(""Formの高さ:"" + this.Size.Height); } } } |
このように、C#でのFormの位置や大きさは、LocationやSizeプロパティで取得できます。
C#の使い方を正しく理解しましょう
いかがでしたでしょうか。C#では画面(ディスプレイ)について、サイズなどの情報を取得できます。また、画面サイズに合わせて、Formサイズなどを自動調整することもできます。
ぜひご自身でソースコードを書いて、理解を深めてみましょう。
- SE
- 画面サイズに合わせて自動調整もできるなんて便利ですね。
- PM
- そうですね。サイズ情報を取得して自動調整も可能です。