首页 > 解决方案 > 使用跨越所有显示器的 C# 绘制线

问题描述

我有一个多显示器设置,我想在用户移动光标时绘制一条垂直和水平线。我要绘制的线条应该跨越所有显示器。我不完全确定如何调整我的表单以使其成为可能,因为当我将其设为全屏时,它只会最大化到一台显示器。

我是否必须为每个监视器制作一个表格并在光标移动时向每个监视器发送信号以重新绘制线条?

在此处输入图像描述

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

namespace fitAllScreens
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            FullScreen();
        }


        public void FullScreen()
        {
            List<int> xBounds = new List<int>() {};
            List<int> yBounds = new List<int>() {};

            foreach (Screen screen in Screen.AllScreens)
            {
                var bounds = screen.Bounds;
                xBounds.Add(bounds.X);
                xBounds.Add(bounds.Right);
                yBounds.Add(bounds.Y);
                yBounds.Add(bounds.Bottom);
            }

            int minX = xBounds.Min();
            int maxX = xBounds.Max();
            int minY = yBounds.Min();
            int maxY = yBounds.Max();

            Console.WriteLine(minX + " - " + maxX + " - " + minY + " - " + maxY);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var graphics = e.Graphics;
            base.OnPaint(e);

            // Draw ruler guides
            Console.WriteLine(Cursor.Position);

            var pos = this.PointToClient(Cursor.Position);

            using (var pen = new Pen(Color.Red))
            {
                pen.DashStyle = DashStyle.Dot;
                var screenBounds = Screen.PrimaryScreen.Bounds;
                graphics.DrawLine(pen, pos.X, screenBounds.Y, pos.X, screenBounds.Height);
                graphics.DrawLine(pen, screenBounds.X, pos.Y, screenBounds.Width, pos.Y);
            }
        }
    }
}

标签: c#winforms

解决方案


推荐阅读