7

我有两台显示器(HP EliteDisplay E190i),都连接到两台电脑(2x VGA + DP)。
这些显示器还支持 DVI,所以每次我想在计算机之间切换时,我都必须浏览显示器的菜单,而不是使用愚蠢的切换按钮。我以前有更笨的显示器,切换真的很容易,但我就是不习惯整个导航——它经常让人困惑……

所以这是交易 - 我希望能够通过执行命令在计算机之间快速切换。显然这不能直接完成(计算机没有以任何方式相互连接),但是当显示器进入省电模式(或操作系统关闭它们时)时,显示器开始扫描可用输入。这样他们就会锁定到另一台计算机并解决问题。

虽然介绍得够多了,但我已经尝试过这个解决方案并且效果很好,但它并不完美:

  • 它有一个淡出动画,在显示器实际关闭之前需要几秒钟
  • 在上述淡出动画期间,我必须不要触摸鼠标/键盘,否则它会被取消

在将监视器发送到睡眠状态之前,我尝试根据此答案禁用输入,然后在 5 秒后重新启用它,但这也不起作用,因为:

  • 它要求我以管理员权限运行应用程序,否则输入不会被阻止
  • 即使在以管理员权限运行时输入被阻止,我仍然可以在淡出动画期间移动鼠标或按键盘上的某些键来取消它(即使指针没有移动,或者键盘输入被忽略)。

这是我的代码:

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

[DllImport("user32.dll")]
private static extern int BlockInput(int fBlockIt);

static void Main()
{
    SendMessage(0xFFFF, 0x112, 0xF170, 2);
    try
    {
        int result = BlockInput(1);
        Console.WriteLine(result);
        Thread.Sleep(5000);
    }
    finally
    {
        BlockInput(0);
    }
}

我在两台计算机上都使用 Windows 7 Enterprise x64。

有什么办法可以让整个仪式顺利进行吗?

4

1 回答 1

0

我不得不编辑我的答案,因为愚蠢地我没有完整地阅读你的问题。

我建议创建一个应用程序,它位于两台计算机上,通过一个简单的 TCP 服务器/客户端套接字相互发送请求。

例如,这将允许您按下 PC 1 上的按钮,使其进入睡眠状态并对监视器执行相同操作,并向 PC 2 发送消息以唤醒并窃取监视器输入。就速度而言,我想这取决于几个变量,但这可以在以后进行。

TCP 客户端/服务器:

using System;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;

namespace ClientSocket_System
  {
   class tcpClientSocket
   {

    #region Global Variables

    TcpClient tcpService;
    IPEndPoint serverEndPoint;
    NetworkStream netStream;

    #endregion


    public void commToServer()
    {
        tcpService = new TcpClient();
        serverEndPoint = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xx"), xxxx); //Enter IP address of computer here along with a port number if needed
        try
        {
            tcpService.Connect(serverEndPoint);
            netStream = tcpService.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes("SwitchComputers");
            netStream.Write(buffer, 0, buffer.Length);
            netStream.Flush();
            tcpService.Close();
        }
        catch(Exception ex)
        {
        }
    }
   }
}

和服务器:

using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;

namespace ClientSocket_System
{
    class tcpServerTerminal
    {



        private TcpListener tcpListener;
        private Thread listenThread;
        private TcpClient tcpService;
        string msgFromClient;



        public void ServerStart()
        {
            tcpListener = new TcpListener(IPAddress.Any, 5565);
            listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();
        }

        public void ListenForClients()
        {
            tcpListener.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();

                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        public void HandleClientComm(object client)
        {
            tcpService = (TcpClient)client;

            NetworkStream netStream = tcpService.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = netStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();

                msgFromClient = encoder.GetString(message, 0, bytesRead);

                if (msgFromClient == "SwitchComputers")
                {

                    //RUN CODE HERE TO ACTION PC SLEEP AND MONITOR SLEEP

                    msgFromClient = null;
                }
            }
        }

        public void SocketSend()
        {
            NetworkStream streamToClient = tcpService.GetStream();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] buffer = encoder.GetBytes("SwitchComputers");

            streamToClient.Write(buffer, 0, buffer.Length);
            streamToClient.Flush();
        }

    }
}

类似的东西,至少值得研究一下,上面的代码并没有完全完善,但它可以让您通过家庭网络控制两台计算机的操作,从而允许同时执行特定命令:睡眠/唤醒等.

希望这能给你一个新的调查方向。

另外,我认为将阻止输入的代码格式化为最佳做法:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool BlockInput([In, MarshalAs(UnmanagedType.Bool)] bool fBlockIt);
于 2016-03-16T12:00:13.847 回答