gpt4 book ai didi

c# - 禁用鼠标单击 Windows 屏幕的特定区域 c# wpf

转载 作者:可可西里 更新时间:2023-11-01 10:14:32 29 4
gpt4 key购买 nike

在这种情况下,我想使用 TightVNC 向客户端授予远程访问权限,以允许他们在第三方应用程序上执行某些任务。

我只想让他们点击屏幕的某些部分,例如禁用“关闭”按钮,并禁用一些区域。我想知道是否有一种方法可以以编程方式拦截 Windows 上的鼠标按下事件,如果他们在特定区域按下鼠标只是返回而不是执行单击?或者是否有办法在选定区域覆盖始终位于屏幕前方的透明背景?

我的应用程序是用 WPF c# 编写的,因此如果有任何示例可以实现这一点,我们将不胜感激。我曾尝试创建一些虚拟透明窗口,问题是当用户单击其他应用程序时,他们会转到后台,因此无法实现目标。

提前致谢。

最佳答案

选项 1:

技术上你应该能够attach global hooks鼠标事件并在需要时抑制它们。 globalmousekeyhook是可用于将处理程序分配给鼠标事件并通过设置 e.Handled = true 有条件地抑制它们的库之一。

选项 2:

如果您只需要为特定应用程序阻止鼠标事件 - 那么您可以覆盖它们的 WindowProc过滤或抑制基于特定区域的鼠标事件的方法。

例如,对于windows 窗体应用程序,您可以使用以下代码来禁止在窗口的第四象限区域中单击鼠标。 (注意:此代码需要添加到您试图阻止访问的应用程序中)

public class Setup
{
//call this method during application start/load
public static bool Start()
{
Application.AddMessageFilter(new MessageFilter());
return true;
}
}

public class MessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// and use other params to determine mouse click location
switch (m.Msg)
{
case 0x201:
//left button down
return IsInFourthQuadrant();//(filter the message and don't let app process it)
case 0x202:
//left button up, ie. a click
break;
case 0x203:
//left button double click
return IsInFourthQuadrant(); //(filter the message and don't let app process it)
}

return false;
}

private static bool IsInFourthQuadrant()
{
var window = Application.OpenForms[0];
var screen_coords = Cursor.Position;

var bounds = window.Bounds;
var fourthQuadrant = new Rectangle(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2, bounds.Width / 2, bounds.Height / 2);

return fourthQuadrant.Contains(screen_coords);
}
}

同样,您可以使用以下示例代码来禁用 WPF 应用程序第四象限区域中的鼠标单击:

public class Setup
{
public static bool Start()
{
HwndSource source = PresentationSource.FromVisual(Application.Current.MainWindow) as HwndSource;
source.AddHook(WndProc);

return true;
}

private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x201:
//left button down
handled = IsInFourthQuadrant();//(filter the message and don't let app process it)
break;
case 0x202:
//left button up, ie. a click
break;
case 0x203:
//left button double click
handled = IsInFourthQuadrant(); //(filter the message and don't let app process it)
break;
}

return IntPtr.Zero;
}

private static bool IsInFourthQuadrant()
{
var window = Application.Current.MainWindow;
var coords = Mouse.GetPosition(Application.Current.MainWindow);

var fourthQuadrant = new Rect(window.Width / 2, window.Height / 2, window.Width / 2, window.Height / 2);

return fourthQuadrant.Contains(coords);
}
}

选项 3:

如果您需要阻止鼠标点击特定的现有运行进程,前提是目标应用程序使用 .NET 和基于 Windows 窗体的 UI 或 WPF - 那么您可以 inject your .NET assemblies into the existing process/application 并覆盖其 WindowsProc 行为,如选项 2 中所述。也可以引用这个sample code同样。

关于c# - 禁用鼠标单击 Windows 屏幕的特定区域 c# wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398860/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com