Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
禁用鼠标单击windows屏幕的特定区域c#wpf_C#_Wpf_Windows_Mouseevent - Fatal编程技术网

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

禁用鼠标单击windows屏幕的特定区域c#wpf,c#,wpf,windows,mouseevent,C#,Wpf,Windows,Mouseevent,在这种情况下,我希望使用TightVNC向客户机提供远程访问,以允许他们在第三方应用程序上执行某些任务 我只想让他们点击屏幕的某些部分,例如禁用“关闭”按钮,并禁用一些区域。我想知道是否有一种方法可以通过编程在windows上截取鼠标按下事件,并且如果他们在某个特定区域上按下鼠标,是否只返回而不执行单击?或者,是否有一种方法可以覆盖一个透明背景,该背景始终位于所选区域的屏幕前部 我的应用程序是用WPF c#编写的,因此如果有任何例子可以实现这一点,我们将不胜感激。我尝试过创建一些虚拟透明窗口,问

在这种情况下,我希望使用TightVNC向客户机提供远程访问,以允许他们在第三方应用程序上执行某些任务

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

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

提前感谢。

选项1: 从技术上讲,您应该能够为鼠标事件设置默认值,并在需要时抑制它们。是可用于为鼠标事件分配处理程序并通过设置
e.Handled=true有条件地抑制它们的库之一

备选案文2: 如果您只需要为特定的应用程序阻止鼠标事件,那么您可以覆盖它们的方法来过滤或抑制特定的基于区域的鼠标事件

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

类似地,您可以使用以下示例代码在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:
如果您需要阻止鼠标单击特定的现有运行进程,前提是目标应用程序正在使用基于windows窗体的UI或WPF的.NET,则您可以/application并覆盖其
WindowsProc
行为,如选项2中所述。您也可以参考这一点。

您可以使透明窗口始终位于顶部检查这也是用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);
    }
}