我有两台显示器(HP EliteDisplay E190i),两台都连接到两台电脑(2x VGA + DP)。 这些显示器也支持DVI,因此我不想使用愚蠢的切换按钮,每次我希望在计算机之间切换时,我都需要浏览显示器的菜单。我以前有笨重的显示器和转换非常容易,但我不能习惯整个导航的东西 - 它经常让人感到困惑......
所以这是交易 - 我希望能够通过执行命令在计算机之间快速切换。显然这不能直接完成(计算机没有以任何方式相互连接),但当监视器进入省电模式时(或当操作系统关闭它们时),监视器开始扫描可用输入。这样他们就可以锁定另一台计算机并解决问题。
虽然足够的介绍,我已经尝试了this solution并且效果很好,但它并不完美:
我尝试在将监视器发送到睡眠状态之前根据this answer禁用输入,然后在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。
有没有办法让整个仪式上班?
答案 0 :(得分: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);