使用C#绘制跨越所有显示器的线条

7
我有一个多显示器设置,我希望在用户移动光标时绘制一条垂直和水平的线。我想要绘制的线应跨越所有显示器。我不确定如何调整我的表单以实现这一点,因为当我将其全屏时,它只最大化到一个监视器。我需要为每个监视器制作一个表单,并在光标移动时向每个表单发送信号以重新绘制线吗?

enter image description here

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);
            }
        }
    }
}

1
我不确定这是可能的。你无法知道不同显示器的物理对齐方式。 - user47589
1
@Amy但是你知道边界框,这应该就足够了。 - GSerg
1
我建议尝试创建一个巨大的表单(x2是所有监视器边界框的尺寸),以十字形状(通过将其大部分区域设置为透明)移动该表单即可。Windows会自动完成剩下的事情。 - GSerg
监视器的位置和布局也可以更改。 - Pavel Anikhouski
无法工作,Winforms拥有坚如磐石的检查机制,防止窗口变得比其所在屏幕更大。 您需要一个小控制器类,该类了解所有三个表单并订阅它们的事件。 别忘了dpiAware。 - Hans Passant
显示剩余3条评论
1个回答

1

我修改了你的代码,使其适用于所有屏幕(我在两个屏幕上测试过,效果很好)。

 public partial class Form1 : Form
{

    int minX;
    int maxX;
    int minY;
    int maxY;
    public Form1()
    {
        InitializeComponent();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
        this.FormBorderStyle = FormBorderStyle.None;
        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        this.DoubleBuffered = true;

        FullScreen();
        CreateCloseButtons();

    }

    private void CloseButtons_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

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

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

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


        this.Location = new Point(minX, minY);
        //this.Location = this.PointToClient(new Point(minX, minY));
        this.Size = new Size(maxX - minX, maxY - minY);

    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        Console.WriteLine(this.Location.X + " - " + this.Location.Y);
        Console.WriteLine(this.Size.Width + " - " + this.Location.Y);
        base.OnMouseMove(e);
        Invalidate(false);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        var pos = this.PointToClient(Cursor.Position);

        using (var pen = new Pen(Color.Red))
        {
            pen.Width = 2;
            pen.DashStyle = DashStyle.Dot;
            e.Graphics.DrawLine(pen, pos.X, minY, pos.X, this.Height);
            e.Graphics.DrawLine(pen, minX, pos.Y, this.Width, pos.Y);
        }
    }



    private void CreateCloseButtons()
    {

        Button button1 = new System.Windows.Forms.Button();
        Button button2 = new System.Windows.Forms.Button();
        Button button3 = new System.Windows.Forms.Button();
        Button button4 = new System.Windows.Forms.Button();

        button1.Click += CloseButtons_Click;
        button2.Click += CloseButtons_Click;
        button3.Click += CloseButtons_Click;
        button4.Click += CloseButtons_Click;

        // 
        // top right
        // 
        button1.BackColor = System.Drawing.Color.Red;
        button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        button1.ForeColor = System.Drawing.SystemColors.ButtonFace;
        button1.Location = new System.Drawing.Point(0, 0);
        button1.Name = "button1";
        button1.Size = new System.Drawing.Size(21, 23);
        button1.TabIndex = 0;
        button1.Text = "X";
        button1.UseVisualStyleBackColor = false;
        // 
        // bottom left
        // 
        button2.BackColor = System.Drawing.Color.Red;
        button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        button2.ForeColor = System.Drawing.SystemColors.ButtonFace;
        button2.Location = new System.Drawing.Point(0, this.Height - 23);
        button2.Name = "button2";
        button2.Size = new System.Drawing.Size(21, 23);
        button2.TabIndex = 1;
        button2.Text = "X";
        button2.UseVisualStyleBackColor = false;
        button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
        // 
        // bottom right
        // 
        button3.BackColor = System.Drawing.Color.Red;
        button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        button3.ForeColor = System.Drawing.SystemColors.ButtonFace;
        button3.Location = new System.Drawing.Point(this.Width - 21, this.Height - 23);
        button3.Name = "button3";
        button3.Size = new System.Drawing.Size(21, 23);
        button3.TabIndex = 2;
        button3.Text = "X";
        button3.UseVisualStyleBackColor = false;
        button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        // 
        // top right
        // 
        button4.BackColor = System.Drawing.Color.Red;
        button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        button4.ForeColor = System.Drawing.SystemColors.ButtonFace;
        button4.Location = new System.Drawing.Point(this.Width - 21, 0);
        button4.Name = "button4";
        button4.Size = new System.Drawing.Size(21, 23);
        button4.TabIndex = 3;
        button4.Text = "X";
        button4.UseVisualStyleBackColor = false;
        button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));



        this.Controls.Add(button4);
        this.Controls.Add(button3);
        this.Controls.Add(button2);
        this.Controls.Add(button1);
    }


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接