未来の自分へ知識谷金

ゆーきのエンジニアブログ
C#Visual Studioプログラミング

【C#】画面の領域をマウスで範囲指定する

.NETC#Visual StudioVisual Studio 2022

C#でデスクトップ上の領域をマウスで選択するソースコードを記載します。

実行すると半透明のフォームを画面一杯に表示し、

マウスをドラッグすると選択した座標を取得できます。

選択領域は赤枠で追従します。

■DialosRegionSelection.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SCSupportTool
{

    public partial class DialogRegionSelection : Form
    {
        private Point startPos;
        private Point endPos;
        private bool isSelecting;
        private int reservePaint = 0;

        public DialogRegionSelection(Screen screen)
        {
            InitializeComponent();

            this.Screen = screen;
            this.FormBorderStyle = FormBorderStyle.None;
            this.BackColor = Color.LightGray;
            this.Opacity = 0.5;
            this.StartPosition = FormStartPosition.Manual;
            this.Bounds = this.Screen.Bounds;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.MouseDown += OverlayForm_MouseDown;
            this.MouseMove += OverlayForm_MouseMove;
            this.MouseUp += OverlayForm_MouseUp;
            this.Paint += OverlayForm_Paint;
        }

        public Screen Screen { get; set; }
        public Rectangle SelectedRegion { get; set; }
        public Rectangle ScreenRegion { get { return this.RectangleToScreen(this.SelectedRegion); } }

        private void OverlayForm_MouseDown(object sender, MouseEventArgs e)
        {
            isSelecting = true;
            this.startPos = e.Location;
            SelectedRegion = new Rectangle(e.Location, new Size());
        }

        private void OverlayForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (isSelecting)
            {
                this.endPos = e.Location;
                int x1 = this.startPos.X;
                int x2 = this.endPos.X;
                int y1 = this.startPos.Y;
                int y2 = this.endPos.Y;
                SelectedRegion = Rectangle.FromLTRB(Math.Min(x1, x2), Math.Min(y1, y2), Math.Max(x1, x2), Math.Max(y1, y2));
                this.reservePaint++;
            }
        }

        private void OverlayForm_MouseUp(object sender, MouseEventArgs e)
        {
            isSelecting = false;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void OverlayForm_Paint(object sender, PaintEventArgs e)
        {
            if (isSelecting)
            {
                using (var pen = new Pen(Color.Red, 2))
                {
                    e.Graphics.DrawRectangle(pen, SelectedRegion);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.reservePaint > 0)
            {
                this.reservePaint = 0;
                this.Invalidate();
            }
        }
    }
}

■使い方

private List<DialogRegionSelection> dlgRegionSelectionList = new List<DialogRegionSelection>();

        private void buttonSelectRegion_Click(object sender, EventArgs e)
        {
            Screen[] screens = Screen.AllScreens;
            foreach (var screen in screens)
            {
                DialogRegionSelection dlg = new DialogRegionSelection(screen);
                dlg.FormClosed += RegionSelection_DialogClosed;
                dlg.Show(this);
                this.dlgRegionSelectionList.Add(dlg);
            }
        }

        private void RegionSelection_DialogClosed(object sender, FormClosedEventArgs e)
        {
            DialogRegionSelection dlg = (DialogRegionSelection)sender;
            if (dlg.DialogResult != DialogResult.OK) return;
            Rectangle screenRegion = dlg.ScreenRegion;
            foreach (var d in this.dlgRegionSelectionList)
            {
                if (d.IsDisposed == false && d.Disposing == false) d.Dispose();
            }
            this.dlgRegionSelectionList.Clear();
        }

以上!